NetworkController.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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(link)){
  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. if(!p.getOwner().getPorts().contains(p))
  359. p.getOwner().addPort(p);
  360. return true;
  361. }else {
  362. //Device could not be added -> Remove
  363. if(p.getConnection()==connection)
  364. p.setConnection(null);
  365. if(connection.getParticipants().contains(p))
  366. connection.removeSmartDevice(p);
  367. if(connection.getProtocol().getDevices().contains(p))
  368. connection.getProtocol().removeDevice(p);
  369. return false;
  370. }
  371. }
  372. /**
  373. * Adds Device p to the connection
  374. * @param p Port/Device to be added
  375. * @param connection Connection
  376. */
  377. public void addDeviceToConnection(Port p, Connection connection){
  378. if(p==null || connection == null)
  379. return;
  380. if(!connection.getParticipants().contains(p))
  381. connection.addSmartDevice(p);
  382. p.setConnection(connection);
  383. }
  384. /**
  385. * 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.
  386. * @param connection connection which should be edited
  387. * @param link new link
  388. * @return true on successful change, false on failure and restore
  389. */
  390. public boolean changeLinkOfConnection(Connection connection, Link link) {
  391. if(connection !=null && link != null){
  392. addConnectionToLink(connection, link);
  393. return true;
  394. }else{
  395. return false;
  396. }
  397. }
  398. /**
  399. * Adds the given connection to the link
  400. * @param connection connection to be added
  401. * @param link link the connection should be added to
  402. */
  403. public void addConnectionToLink(Connection connection, Link link) {
  404. if(connection == null || link == null)return;
  405. /**
  406. * Remove connection from Old Link
  407. */
  408. if(connection.getLink()!=null)
  409. removeConnectionFromLink(connection, connection.getLink());
  410. /**
  411. * Add Connection to new Link
  412. */
  413. connection.setLink(link);
  414. if(!link.getConnections().contains(connection))
  415. link.addConnection(connection);
  416. /**
  417. *
  418. */
  419. for(Port p:connection.getParticipants()){
  420. if(p!=null && !link.getDevices().contains(p.getOwner())){
  421. addLinkToDevice(link, p.getOwner());
  422. }
  423. }
  424. }
  425. /**
  426. * Changes the type of the connection to the new Type and updates all references
  427. * @param connection connection to be updated
  428. * @param newConnectionClass class of new type
  429. * @return newly created connection
  430. */
  431. public Connection changeConnectionType(Connection connection, Class<? extends Connection> newConnectionClass) {
  432. Connection newCon = null;
  433. try{
  434. newCon = newConnectionClass.newInstance();
  435. copyNetworkTreeStatus(connection, newCon);
  436. newCon.setProtocol(connection.getProtocol());
  437. newCon.setStatus(connection.getStatus());
  438. newCon.setPacketLossProbability(connection.getPacketLossProbability());
  439. newCon.setName(connection.getName());
  440. if(getConnections().contains(connection)){
  441. removeConnection(connection);
  442. addConnection(newCon);
  443. }
  444. addConnectionToLink(newCon, connection.getLink());
  445. connection.setProtocol(null);
  446. for (Iterator<Port> p = connection.getParticipants().iterator(); p.hasNext();) {
  447. Port type = (Port) p.next();
  448. removeDeviceFromConnection(type, connection);
  449. addDeviceToConnection(type, newCon);
  450. }
  451. }catch(Exception e){
  452. System.out.println("Error while changing protocol: "+e.toString());
  453. /**
  454. * Restore old connection on Failure
  455. */
  456. if(newCon != null){
  457. if(getConnections().contains(newCon)){
  458. removeConnection(newCon);
  459. addConnection(connection);
  460. }
  461. if(newCon.getProtocol()!=null){
  462. connection.setProtocol(newCon.getProtocol());
  463. newCon.setProtocol(null);
  464. }
  465. if(newCon.getLink()!=null)
  466. newCon.getLink().removeConnection(newCon);
  467. newCon.setLink(null);
  468. for (Iterator<Port> p = connection.getParticipants().iterator(); p.hasNext();) {
  469. Port type = (Port) p.next();
  470. if(type.getConnection()!=connection)
  471. type.setConnection(connection);
  472. newCon.removeSmartDevice(type);
  473. }
  474. }
  475. return null;
  476. }
  477. controller.getSettingsController().getConfigurationManager().getSelectionModel().clickedConnection.clear();
  478. connection.setProtocol(null);
  479. deleteConnection(connection);
  480. controller.notifyObservers();
  481. return newCon;
  482. }
  483. /**
  484. * Deletes the network model, removes all Devices, Connections and Links
  485. */
  486. public void deleteNetworkModel(){
  487. /**
  488. * Devices which should be deleted
  489. */
  490. LinkedList<SmartDevice> devicesToDelete = new LinkedList<SmartDevice>(model.getDevices());
  491. for(SmartDevice d: devicesToDelete)
  492. deleteSmartDevice(d);
  493. devicesToDelete.clear();
  494. /**
  495. * Connections which should be deleted
  496. */
  497. LinkedList<Connection> connectionsToDelete = new LinkedList<Connection>(model.getConnections());
  498. for(Connection c: connectionsToDelete)
  499. deleteConnection(c);
  500. connectionsToDelete.clear();
  501. /**
  502. * Links which should be deleted
  503. */
  504. LinkedList<Link> linksToDelete = new LinkedList<Link>(model.getConnectionNetworks());
  505. for(Link l: model.getConnectionNetworks())
  506. deleteLink(l);
  507. linksToDelete.clear();
  508. /**
  509. * Delete Collectors
  510. */
  511. LinkedList<PacketCollector> collectors = new LinkedList<PacketCollector>(captureController.getPacketCollectors());
  512. for(PacketCollector p:collectors){
  513. captureController.removePacketCollector(p);
  514. }
  515. /**
  516. * Clear event queue
  517. */
  518. /**
  519. * Update the GUI
  520. */
  521. controller.notifyObservers();
  522. }
  523. /**
  524. * Deletes the Connection c and all references
  525. * @param c Connection to be deleted
  526. */
  527. public void deleteConnection(Connection c) {
  528. if(c == null)
  529. return;
  530. c.setName("Deleted");
  531. LinkedList<Port> ports = new LinkedList<Port>(c.getParticipants());
  532. for(Port p:ports)
  533. removeDeviceFromConnectionAndProtocol(p, c);
  534. ports.clear();
  535. removeConnectionFromLink(c, c.getLink());
  536. c.setStatus(Connection.TERMINATED);
  537. networkTreeSettings.removeStatusOfObject(c);
  538. removeConnection(c);
  539. }
  540. /**
  541. * Deletes Link 'toDelete' and all references
  542. * @param toDelete Link to be deleted
  543. */
  544. public void deleteLink(Link toDelete) {
  545. if(toDelete==null)return;
  546. LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>(toDelete.getDevices());
  547. for(SmartDevice d : devices)
  548. removeLinkFromDevice(toDelete, d);
  549. devices.clear();
  550. LinkedList<Connection> connections = new LinkedList<Connection>(toDelete.getConnections());
  551. for(Connection c:connections)
  552. removeConnectionFromLink(c,toDelete);
  553. connections.clear();
  554. toDelete.getPackets().clear();
  555. /**
  556. * Remove Link Color
  557. */
  558. controller.getSettingsController().getLinkColors().removeLink(toDelete);
  559. networkTreeSettings.removeStatusOfObject(toDelete);
  560. /**
  561. * Remove from Collectors
  562. */
  563. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  564. for(PacketCollector collector:captureController.getPacketCollectors()){
  565. if(collector.getLinks().contains(toDelete)){
  566. captureController.removeLinkFromCollector(collector, toDelete);
  567. }
  568. }
  569. /**
  570. * Remove Link from model
  571. */
  572. removeLink(toDelete);
  573. }
  574. /**
  575. * Removes Connection from Link
  576. * @param c Connection to be removed
  577. * @param l Link to be removed
  578. */
  579. public void removeConnectionFromLink(Connection c, Link l) {
  580. if(c!=null && c.getLink()==l){
  581. c.setLink(null);
  582. }
  583. if(l!=null){
  584. l.removeConnection(c);
  585. }
  586. }
  587. /**
  588. * Changes the type of the link to new type, specified by the given Link-class
  589. * @param oldLink oldLink, whose attributes will b copied to the new Link
  590. * @param newType Type/Class of the new Link
  591. * @return newly created Link, null on failure/error
  592. */
  593. public Link changeLinkType(Link oldLink, Class<? extends Link> newType) {
  594. if(newType == null)
  595. return null;
  596. /**
  597. * New Link which was created
  598. */
  599. Link newLink = null;
  600. try{
  601. newLink = newType.newInstance();
  602. }catch(Exception e){
  603. return null;
  604. }
  605. if (newLink == null || !(newLink instanceof Link)) {
  606. return null;
  607. }else {
  608. //Update Link Color
  609. LinkColorController linkColor = controller.getSettingsController().getLinkColors();
  610. linkColor.setColorOfLink(newLink, linkColor.getColorOfLink(oldLink).getRight());
  611. copyNetworkTreeStatus(oldLink, newLink);
  612. //Update Collectors
  613. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  614. for(PacketCollector collector:captureController.getPacketCollectors()){
  615. if(collector.getLinks().contains(oldLink)){
  616. captureController.removeLinkFromCollector(collector, oldLink);
  617. captureController.addLinkToCollector(collector, newLink);
  618. }
  619. }
  620. // Set old Name
  621. newLink.setName(oldLink.getName());
  622. // Add to Model
  623. if(getLinks().contains(oldLink)){
  624. removeLink(oldLink);
  625. addLink(newLink);
  626. }
  627. //Connection to the new Link
  628. for(Connection c: new LinkedList<Connection>(oldLink.getConnections())){
  629. addConnectionToLink(c, newLink);
  630. }
  631. // Add devices to the new Link
  632. LinkedList<SmartDevice> devices= new LinkedList<>(oldLink.getDevices());
  633. for(SmartDevice device:devices){
  634. removeLinkFromDevice(oldLink, device);
  635. addLinkToDevice(newLink, device);
  636. }
  637. return newLink;
  638. }
  639. }
  640. /**
  641. * Adds status of the old Object to the new Object
  642. * @param oldObject old object, whose status should be copied
  643. * @param newObject new object, which should get the new status
  644. */
  645. private void copyNetworkTreeStatus(Object oldObject, Object newObject){
  646. NetworkTreeNodeStatus oldStatus = networkTreeSettings.getStatusOfObject(oldObject);
  647. NetworkTreeNodeStatus newStatus = new NetworkTreeNodeStatus(newObject);
  648. newStatus.setExpanded(oldStatus.isExpanded());
  649. newStatus.setVisible(oldStatus.isVisible());
  650. networkTreeSettings.addStatusOfObject(newObject, newStatus);
  651. }
  652. /**
  653. * Changes the Type of the SmartDevice
  654. * @param old old Device which should be
  655. * @param newClass new Class of the SmartDevice
  656. * @return new SmartDevice, null on failure
  657. */
  658. public SmartDevice changeDeviceType(SmartDevice old, Class<? extends SmartDevice> newClass){
  659. //Compile new SmartDevice
  660. if(newClass == null)
  661. return null;
  662. /**
  663. * New Link which was created
  664. */
  665. SmartDevice newDevice = null;
  666. try{
  667. newDevice = newClass.newInstance();
  668. }catch(Exception e){
  669. return null;
  670. }
  671. if (newDevice == null || !(newDevice instanceof SmartDevice)) {
  672. return null;
  673. }else {
  674. // Update base informations
  675. newDevice.setName(old.getName());
  676. old.setName("Deleted");
  677. newDevice.setX(old.getX());
  678. newDevice.setY(old.getY());
  679. newDevice.setZ(old.getZ());
  680. //Update Packet Collectors
  681. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  682. for(PacketCollector collector:captureController.getPacketCollectors()){
  683. if(collector.getDevices().contains(old)){
  684. captureController.removeDeviceFromCollector(collector, newDevice);
  685. captureController.addDeviceToCollector(collector, newDevice);
  686. }
  687. }
  688. //Update all references
  689. LinkedList<Link> links = new LinkedList<Link>(old.getLinks());
  690. for(Link l: links){
  691. addLinkToDevice(l, newDevice);
  692. removeLinkFromDevice(l, old);
  693. }
  694. LinkedList<Port> ports = new LinkedList<Port>(old.getPorts());
  695. for(Port p:ports){
  696. p.setOwner(newDevice);
  697. newDevice.addPort(p);
  698. old.removePort(p);
  699. }
  700. SelectionModel selectionModel = controller.getSettingsController().getConfigurationManager().getSelectionModel();
  701. if(selectionModel.selectedDevices.contains(old)){
  702. selectionModel.selectedDevices.add(newDevice);
  703. selectionModel.selectedDevices.remove(old);
  704. }
  705. if(selectionModel.selectedDevicesDrag.contains(old)){
  706. selectionModel.selectedDevicesDrag.add(newDevice);
  707. selectionModel.selectedDevicesDrag.remove(old);
  708. }
  709. //Update Colors tree status ?
  710. copyNetworkTreeStatus(old, newDevice);
  711. //Remove and add Device
  712. deleteSmartDevice(old);
  713. addSmartDevice(newDevice);
  714. return newDevice;
  715. }
  716. }
  717. }