401
|
1 package alice.topology.manager;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.FileNotFoundException;
|
|
5 import java.io.FileReader;
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.HashMap;
|
|
8 import java.util.LinkedList;
|
|
9
|
|
10 import org.apache.log4j.Logger;
|
|
11
|
|
12 import alice.codesegment.CodeSegment;
|
|
13
|
|
14 import com.alexmerz.graphviz.ParseException;
|
|
15 import com.alexmerz.graphviz.Parser;
|
|
16 import com.alexmerz.graphviz.objects.Edge;
|
|
17 import com.alexmerz.graphviz.objects.Graph;
|
|
18 import com.alexmerz.graphviz.objects.Node;
|
|
19
|
|
20 public class StartTopologyManager extends CodeSegment {
|
|
21
|
|
22 TopologyManagerConfig conf;
|
|
23 Logger logger = Logger.getLogger(StartTopologyManager.class);
|
|
24
|
|
25 public StartTopologyManager(TopologyManagerConfig conf) {
|
|
26 this.conf = conf;
|
|
27 }
|
|
28
|
|
29 @Override
|
|
30 public void run() {
|
|
31 LinkedList<String> nodeNames = new LinkedList<String>();
|
|
32 HashMap<String, LinkedList<NodeInfo>> topology = new HashMap<String, LinkedList<NodeInfo>>();
|
|
33 int nodeNum = 0;
|
|
34 try {
|
|
35 FileReader reader = new FileReader(new File(conf.confFilePath));
|
|
36 Parser parser = new Parser();
|
|
37 parser.parse(reader);
|
|
38 ArrayList<Graph> graphs = parser.getGraphs();
|
|
39 for (Graph graph : graphs) {
|
|
40 ArrayList<Node> nodes = graph.getNodes(false);
|
|
41 nodeNum = nodes.size();
|
|
42 for (Node node : nodes) {
|
|
43 String nodeName = node.getId().getId();
|
|
44 nodeNames.add(nodeName);
|
|
45 topology.put(nodeName, new LinkedList<NodeInfo>());
|
|
46 }
|
|
47 ArrayList<Edge> edges = graph.getEdges();
|
|
48 HashMap<String, NodeInfo> hash = new HashMap<String, NodeInfo>();
|
|
49 for (Edge edge : edges) {
|
|
50 String connection = edge.getAttribute("label");
|
|
51 String source = edge.getSource().getNode().getId().getId();
|
|
52 String target = edge.getTarget().getNode().getId().getId();
|
|
53 LinkedList<NodeInfo> sources = topology.get(target);
|
|
54 NodeInfo nodeInfo = new NodeInfo(source, connection);
|
|
55 sources.add(nodeInfo);
|
|
56 hash.put(source + "," + target, nodeInfo);
|
|
57 }
|
|
58 for (Edge edge : edges) {
|
|
59 String connection = edge.getAttribute("label");
|
|
60 String source = edge.getSource().getNode().getId().getId();
|
|
61 String target = edge.getTarget().getNode().getId().getId();
|
|
62 NodeInfo nodeInfo = hash.get(target + "," + source);
|
|
63 if (nodeInfo != null) {
|
|
64 nodeInfo.reverseName = connection;
|
|
65 }
|
|
66 }
|
|
67 }
|
|
68
|
|
69 } catch (FileNotFoundException e) {
|
|
70 logger.error("File not found: " + conf.confFilePath);
|
|
71 e.printStackTrace();
|
|
72 } catch (ParseException e) {
|
|
73 logger.error("File format error: " + conf.confFilePath);
|
|
74 e.printStackTrace();
|
|
75 }
|
|
76
|
|
77 IncomingHosts cs1 = new IncomingHosts(topology, nodeNames);
|
|
78 cs1.host.setKey("local", "host");
|
|
79
|
|
80 TopologyFinish cs2 = new TopologyFinish();
|
|
81 cs2.finish.setKey("local", "finish");
|
|
82
|
|
83 ConfigWaiter cs3 = new ConfigWaiter(nodeNum);
|
|
84 cs3.done.setKey("local", "done");
|
|
85 }
|
|
86
|
|
87 }
|