Add INFO.yaml for nemo
[nemo.git] / nemo-tools / sandbox / src / main / java / org / opendaylight / nemo / tool / sandbox / models / Network.java
1 /*
2  * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.nemo.tool.sandbox.models;
10
11 import org.json.JSONArray;
12 import org.json.JSONException;
13 import org.json.JSONObject;
14 import org.opendaylight.nemo.tool.sandbox.CmdExecutor;
15 import org.opendaylight.nemo.tool.sandbox.utils.Config;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 /**
24  * Created by hj on 12/9/15.
25  */
26 public class Network {
27     private static Logger log = LoggerFactory.getLogger(Network.class);
28     private Map<String/*name*/, Host> hostMap;
29     private Map<String/*name*/, Switch> switchMap;
30     private Map<Connector/*External connector*/, Connector/*Connector on host*/> externalConnectorMap;
31     private List<Link> internalLinks;
32     private List<Link> links;
33
34     public Network() {
35         hostMap = new HashMap<String, Host>();
36         switchMap = new HashMap<String, Switch>();
37         links = new ArrayList<Link>();
38
39         externalConnectorMap = new HashMap<Connector, Connector>();
40         internalLinks = new ArrayList<Link>();
41     }
42
43     public void addHost(Host host) {
44         hostMap.put(host.getName(), host);
45     }
46
47     public void addSwitch(Switch sw) {
48         switchMap.put(sw.getName(), sw);
49     }
50
51     public void addLink(Link link) {
52         links.add(link);
53     }
54
55     public String execute(String name, String command) {
56         Host host = hostMap.get(name);
57         if (host != null) {
58             if (CmdExecutor.open()) {
59                 String result = CmdExecutor.sshExecute("ip netns exec " + name + " " + command);
60                 CmdExecutor.close();
61                 return result;
62             } else {
63                 return "Can not open ssh right now,please try again.";
64             }
65         }
66         return name + " " + "is not a firewall,can not execute " + command;
67     }
68
69     public void install() {
70         traversal();
71
72         for (Link link : links) {
73             link.install();
74         }
75
76         for (Node node : switchMap.values()) {
77             node.install();
78         }
79
80         for (Node node : hostMap.values()) {
81             node.install();
82         }
83     }
84
85     public void uninstall() {
86         for (Link link : links) {
87             link.uninstall();
88         }
89
90         pKill();
91
92         for (Host host : hostMap.values()) {
93             host.uninstall();
94         }
95     }
96
97     private void pKill() {
98         try {
99             CmdExecutor.sshExecute("pkill -9 ofdatapath");
100             CmdExecutor.sshExecute("pkill -9 ofprotocol");
101
102             CmdExecutor.sshExecute("pkill -9 fail-ofdatapath");
103             CmdExecutor.sshExecute("pkill -9 fail-ofprotocol");
104
105             CmdExecutor.sshExecute("pkill -9 ext-ofdatapath");
106             CmdExecutor.sshExecute("pkill -9 ext-ofprotocol");
107         } catch (Exception e) {
108
109         }
110     }
111
112     public void echoConfig() {
113         CmdExecutor.sshExecute("mkdir -p " + Config.getConfigPath());
114
115         String hosts = hostJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
116         String hostsPath = Config.getConfigPath() + "/" + Config.getConfigHostsFileName();
117         CmdExecutor.sshExecute("echo " + hosts + " > " + hostsPath);
118 //        FileUtils.write(hostsPath, hosts);
119
120         String externals = externalJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
121         String externalsPath = Config.getConfigPath() + "/" + Config.getConfigExternalsFileName();
122         CmdExecutor.sshExecute("echo " + externals + " > " + externalsPath);
123 //        FileUtils.write(externalsPath, externals);
124
125         String nodes = nodeJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
126         String nodesPath = Config.getConfigPath() + "/" + Config.getConfigNodesFileName();
127         CmdExecutor.sshExecute("echo " + nodes + " > " + nodesPath);
128 //        FileUtils.write(nodesPath, nodes);
129
130         String links = externalJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
131         String linksPath = Config.getConfigPath() + "/" + Config.getConfigLinksFileName();
132         CmdExecutor.sshExecute("echo " + links + " > " + linksPath);
133 //        FileUtils.write(linksPath,links);
134     }
135
136     private void traversal() {
137         externalConnectorMap.clear();
138         internalLinks.clear();
139
140         for (Link link : links) {
141             String leftNode = link.getSrcConnector().getNodeName();
142             String rightNode = link.getDstConnector().getNodeName();
143             if (hostMap.containsKey(leftNode) && switchMap.containsKey(rightNode)) {
144                 externalConnectorMap.put(link.getDstConnector(), link.getSrcConnector());
145             } else if (hostMap.containsKey(rightNode) && switchMap.containsKey(leftNode)) {
146                 externalConnectorMap.put(link.getSrcConnector(), link.getDstConnector());
147             } else if (switchMap.containsKey(leftNode) && switchMap.containsKey(rightNode)) {
148                 internalLinks.add(link);
149             } else {
150                 log.error("Illegal link: {}.", link);
151             }
152         }
153     }
154
155     public JSONObject nodeJsonNode() {
156         JSONObject root = new JSONObject();
157         JSONArray nodeArray = new JSONArray();
158         int index = 0;
159         try {
160             for (Switch sw : switchMap.values()) {
161                 JSONObject nodeJson = buildNodeJson(sw);
162                 if (nodeJson != null) {
163                     nodeArray.put(index, nodeJson);
164                     index++;
165                 }
166             }
167             root.put("node", nodeArray);
168             return root;
169         } catch (JSONException e) {
170             // TODO Auto-generated catch block
171             log.error("Exception:",e);
172         }
173         return null;
174     }
175
176     private JSONObject buildNodeJson(Switch sw) {
177         JSONObject nodeJson = new JSONObject();
178         try {
179             String nodeId = "openflow:" + sw.getDataPathId();
180             String type = sw instanceof Router ? "router" : "switch";
181             JSONArray attributes = defaultNodeAttributes(nodeId);
182             JSONArray ports = new JSONArray();
183             int index = 0;
184             List<Connector> connectors = sw.getConnectors();
185             for (Connector connector : connectors) {
186                 JSONObject portJson = new JSONObject();
187                 String portId = nodeId + ":" + connector.getOrder();
188                 String portType = externalConnectorMap.containsKey(connector) ? "external" : "internal";
189                 JSONArray portAttributes = defaultPortAttributes(portId);
190                 portJson.put("port-id", portId);
191                 portJson.put("port-type", portType);
192                 portJson.put("attribute", portAttributes);
193                 ports.put(index, portJson);
194                 index++;
195             }
196             nodeJson.put("node-id", nodeId);
197             nodeJson.put("node-type", type);
198             nodeJson.put("attribute", attributes);
199             nodeJson.put("port", ports);
200             return nodeJson;
201         } catch (Exception e) {
202             // TODO Auto-generated catch block
203             log.error("Exception:",e);
204         }
205         return null;
206     }
207
208     private JSONArray defaultNodeAttributes(String nodeId) throws JSONException {
209         JSONArray jsonArray = new JSONArray();
210         JSONObject jsonObject = new JSONObject();
211         jsonObject.put("name", "location");
212         jsonObject.put("value", nodeId);
213         jsonArray.put(0, jsonObject);
214         return jsonArray;
215     }
216
217     private JSONArray defaultPortAttributes(String portId) throws JSONException {
218         JSONArray jsonArray = new JSONArray();
219         JSONObject jsonObject = new JSONObject();
220         jsonObject.put("name", "location");
221         jsonObject.put("value", portId);
222         jsonArray.put(0, jsonObject);
223         return jsonArray;
224     }
225
226     public JSONObject externalJsonNode() {
227         JSONObject root = new JSONObject();
228         JSONArray externalMacs = new JSONArray();
229         int index = 0;
230         try {
231             for (Connector exCon : externalConnectorMap.keySet()) {
232                 Connector hostCon = externalConnectorMap.get(exCon);
233                 String hostMac = CmdExecutor.queryInterfaceMac(hostCon.getConnectorName(), hostCon.getNodeName());
234                 Switch sw = switchMap.get(exCon.getNodeName());
235                 if (hostMac != null && sw != null) {
236                     JSONObject exNode = new JSONObject();
237                     String nodeId = "openflow:" + sw.getDataPathId();
238                     String portId = nodeId + ":" + exCon.getOrder();
239
240                     exNode.put("node-id", nodeId);
241                     exNode.put("port-id", portId);
242                     exNode.put("mac-address", hostMac);
243                     externalMacs.put(index, exNode);
244                     index++;
245                 }
246             }
247             root.put("external-network-mac", externalMacs);
248             return root;
249         } catch (JSONException e) {
250             // TODO Auto-generated catch block
251             log.error("Exception:",e);
252         }
253         return null;
254     }
255
256     public JSONObject hostJsonNode() {
257         JSONObject root = new JSONObject();
258         JSONArray hostArray = new JSONArray();
259         int index = 0;
260         try {
261             for (Connector exCon : externalConnectorMap.keySet()) {
262                 Connector hostCon = externalConnectorMap.get(exCon);
263                 String exConMac = CmdExecutor.queryInterfaceMac(exCon.getConnectorName());
264                 Switch sw = switchMap.get(exCon.getNodeName());
265
266                 JSONObject hostJson = new JSONObject();
267                 String hostName = hostCon.getNodeName();
268                 Host host = hostMap.get(hostName);
269                 if ( exConMac != null && sw != null && host != null) {
270                     JSONArray ipv4Array = ipAddress(hostCon);
271                     String nodeId = "openflow:" + sw.getDataPathId();
272                     String connectorId = nodeId + ":" + exCon.getOrder();
273                     hostJson.put("name", hostName);
274                     hostJson.put("id", host.getUuid());
275                     hostJson.put("type", host.getNodeType());
276                     hostJson.put("ip-addresses", ipv4Array);
277                     hostJson.put("mac-address", exConMac);
278                     hostJson.put("node-id", nodeId);
279                     hostJson.put("connector-id", connectorId);
280                     hostArray.put(index, hostJson);
281                     index++;
282                 } else {
283                     log.error("Can not put host [{}] to configuration file,exMac:{}  sw:{} id:{}.", hostName, sw);
284                 }
285             }
286             root.put("host", hostArray);
287             return root;
288         } catch (JSONException e) {
289             // TODO Auto-generated catch block
290             log.error("Exception:",e);
291         }
292         return null;
293     }
294
295     private JSONArray ipAddress(Connector connector) throws JSONException {
296         JSONArray jsonArray = new JSONArray();
297         Host host = hostMap.get(connector.getNodeName());
298         if (host != null) {
299             String ipv4 = host.getIpv4(connector.getOrder());
300             if (ipv4 != null) {
301                 ipv4 = ipv4 + "/";
302                 ipv4 = ipv4.substring(0, ipv4.indexOf("/"));
303                 JSONObject jsonObject = new JSONObject();
304                 jsonObject.put("ip-address", ipv4);
305                 jsonArray.put(0, jsonObject);
306                 return jsonArray;
307             }
308         }
309         return null;
310     }
311
312     public JSONObject innerLinkJsonNode() {
313         JSONObject root = new JSONObject();
314         JSONArray linkArray = new JSONArray();
315         int index = 0;
316         try {
317             for (Link link : internalLinks) {
318                 JSONObject srcLinkNode = buildLinkNode(link.getSrcConnector());
319                 if (srcLinkNode != null) {
320                     linkArray.put(index, srcLinkNode);
321                     index++;
322                 }
323                 JSONObject dstLinkNode = buildLinkNode(link.getDstConnector());
324                 if (srcLinkNode != null) {
325                     linkArray.put(index, dstLinkNode);
326                     index++;
327                 }
328             }
329             root.put("link", linkArray);
330         } catch (JSONException e) {
331             // TODO Auto-generated catch block
332             log.error("Exception:",e);
333             return null;
334         }
335         return root;
336     }
337
338     private JSONObject buildLinkNode(Connector connector) {
339         Switch sw = switchMap.get(connector.getNodeName());
340         if (sw != null) {
341             JSONObject linkNode = new JSONObject();
342             String nodeId = "openflow:" + sw.getDataPathId();
343             String linkId = nodeId + ":" + connector.getOrder();
344             try {
345                 linkNode.put("link-id", linkId);
346                 linkNode.put("metric", "1");
347                 linkNode.put("delay", "");
348                 linkNode.put("loss-rate", "");
349                 return linkNode;
350             } catch (JSONException e) {
351                 // TODO Auto-generated catch block
352                 log.error("Exception:",e);
353             }
354         }
355         return null;
356     }
357 }