38
|
1 package christie.topology.manager;
|
|
2
|
|
3 import christie.codegear.CodeGear;
|
|
4 import christie.codegear.CodeGearManager;
|
|
5
|
41
|
6 import java.io.File;
|
|
7 import java.io.FileNotFoundException;
|
|
8 import java.io.FileReader;
|
|
9 import java.util.ArrayList;
|
|
10 import java.util.HashMap;
|
|
11 import java.util.LinkedList;
|
|
12
|
45
|
13 import christie.topology.HostMessage;
|
41
|
14 import org.apache.log4j.Logger;
|
|
15
|
|
16 import com.alexmerz.graphviz.ParseException;
|
|
17 import com.alexmerz.graphviz.Parser;
|
|
18 import com.alexmerz.graphviz.objects.*;
|
|
19
|
38
|
20 public class TopologyManager extends CodeGear {
|
|
21
|
45
|
22 private TopologyManagerConfig conf;
|
|
23 private Logger logger = Logger.getLogger(TopologyManager.class);
|
41
|
24
|
45
|
25 public TopologyManager(TopologyManagerConfig conf) {
|
|
26 this.conf = conf;
|
|
27 }
|
41
|
28
|
38
|
29 @Override
|
|
30 protected void run(CodeGearManager cgm) {
|
41
|
31 cgm.setup(new CheckComingHost());
|
38
|
32
|
45
|
33 getLocalDGM().put("absCookieTable", new HashMap<String, String>());
|
|
34 getLocalDGM().put("config", conf );
|
|
35
|
41
|
36 // if (!conf.dynamic) は, conf.dynamic = trueの動作がわからないので, 省いた.
|
|
37
|
45
|
38
|
|
39 LinkedList<String> nodeNames = new LinkedList<>();
|
|
40 HashMap<String, LinkedList<NodeInfo>> topology = new HashMap<>();
|
41
|
41 int nodeNum = 0;
|
|
42
|
|
43 try {
|
45
|
44 FileReader reader = new FileReader(new File(conf.confFilePath));
|
41
|
45 Parser parser = new Parser();
|
|
46 parser.parse(reader);
|
|
47
|
|
48
|
|
49 ArrayList<Graph> digraphs = parser.getGraphs();
|
|
50
|
|
51
|
|
52 for (Graph digraph : digraphs) {
|
|
53 ArrayList<Node> nodes = digraph.getNodes(false);
|
|
54 nodeNum = nodes.size();
|
|
55
|
|
56 for (Node node : nodes) {
|
|
57 String nodeName = node.getId().getId();
|
|
58 nodeNames.add(nodeName);
|
45
|
59 topology.put(nodeName, new LinkedList<>());
|
41
|
60 }
|
|
61
|
|
62 ArrayList<Edge> edges = digraph.getEdges();
|
45
|
63 HashMap<String, NodeInfo> hash = new HashMap<>();
|
41
|
64
|
|
65 String connection;
|
|
66 String source;
|
|
67 String target;
|
|
68
|
|
69 NodeInfo nodeInfo;
|
|
70
|
|
71 // まず1回グラフを読み込む
|
|
72 for (Edge edge : edges) {
|
|
73 connection = edge.getAttribute("label");
|
|
74 source = edge.getSource().getNode().getId().getId();
|
|
75 target = edge.getTarget().getNode().getId().getId();
|
|
76 nodeInfo = new NodeInfo(source, connection);
|
|
77
|
|
78 //Question: この下の2行はどんな役目?
|
|
79 //LinkedList<NodeInfo> sources = topology.get(target);
|
|
80 //sources.add(nodeInfo); // addしてその後の使い道は...?
|
|
81
|
|
82 hash.put(source + "," + target, nodeInfo);
|
|
83 }
|
|
84
|
|
85 // hash.get(target + "," + source); をして, グラフを逆にたどって, reverseNameにlabelを入れてる.
|
|
86 for (Edge edge : edges) {
|
|
87 connection = edge.getAttribute("label");
|
|
88 source = edge.getSource().getNode().getId().getId();
|
|
89 target = edge.getTarget().getNode().getId().getId();
|
|
90 nodeInfo = hash.get(target + "," + source);
|
|
91
|
|
92 if (nodeInfo != null) {
|
|
93 nodeInfo.reverseName = connection;
|
|
94 }
|
|
95
|
|
96 }
|
|
97 }
|
|
98
|
|
99 } catch (FileNotFoundException e) {
|
45
|
100 logger.error("File not found: " + conf.confFilePath);
|
41
|
101 e.printStackTrace();
|
|
102 } catch (ParseException e) {
|
45
|
103 logger.error("File format error: " + conf.confFilePath);
|
41
|
104 e.printStackTrace();
|
|
105 }
|
|
106
|
|
107 // for recode topology information
|
|
108 // cookie List
|
|
109 getLocalDGM().put("running", false);
|
|
110 getLocalDGM().put("resultParse", topology);
|
|
111 getLocalDGM().put("nodeNames", nodeNames);
|
|
112
|
|
113 cgm.setup(new IncomingHosts());
|
|
114
|
|
115
|
|
116
|
|
117 // Question: この処理何をやっているのかわからない. 一応, その下にそれっぽいコードを書いた.
|
|
118 // ConfigWaiter cs3 = new ConfigWaiter(nodeNum);
|
|
119 // cs3.done.setKey("local", "done");
|
|
120
|
|
121 cgm.setup(new ConfigWaiter());
|
|
122 getLocalDGM().put("nodeNum", nodeNum);
|
45
|
123
|
|
124
|
|
125
|
|
126
|
|
127
|
|
128 getLocalDGM().put("topology", new HashMap<String, LinkedList<HostMessage>>());
|
|
129 getLocalDGM().put("createdList", new LinkedList<String>());
|
|
130 cgm.setup(new CreateHash());
|
|
131
|
|
132 cgm.setup(new TopologyFinish());
|
38
|
133
|
|
134 }
|
|
135
|
41
|
136
|
38
|
137 }
|