Skip to contentMethod: adapter(NetworkAdapter)
      1: package networkconfigurator.actions;
2: 
3: import java.io.BufferedReader;
4: import java.io.FileReader;
5: import java.util.ArrayList;
6: import java.util.Iterator;
7: import java.util.List;
8: import java.util.Map;
9: 
10: import com.thoughtworks.xstream.XStream;
11: 
12: import model.RouterConfiguration;
13: import model.assignment.VariableAssignment;
14: import networkconfigurator.CompareVaNameVisitor;
15: import networkconfigurator.ConfigurationGraph;
16: import networkconfigurator.DetailBarLabel;
17: import networkconfigurator.DetailBarPropertyEntry;
18: import networkconfigurator.DetailInputBox;
19: import networkconfigurator.LoadConfigurationGraphException;
20: import networkconfigurator.item.ConfigRouter;
21: import networkconfigurator.item.HardwareEdge;
22: import networkconfigurator.item.Host;
23: import networkconfigurator.item.Internet;
24: import networkconfigurator.item.Item;
25: import networkconfigurator.item.ItemVisitor;
26: import networkconfigurator.item.NetworkAdapter;
27: import networkconfigurator.item.NetworkCable;
28: import networkconfigurator.item.Router;
29: import networkconfigurator.item.Slot;
30: import networkconfigurator.item.Switch;
31: 
32: /**
33:  * Load the additional informations of the nodes.
34:  * 
35:  * @author Erik Arand
36:  *
37:  */
38: public class LoadTask {
39:         /**
40:          * The Graph.
41:          */
42:         private final ConfigurationGraph graph;
43: 
44:         /**
45:          * 
46:          * @param configGraph
47:          *            the graph.
48:          */
49:         public LoadTask(final ConfigurationGraph configGraph) {
50:                 graph = configGraph;
51:         }
52: 
53:         /**
54:          * method for loading the additional information of the nodes.
55:          * 
56:          * @param rc
57:          *            the router configuration to get the path where the configuration is located. For
58:          *            example, is as follows: "C:\Users\admin\Documents\example-3.10.15\config" and the
59:          *            Assignments.
60:          * @throws LoadConfigurationGraphException
61:          *             the error is thrown if a problem occurs during loading.
62:          * 
63:          */
64:         public void loadGraphConfiguration(final RouterConfiguration rc)
65:                         throws LoadConfigurationGraphException {
66:                 final String configPath = rc.getPath();
67:                 final Map<String, VariableAssignment> loadConfig = rc.getAllAssignments();
68: 
69:                 // Create ConfigRouter
70:                 createConfigRouter(loadConfig);
71: 
72:                 // Create Route
73:                 createRoutes(loadConfig);
74: 
75:                 // Create Hosts
76:                 createHosts(loadConfig);
77: 
78:                 // Create NetworkAdapter
79:                 createNetworkAdapter(loadConfig);
80: 
81:                 final String configFile = configPath + System.getProperty("file.separator") + "graph.xml";
82:                 final String rawXML = getFileContent(configFile);
83:                 final XStream xStream = new XStream();
84: 
85:                 try {
86:                         @SuppressWarnings("unchecked")
87:                         final ArrayList<Item> xmlList = (ArrayList<Item>) xStream.fromXML(rawXML);
88:                         final List<Item> fli4lList = graph.getItems();
89:                         fillList(fli4lList, xmlList);
90:                 } catch (final Exception e) {
91:                         // TODO Create a new File
92:                         // throw new LoadNetworkConfiguratorException();
93:                 }
94: 
95:                 this.graph.repaint();
96:         }
97: 
98:         /**
99:          * create the config router.
100:          * 
101:          * @param loadConfig
102:          *            the loadConfig contain the values to be added.
103:          * @throws LoadConfigurationGraphException
104:          *             the error is thrown if a problem occurs during loading.
105:          */
106:         private void createConfigRouter(final Map<String, VariableAssignment> loadConfig)
107:                         throws LoadConfigurationGraphException {
108:                 try {
109:                         final ConfigRouter configRouter =
110:                                         new ConfigRouter(this.graph.getMousePt().getLocation(),
111:                                                         loadConfig.get(NcActionsConstants.HOSTNAME));
112:                         this.graph.setTheConfigRouter(configRouter);
113:                         this.graph.getItems().add(configRouter);
114:                 } catch (final NullPointerException e) {
115:                         throw new LoadConfigurationGraphException(
116:                                         NcActionsConstants.THE_VARIABLE_HOSTNAME_IS_UNDEFINED);
117:                 }
118:         }
119: 
120:         /**
121:          * create the routes.
122:          * 
123:          * @param loadConfig
124:          *            the loadConfig contain the values to be added.
125:          * @throws LoadConfigurationGraphException
126:          *             the error is thrown if a problem occurs during loading.
127:          */
128:         private void createRoutes(final Map<String, VariableAssignment> loadConfig)
129:                         throws LoadConfigurationGraphException {
130:                 try {
131:                         final int n = numberOfVariableAssignment(loadConfig, NcActionsConstants.IP_ROUTE_N);
132:                         for (int i = 1; i <= n; i++) {
133:                                 final VariableAssignment route =
134:                                                 loadConfig.get(String.format(NcActionsConstants.IP_ROUTE_D, i));
135:                                 this.graph.getTheConfigRouter().addRoute(route);
136:                         }
137:                 } catch (final NullPointerException e) {
138:                         throw new LoadConfigurationGraphException(
139:                                         NcActionsConstants.THE_VARIABLE_IP_ROUTE_N_FOR_THE_N_IS_UNDEFINED);
140:                 }
141:         }
142: 
143:         /**
144:          * create the hosts.
145:          * 
146:          * @param loadConfig
147:          *            the loadConfig contain the values to be added.
148:          * @throws LoadConfigurationGraphException
149:          *             the error is thrown if a problem occurs during loading.
150:          */
151:         private void createHosts(final Map<String, VariableAssignment> loadConfig)
152:                         throws LoadConfigurationGraphException {
153:                 try {
154:                         final int n = numberOfVariableAssignment(loadConfig, NcActionsConstants.HOST_N);
155:                         for (int i = 1; i <= n; i++) {
156:                                 final VariableAssignment hostName =
157:                                                 loadConfig.get(String.format(NcActionsConstants.HOST_D_NAME, i));
158:                                 final Host host = new Host(this.graph.getMousePt().getLocation(), hostName);
159:                                 this.graph.getItems().add(host);
160:                         }
161:                 } catch (final NullPointerException e) {
162:                         throw new LoadConfigurationGraphException(
163:                                         NcActionsConstants.THE_VARIABLE_HOST_N_NAME_FOR_THE_N_IS_UNDEFINED);
164:                 }
165:         }
166: 
167:         /**
168:          * create the network adapter.
169:          * 
170:          * @param loadConfig
171:          *            the loadConfig contain the values to be added.
172:          * @throws LoadConfigurationGraphException
173:          *             the error is thrown if a problem occurs during loading.
174:          */
175:         private void createNetworkAdapter(final Map<String, VariableAssignment> loadConfig)
176:                         throws LoadConfigurationGraphException {
177:                 try {
178:                         final int n = numberOfVariableAssignment(loadConfig, NcActionsConstants.IP_NET_N);
179:                         for (int i = 1; i <= n; i++) {
180:                                 final VariableAssignment networkAdapterName =
181:                                                 loadConfig.get(String.format(NcActionsConstants.IP_NET_D_DEV, i));
182:                                 final VariableAssignment networkAdapterIPAdresse =
183:                                                 loadConfig.get(String.format(NcActionsConstants.IP_NET_D, i));
184:                                 final NetworkAdapter networkAdapter = new NetworkAdapter(
185:                                                 this.graph.getMousePt().getLocation(), this.graph.getTheConfigRouter(),
186:                                                 networkAdapterName, networkAdapterIPAdresse);
187:                                 this.graph.getItems().add(networkAdapter);
188:                                 this.graph.getItems()
189:                                                 .add(new HardwareEdge(this.graph.getTheConfigRouter(), networkAdapter));
190:                         }
191:                 } catch (final NullPointerException e) {
192:                         throw new LoadConfigurationGraphException(
193:                                         NcActionsConstants.THE_VARIABLE_IP_NET_N_OR_IP_NET_N_DEV_FOR_THE_N_IS_UNDEFINED);
194:                 }
195:         }
196: 
197:         /**
198:          * Determine the number of variable assignments. If no value is set, the value 0 is returned.
199:          * 
200:          * @param loadConfig
201:          *            Determines the number based on this configuration.
202:          * @param variableAssignment
203:          *            The name of the variable assignment to determines the number.
204:          * @return the number of hosts.
205:          */
206:         private int numberOfVariableAssignment(final Map<String, VariableAssignment> loadConfig,
207:                         final String variableAssignment) {
208:                 try {
209:                         final VariableAssignment variableAssignmentN = loadConfig.get(variableAssignment);
210:                         return Integer.parseInt(variableAssignmentN.getValue());
211:                 } catch (final NullPointerException e) {
212:                         return 0;
213:                 }
214:         }
215: 
216:         /**
217:          * fills the list with the needed information next to the information given by the fli4l-config.
218:          * 
219:          * @param fli4lList
220:          *            the list of items
221:          * @param xmlList
222:          *            the xml-list of items
223:          */
224:         private void fillList(final List<Item> fli4lList, final ArrayList<Item> xmlList) {
225:                 for (Item item : xmlList) {
226:                         item.accept(new FindItemAndUpdatePoint(fli4lList));
227:                 }
228:         }
229: 
230:         /**
231:          * method for loading the xml file.
232:          * 
233:          * @param configPath
234:          *            location of the xml file
235:          * @return string of the xml file.
236:          */
237:         public static String getFileContent(final String configPath) {
238:                 final StringBuilder sb = new StringBuilder();
239: 
240:                 try {
241:                         final BufferedReader reader = new BufferedReader(new FileReader(configPath));
242:                         final char[] readerContent = new char[1];
243:                         while (reader.read(readerContent) != -1) {
244:                                 sb.append(readerContent[0]);
245:                         }
246:                         reader.close();
247:                 } catch (final Exception ex) {
248:                         return null;
249:                 }
250: 
251:                 return sb.toString();
252:         }
253: 
254:         /**
255:          * class for finding an item in a list and updating the point.
256:          * 
257:          * @author admin
258:          *
259:          */
260:         private final class FindItemAndUpdatePoint implements ItemVisitor {
261:                 /**
262:                  * the list of items.
263:                  */
264:                 private final List<Item> fli4lList;
265: 
266:                 /**
267:                  * the constructor.
268:                  * 
269:                  * @param fli4lList
270:                  *            the list of items.
271:                  */
272:                 private FindItemAndUpdatePoint(final List<Item> fli4lList) {
273:                         this.fli4lList = fli4lList;
274:                 }
275: 
276:                 @Override
277:                 public void slot(final Slot slot) {
278:                         graph.getItems().add(slot);
279:                 }
280: 
281:                 @Override
282:                 public void sWitch(final Switch sWitch) {
283:                         graph.getItems().add(sWitch);
284:                 }
285: 
286:                 @Override
287:                 public void router(final Router router) {
288:                         graph.getItems().add(router);
289:                 }
290: 
291:                 @Override
292:                 public void networkCable(final NetworkCable edge) {
293:                 }
294: 
295:                 @Override
296:                 public void internet(final Internet internet) {
297:                         graph.getItems().add(internet);
298:                 }
299: 
300:                 @Override
301:                 public void host(final Host host) {
302:                         findItemAndUpdatePoint(fli4lList, host);
303:                 }
304: 
305:                 @Override
306:                 public void hardwareEdge(final HardwareEdge edge) {
307: 
308:                 }
309: 
310:                 @Override
311:                 public void configRouter(final ConfigRouter router) {
312:                         findItemAndUpdatePoint(fli4lList, router);
313:                 }
314: 
315:                 @Override
316:                 public void adapter(final NetworkAdapter adapter) {
317:                         findItemAndUpdatePoint(fli4lList, adapter);
318:                 }
319: 
320:                 /**
321:                  * find an item in a list and update point.
322:                  * 
323:                  * @param fli4lItemList
324:                  *            the list
325:                  * @param item
326:                  *            the item
327:                  */
328:                 private void findItemAndUpdatePoint(final List<Item> fli4lItemList, final Item item) {
329:                         final Iterator<DetailBarPropertyEntry> iterator = item.getPropertyBar().iterator();
330:                         while (iterator.hasNext()) {
331:                                 iterator.next().getPropertyBarEntry().accept(new CompareVaNameVisitor() {
332:                                         @Override
333:                                         public boolean visit(final DetailBarLabel label) {
334:                                                 return false;
335:                                         }
336: 
337:                                         @Override
338:                                         public boolean visit(final VariableAssignment va) {
339:                                                 final Iterator<Item> iIterator = fli4lItemList.iterator();
340:                                                 while (iIterator.hasNext()) {
341:                                                         final Item fli4lItem = iIterator.next();
342:                                                         if (fli4lItem.containsVaByIdentifier(va)) {
343:                                                                 fli4lItem.setPoint(item.getPoint());
344:                                                                 fli4lItem.setBoundary();
345:                                                         }
346:                                                 }
347:                                                 return false;
348:                                         }
349: 
350:                                         @Override
351:                                         public boolean visit(final DetailInputBox detailBarInputBox) {
352:                                                 return false;
353:                                         }
354:                                 });
355: 
356:                         }
357:                 }
358:         }
359: 
360: }