NetworkController.java 25 KB

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