NetworkController.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.NetworkTreeNodeStatus;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.SelectionModel;
  14. /**
  15. * Controller for manipulation of the network model with methods like
  16. *
  17. *
  18. * @author Andreas T. Meyer-Berg
  19. */
  20. public class NetworkController {
  21. /**
  22. * Model which will be manipulated
  23. */
  24. private Model model;
  25. /**
  26. * Controller which can be use
  27. */
  28. private Controller controller;
  29. /**
  30. * NetworkTreeSettings Controller
  31. */
  32. private NetworkTreeSettingsController networkTreeSettings;
  33. /**
  34. * Packet Capture Controller
  35. */
  36. private PacketCaptureController captureController;
  37. /**
  38. * Creates a new NetworkController, which may manipulate the given model and use the controller
  39. * @param model Model which can be manipulated
  40. * @param controller main controller for updates etc
  41. */
  42. public NetworkController(Model model, Controller controller) {
  43. this.model = model;
  44. this.controller = controller;
  45. networkTreeSettings = controller.getSettingsController().getNetworkTreeSettingsController();
  46. captureController = controller.getSimulationController().getPacketCaptureController();
  47. }
  48. /**
  49. * Validate all device positions, move all devices into the bounds, if they are outside the visualization
  50. */
  51. public void validateDevicePosition() {
  52. // Update all device positions
  53. for (SmartDevice d : model.getDevices()) {
  54. d.setX(controller.getSettingsController().scalePos(d.getX(), 1.0, controller.getSettingsController().getWidth()));
  55. d.setY(controller.getSettingsController().scalePos(d.getY(), 1.0, controller.getSettingsController().getHeight()));
  56. d.setZ(controller.getSettingsController().scalePos(d.getZ(), 1.0, controller.getSettingsController().getDepth()));
  57. }
  58. }
  59. /**
  60. * Adds SmartDevice to the Model and verifies that it is inside the model
  61. * bounds
  62. *
  63. * @param sd
  64. * SmartDevice which should be added to the model
  65. */
  66. public void addSmartDevice(SmartDevice sd) {
  67. if (model.getDevices().contains(sd))
  68. return;
  69. model.addDevices(sd);
  70. // validate Position
  71. sd.setX(controller.getSettingsController().scalePos(sd.getX(), 1.0, controller.getSettingsController().getWidth()));
  72. sd.setY(controller.getSettingsController().scalePos(sd.getY(), 1.0, controller.getSettingsController().getHeight()));
  73. sd.setZ(controller.getSettingsController().scalePos(sd.getZ(), 1.0, controller.getSettingsController().getDepth()));
  74. }
  75. /**
  76. * Creates a new SmartDevice at the given Position. The Device is moved
  77. * into the model bounds, if the position is outside the bounds
  78. *
  79. * @param name
  80. * name of the smartDevice
  81. * @param x_pos
  82. * position on the x-Axis
  83. * @param y_pos
  84. * position on the y-Axis
  85. * @param z_pos
  86. * position on the z-Axis
  87. */
  88. public SmartDevice createSmartDevice(String name, int x_pos, int y_pos, int z_pos) {
  89. SmartDevice sd = new SmartDevice(name);
  90. sd.setX(controller.getSettingsController().scalePos(x_pos, 1.0, controller.getSettingsController().getWidth()));
  91. sd.setY(controller.getSettingsController().scalePos(y_pos, 1.0, controller.getSettingsController().getHeight()));
  92. sd.setZ(controller.getSettingsController().scalePos(z_pos, 1.0, controller.getSettingsController().getDepth()));
  93. model.addDevices(sd);
  94. return sd;
  95. }
  96. /**
  97. * Deletes the given SmartDevice toDelete, removes it from its links and
  98. * connections.
  99. *
  100. * @param toDelete
  101. * the SmartDevice to delete
  102. */
  103. public void deleteSmartDevice(SmartDevice toDelete) {
  104. if (toDelete == null)
  105. return;
  106. // Remove from Collectors
  107. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  108. for(PacketCollector collector:captureController.getPacketCollectors()){
  109. if(collector.getDevices().contains(toDelete)){
  110. captureController.removeDeviceFromCollector(collector, toDelete);
  111. }
  112. }
  113. // Delete Connections
  114. for (Port p : toDelete.getPorts()) {
  115. //Skip ports that are not connected
  116. if(p.getConnection()==null)
  117. continue;
  118. removeDeviceFromConnectionAndProtocol(p, p.getConnection());
  119. }
  120. // Remove from Links
  121. LinkedList<Link> links = new LinkedList<Link>(toDelete.getLinks());
  122. for (Link link : links)
  123. removeSmartDeviceFromLink(toDelete, link);
  124. links.clear();
  125. // Remove Links from Device
  126. toDelete.getLinks().clear();
  127. //Remove all ports from the device
  128. toDelete.getPorts().clear();
  129. //Remove from network Tree
  130. networkTreeSettings.removeStatusOfObject(toDelete);
  131. model.getDevices().remove(toDelete);
  132. }
  133. /**
  134. * Returns smartDevices of the model, which are not hidden
  135. * @return all SmartDevices which are not hidden
  136. */
  137. public Collection<SmartDevice> getVisibleSmartDevices(){
  138. LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>(model.getDevices());
  139. devices.removeIf(c->!controller.getSettingsController().getNetworkTreeSettingsController().isVisible(c));
  140. return devices;
  141. }
  142. /**
  143. * Returns smartDevices of the model
  144. * @return all SmartDevices of the model
  145. */
  146. public Collection<SmartDevice> getSmartDevices(){
  147. return model.getDevices();
  148. }
  149. /**
  150. * Removes the SmartDevice from the given Link
  151. *
  152. * @param toRemove the Device that should be removed from the link
  153. * @param link the Link, which contains the SmartDevice
  154. */
  155. public void removeSmartDeviceFromLink(SmartDevice toRemove, Link link) {
  156. if(link != null){
  157. link.removeDevice(toRemove);
  158. if(link.getDevices().size()==0)
  159. deleteLink(link);
  160. }
  161. if(toRemove!=null)
  162. toRemove.removeLink(link);
  163. }
  164. /**
  165. * Moves the SmartDevice device to the specified location, if it exists
  166. *
  167. * @param device
  168. * the device to move
  169. * @param x
  170. * new x position
  171. * @param y
  172. * new y position
  173. * @param z
  174. * new z position
  175. */
  176. public void moveSmartDevice(SmartDevice device, int x, int y, int z) {
  177. device.setX(x);
  178. device.setY(y);
  179. device.setZ(z);
  180. }
  181. /**
  182. * Adds Link to the model
  183. * @param link link to add
  184. */
  185. public void addLink(Link link){
  186. if(link!=null && !model.getConnectionNetworks().contains(link))
  187. model.addConnectionNetwork(link);
  188. }
  189. /**
  190. * Removes Link from the Model
  191. * @param link link to remove
  192. */
  193. public void removeLink(Link link){
  194. model.getConnectionNetworks().remove(link);
  195. }
  196. /**
  197. * Return visible links of the model
  198. * @return visible link
  199. */
  200. public Collection<Link> getVisibleLinks(){
  201. LinkedList<Link> links = new LinkedList<Link>(model.getConnectionNetworks());
  202. links.removeIf(c->!controller.getSettingsController().getNetworkTreeSettingsController().isVisible(c));
  203. return links;
  204. }
  205. /**
  206. * Return links of the model
  207. * @return link
  208. */
  209. public Collection<Link> getLinks(){
  210. return model.getConnectionNetworks();
  211. }
  212. /**
  213. * Add Connection to the model
  214. * @param connection connection to be added
  215. */
  216. public void addConnection(Connection connection){
  217. if(connection!=null && !getConnections().contains(connection))
  218. model.addConnection(connection);
  219. }
  220. /**
  221. * Remove the connection from the model
  222. * @param connection connection to be removed
  223. */
  224. public void removeConnection(Connection connection){
  225. model.getConnections().remove(connection);
  226. }
  227. /**
  228. * Returns visible connections of the model
  229. * @return visible connections
  230. */
  231. public Collection<Connection> getVisibleConnections(){
  232. LinkedList<Connection> links = new LinkedList<Connection>(model.getConnections());
  233. links.removeIf(c->!controller.getSettingsController().getNetworkTreeSettingsController().isVisible(c));
  234. return links;
  235. }
  236. /**
  237. * Returns connections of the model
  238. * @return connections
  239. */
  240. public Collection<Connection> getConnections(){
  241. return model.getConnections();
  242. }
  243. /**
  244. * Adds the smartDevice to the link
  245. * @param link link to be added to the device
  246. * @param smartDevice device to be added to the link
  247. */
  248. public void addLinkToDevice(Link link, SmartDevice smartDevice) {
  249. if(link == null || smartDevice == null)return;
  250. if(!link.getDevices().contains(smartDevice)){
  251. link.addDevice(smartDevice);
  252. }
  253. if(!smartDevice.getLinks().contains(smartDevice)){
  254. smartDevice.addLink(link);
  255. }
  256. }
  257. /**
  258. * Remove the smartDevice from the link
  259. * @param link link, the device should be removed from
  260. * @param smartDevice device which should be removed
  261. */
  262. public void removeLinkFromDevice(Link link, SmartDevice smartDevice) {
  263. if(smartDevice!=null)
  264. smartDevice.removeLink(link);
  265. if(link!=null){
  266. link.removeDevice(smartDevice);
  267. if(link.getDevices().size()==0)
  268. deleteLink(link);
  269. }
  270. }
  271. /**
  272. * Changes Roles of the device to the newRole, returns true, if the role was successfully changed, false if not.
  273. * If false is returned, the device will no longer be part of this connection.
  274. *
  275. * @param protocol protocol which should be edited
  276. * @param con Connection, which contains the protocol
  277. * @param device device which should be added
  278. * @param newRole new role of the device
  279. * @return true if new role was set, false if not
  280. */
  281. public boolean changeRoleOfDevice(Protocol protocol, Connection con, Port device, int newRole){
  282. if(newRole < 0 || newRole >= protocol.getNumberOfRoles()){
  283. protocol.removeDevice(device);
  284. removeDeviceFromConnectionAndProtocol(device, con);
  285. return false;
  286. } else if(protocol.getDevicesWithRole(newRole).contains(device)){
  287. if(!con.getParticipants().contains(device))
  288. con.addSmartDevice(device);
  289. if(device.getConnection()!=con)
  290. device.setConnection(con);
  291. return true;
  292. }
  293. else{
  294. protocol.removeDevice(device);
  295. boolean added = protocol.addDeviceOfRole(device, newRole);
  296. if(added){
  297. if(!con.getParticipants().contains(device))
  298. con.addSmartDevice(device);
  299. if(device.getConnection()!=con)
  300. device.setConnection(con);
  301. return true;
  302. }else{
  303. if(con.getParticipants().contains(device))
  304. con.removeSmartDevice(device);
  305. if(device.getConnection()!=null)
  306. device.setConnection(null);
  307. return false;
  308. }
  309. }
  310. }
  311. /**
  312. * Removes Device p from the Connection and Protocol
  313. * @param p Port/Device to remove
  314. * @param connection connection, which should remove the device
  315. */
  316. public void removeDeviceFromConnectionAndProtocol(Port p, Connection connection){
  317. if(connection != null){
  318. connection.removeSmartDevice(p);
  319. //TODO: Protocol ?
  320. if(connection.getProtocol()!=null){
  321. connection.getProtocol().removeDevice(p);
  322. if(connection.getParticipants().size() == 0)
  323. deleteConnection(connection);
  324. }
  325. }
  326. if(p!=null && p.getConnection() == connection)
  327. p.setConnection(null);
  328. }
  329. /**
  330. * Removes Device p from the Connection and Protocol
  331. * @param p Port/Device to remove
  332. * @param connection connection, which should remove the device
  333. */
  334. public void removeDeviceFromConnection(Port p, Connection connection){
  335. if(connection != null){
  336. connection.removeSmartDevice(p);
  337. p.setConnection(null);
  338. if(connection.getParticipants().isEmpty())
  339. deleteConnection(connection);
  340. }
  341. if(p != connection)
  342. p.setConnection(null);
  343. }
  344. /**
  345. * Adds Device p to the connection with the specified role
  346. * @param p Port/Device to be added
  347. * @param connection Connection
  348. * @param role new role of the device (See {@link Protocol} Roles)
  349. * @return true, if the device was added
  350. */
  351. public boolean addDeviceToConnectionAndProtocol(Port p, Connection connection, int role){
  352. if(connection.getProtocol().getDevicesWithRole(role).contains(p) || connection.getProtocol().addDeviceOfRole(p, role)){
  353. //If port already has the role, or it could be assigned - just check the fields
  354. if(!connection.getParticipants().contains(p))
  355. connection.addSmartDevice(p);
  356. if(p.getConnection()!=connection)
  357. p.setConnection(connection);
  358. return true;
  359. }else {
  360. //Device could not be added -> Remove
  361. if(p.getConnection()==connection)
  362. p.setConnection(null);
  363. if(connection.getParticipants().contains(p))
  364. connection.removeSmartDevice(p);
  365. if(connection.getProtocol().getDevices().contains(p))
  366. connection.getProtocol().removeDevice(p);
  367. return false;
  368. }
  369. }
  370. /**
  371. * Adds Device p to the connection
  372. * @param p Port/Device to be added
  373. * @param connection Connection
  374. */
  375. public void addDeviceToConnection(Port p, Connection connection){
  376. if(p==null || connection == null)
  377. return;
  378. if(!connection.getParticipants().contains(p))
  379. connection.addSmartDevice(p);
  380. p.setConnection(connection);
  381. }
  382. /**
  383. * Changes the link of the given connection to the new link. Returns true if it was successfully changed, false if it could not be changed.
  384. * @param connection connection which should be edited
  385. * @param link new link
  386. * @return true on successful change, false on failure and restore
  387. */
  388. public boolean changeLinkOfConnection(Connection connection, Link link) {
  389. if(connection !=null && link != null){
  390. addConnectionToLink(connection, link);
  391. return true;
  392. }else{
  393. return false;
  394. }
  395. }
  396. /**
  397. * Adds the given connection to the link
  398. * @param connection connection to be added
  399. * @param link link the connection should be added to
  400. */
  401. public void addConnectionToLink(Connection connection, Link link) {
  402. if(connection == null || link == null)return;
  403. /**
  404. * Remove connection from Old Link
  405. */
  406. if(connection.getLink()!=null)
  407. removeConnectionFromLink(connection, connection.getLink());
  408. /**
  409. * Add Connection to new Link
  410. */
  411. connection.setLink(link);
  412. if(!link.getConnections().contains(connection))
  413. link.addConnection(connection);
  414. /**
  415. *
  416. */
  417. for(Port p:connection.getParticipants()){
  418. if(p!=null && !link.getDevices().contains(p.getOwner())){
  419. addLinkToDevice(link, p.getOwner());
  420. }
  421. }
  422. }
  423. /**
  424. * Changes the type of the connection to the new Type and updates all references
  425. * @param connection connection to be updated
  426. * @param newConnectionClass class of new type
  427. * @return newly created connection
  428. */
  429. public Connection changeConnectionType(Connection connection, Class<? extends Connection> newConnectionClass) {
  430. Connection newCon = null;
  431. try{
  432. newCon = newConnectionClass.newInstance();
  433. copyNetworkTreeStatus(connection, newCon);
  434. newCon.setProtocol(connection.getProtocol());
  435. newCon.setStatus(connection.getStatus());
  436. newCon.setPacketLossProbability(connection.getPacketLossProbability());
  437. newCon.setName(connection.getName());
  438. if(getConnections().contains(connection)){
  439. removeConnection(connection);
  440. addConnection(newCon);
  441. }
  442. addConnectionToLink(newCon, connection.getLink());
  443. connection.setProtocol(null);
  444. for (Iterator<Port> p = connection.getParticipants().iterator(); p.hasNext();) {
  445. Port type = (Port) p.next();
  446. removeDeviceFromConnection(type, connection);
  447. addDeviceToConnection(type, newCon);
  448. }
  449. }catch(Exception e){
  450. System.out.println("Error while changing protocol: "+e.toString());
  451. /**
  452. * Restore old connection on Failure
  453. */
  454. if(newCon != null){
  455. if(getConnections().contains(newCon)){
  456. removeConnection(newCon);
  457. addConnection(connection);
  458. }
  459. if(newCon.getProtocol()!=null){
  460. connection.setProtocol(newCon.getProtocol());
  461. newCon.setProtocol(null);
  462. }
  463. if(newCon.getLink()!=null)
  464. newCon.getLink().removeConnection(newCon);
  465. newCon.setLink(null);
  466. for (Iterator<Port> p = connection.getParticipants().iterator(); p.hasNext();) {
  467. Port type = (Port) p.next();
  468. if(type.getConnection()!=connection)
  469. type.setConnection(connection);
  470. newCon.removeSmartDevice(type);
  471. }
  472. }
  473. return null;
  474. }
  475. controller.getSettingsController().getConfigurationManager().getSelectionModel().clickedConnection.clear();
  476. connection.setProtocol(null);
  477. deleteConnection(connection);
  478. controller.notifyObservers();
  479. return newCon;
  480. }
  481. /**
  482. * Deletes the network model, removes all Devices, Connections and Links
  483. */
  484. public void deleteNetworkModel(){
  485. /**
  486. * Devices which should be deleted
  487. */
  488. LinkedList<SmartDevice> devicesToDelete = new LinkedList<SmartDevice>(model.getDevices());
  489. for(SmartDevice d: devicesToDelete)
  490. deleteSmartDevice(d);
  491. devicesToDelete.clear();
  492. /**
  493. * Connections which should be deleted
  494. */
  495. LinkedList<Connection> connectionsToDelete = new LinkedList<Connection>(model.getConnections());
  496. for(Connection c: connectionsToDelete)
  497. deleteConnection(c);
  498. connectionsToDelete.clear();
  499. /**
  500. * Links which should be deleted
  501. */
  502. LinkedList<Link> linksToDelete = new LinkedList<Link>(model.getConnectionNetworks());
  503. for(Link l: model.getConnectionNetworks())
  504. deleteLink(l);
  505. linksToDelete.clear();
  506. /**
  507. * Delete Collectors
  508. */
  509. LinkedList<PacketCollector> collectors = new LinkedList<PacketCollector>(captureController.getPacketCollectors());
  510. for(PacketCollector p:collectors){
  511. captureController.removePacketCollector(p);
  512. }
  513. /**
  514. * Clear event queue
  515. */
  516. /**
  517. * Update the GUI
  518. */
  519. controller.notifyObservers();
  520. }
  521. /**
  522. * Deletes the Connection c and all references
  523. * @param c Connection to be deleted
  524. */
  525. public void deleteConnection(Connection c) {
  526. if(c == null)
  527. return;
  528. c.setName("Deleted");
  529. LinkedList<Port> ports = new LinkedList<Port>(c.getParticipants());
  530. for(Port p:ports)
  531. removeDeviceFromConnectionAndProtocol(p, c);
  532. ports.clear();
  533. removeConnectionFromLink(c, c.getLink());
  534. c.setStatus(Connection.TERMINATED);
  535. networkTreeSettings.removeStatusOfObject(c);
  536. removeConnection(c);
  537. }
  538. /**
  539. * Deletes Link 'toDelete' and all references
  540. * @param toDelete Link to be deleted
  541. */
  542. public void deleteLink(Link toDelete) {
  543. if(toDelete==null)return;
  544. LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>(toDelete.getDevices());
  545. for(SmartDevice d : devices)
  546. removeLinkFromDevice(toDelete, d);
  547. devices.clear();
  548. LinkedList<Connection> connections = new LinkedList<Connection>(toDelete.getConnections());
  549. for(Connection c:connections)
  550. removeConnectionFromLink(c,toDelete);
  551. connections.clear();
  552. toDelete.getPackets().clear();
  553. /**
  554. * Remove Link Color
  555. */
  556. controller.getSettingsController().getLinkColors().removeLink(toDelete);
  557. networkTreeSettings.removeStatusOfObject(toDelete);
  558. /**
  559. * Remove from Collectors
  560. */
  561. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  562. for(PacketCollector collector:captureController.getPacketCollectors()){
  563. if(collector.getLinks().contains(toDelete)){
  564. captureController.removeLinkFromCollector(collector, toDelete);
  565. }
  566. }
  567. /**
  568. * Remove Link from model
  569. */
  570. removeLink(toDelete);
  571. }
  572. /**
  573. * Removes Connection from Link
  574. * @param c Connection to be removed
  575. * @param l Link to be removed
  576. */
  577. public void removeConnectionFromLink(Connection c, Link l) {
  578. if(c!=null && c.getLink()==l){
  579. c.setLink(null);
  580. }
  581. if(l!=null){
  582. l.removeConnection(c);
  583. }
  584. }
  585. /**
  586. * Changes the type of the link to new type, specified by the given Link-class
  587. * @param oldLink oldLink, whose attributes will b copied to the new Link
  588. * @param newType Type/Class of the new Link
  589. * @return newly created Link, null on failure/error
  590. */
  591. public Link changeLinkType(Link oldLink, Class<? extends Link> newType) {
  592. if(newType == null)
  593. return null;
  594. /**
  595. * New Link which was created
  596. */
  597. Link newLink = null;
  598. try{
  599. newLink = newType.newInstance();
  600. }catch(Exception e){
  601. return null;
  602. }
  603. if (newLink == null || !(newLink instanceof Link)) {
  604. return null;
  605. }else {
  606. //Update Link Color
  607. LinkColorController linkColor = controller.getSettingsController().getLinkColors();
  608. linkColor.setColorOfLink(newLink, linkColor.getColorOfLink(oldLink).getRight());
  609. copyNetworkTreeStatus(oldLink, newLink);
  610. //Update Collectors
  611. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  612. for(PacketCollector collector:captureController.getPacketCollectors()){
  613. if(collector.getLinks().contains(oldLink)){
  614. captureController.removeLinkFromCollector(collector, oldLink);
  615. captureController.addLinkToCollector(collector, newLink);
  616. }
  617. }
  618. // Set old Name
  619. newLink.setName(oldLink.getName());
  620. // Add to Model
  621. if(getLinks().contains(oldLink)){
  622. removeLink(oldLink);
  623. addLink(newLink);
  624. }
  625. //Connection to the new Link
  626. for(Connection c: new LinkedList<Connection>(oldLink.getConnections())){
  627. addConnectionToLink(c, newLink);
  628. }
  629. // Add devices to the new Link
  630. LinkedList<SmartDevice> devices= new LinkedList<>(oldLink.getDevices());
  631. for(SmartDevice device:devices){
  632. removeLinkFromDevice(oldLink, device);
  633. addLinkToDevice(newLink, device);
  634. }
  635. return newLink;
  636. }
  637. }
  638. /**
  639. * Adds status of the old Object to the new Object
  640. * @param oldObject old object, whose status should be copied
  641. * @param newObject new object, which should get the new status
  642. */
  643. private void copyNetworkTreeStatus(Object oldObject, Object newObject){
  644. NetworkTreeNodeStatus oldStatus = networkTreeSettings.getStatusOfObject(oldObject);
  645. NetworkTreeNodeStatus newStatus = new NetworkTreeNodeStatus(newObject);
  646. newStatus.setExpanded(oldStatus.isExpanded());
  647. newStatus.setVisible(oldStatus.isVisible());
  648. networkTreeSettings.addStatusOfObject(newObject, newStatus);
  649. }
  650. /**
  651. * Changes the Type of the SmartDevice
  652. * @param old old Device which should be
  653. * @param newClass new Class of the SmartDevice
  654. * @return new SmartDevice, null on failure
  655. */
  656. public SmartDevice changeDeviceType(SmartDevice old, Class<? extends SmartDevice> newClass){
  657. //Compile new SmartDevice
  658. if(newClass == null)
  659. return null;
  660. /**
  661. * New Link which was created
  662. */
  663. SmartDevice newDevice = null;
  664. try{
  665. newDevice = newClass.newInstance();
  666. }catch(Exception e){
  667. return null;
  668. }
  669. if (newDevice == null || !(newDevice instanceof SmartDevice)) {
  670. return null;
  671. }else {
  672. // Update base informations
  673. newDevice.setName(old.getName());
  674. old.setName("Deleted");
  675. newDevice.setX(old.getX());
  676. newDevice.setY(old.getY());
  677. newDevice.setZ(old.getZ());
  678. //Update Packet Collectors
  679. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  680. for(PacketCollector collector:captureController.getPacketCollectors()){
  681. if(collector.getDevices().contains(old)){
  682. captureController.removeDeviceFromCollector(collector, newDevice);
  683. captureController.addDeviceToCollector(collector, newDevice);
  684. }
  685. }
  686. //Update all references
  687. LinkedList<Link> links = new LinkedList<Link>(old.getLinks());
  688. for(Link l: links){
  689. addLinkToDevice(l, newDevice);
  690. removeLinkFromDevice(l, old);
  691. }
  692. LinkedList<Port> ports = new LinkedList<Port>(old.getPorts());
  693. for(Port p:ports){
  694. p.setOwner(newDevice);
  695. newDevice.addPort(p);
  696. old.removePort(p);
  697. }
  698. SelectionModel selectionModel = controller.getSettingsController().getConfigurationManager().getSelectionModel();
  699. if(selectionModel.selectedDevices.contains(old)){
  700. selectionModel.selectedDevices.add(newDevice);
  701. selectionModel.selectedDevices.remove(old);
  702. }
  703. if(selectionModel.selectedDevicesDrag.contains(old)){
  704. selectionModel.selectedDevicesDrag.add(newDevice);
  705. selectionModel.selectedDevicesDrag.remove(old);
  706. }
  707. //Update Colors tree status ?
  708. copyNetworkTreeStatus(old, newDevice);
  709. //Remove and add Device
  710. deleteSmartDevice(old);
  711. addSmartDevice(newDevice);
  712. return newDevice;
  713. }
  714. }
  715. }