914926891297518a3838e812395b9289bc0ff161
[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 /**
25  * Created by hj on 12/9/15.
26  */
27 public class Network {
28     private static Logger log = LoggerFactory.getLogger(Network.class);
29     private Map<String/*name*/, Host> hostMap;
30     private Map<String/*name*/, Switch> switchMap;
31     private Map<Connector/*External connector*/, Connector/*Connector on host*/> externalConnectorMap;
32     private List<Link> internalLinks;
33     private List<Link> links;
34
35     public Network() {
36         hostMap = new HashMap<String, Host>();
37         switchMap = new HashMap<String, Switch>();
38         links = new ArrayList<Link>();
39
40         externalConnectorMap = new HashMap<Connector, Connector>();
41         internalLinks = new ArrayList<Link>();
42     }
43
44     public void addHost(Host host) {
45         hostMap.put(host.getName(), host);
46     }
47
48     public void addSwitch(Switch sw) {
49         switchMap.put(sw.getName(), sw);
50     }
51
52     public void addLink(Link link) {
53         links.add(link);
54     }
55
56     public String execute(String name, String command) {
57         Host host = hostMap.get(name);
58         if (host != null) {
59             if (CmdExecutor.open()) {
60                 String result = CmdExecutor.sshExecute("ip netns exec " + name + " " + command);
61                 CmdExecutor.close();
62                 return result;
63             } else {
64                 return "Can not open ssh right now,please try again.";
65             }
66         }
67         return name + " " + "is not a firewall,can not execute " + command;
68     }
69
70     public void install() {
71         traversal();
72
73         for (Link link : links) {
74             link.install();
75         }
76
77         for (Node node : switchMap.values()) {
78             node.install();
79         }
80
81         for (Node node : hostMap.values()) {
82             node.install();
83         }
84     }
85
86     public void uninstall() {
87         for (Link link : links) {
88             link.uninstall();
89         }
90
91         pKill();
92
93         for (Host host : hostMap.values()) {
94             host.uninstall();
95         }
96     }
97
98     private void pKill() {
99         try {
100             CmdExecutor.sshExecute("pkill -9 ofdatapath");
101             CmdExecutor.sshExecute("pkill -9 ofprotocol");
102
103             CmdExecutor.sshExecute("pkill -9 fail-ofdatapath");
104             CmdExecutor.sshExecute("pkill -9 fail-ofprotocol");
105
106             CmdExecutor.sshExecute("pkill -9 ext-ofdatapath");
107             CmdExecutor.sshExecute("pkill -9 ext-ofprotocol");
108         } catch (Exception e) {
109
110         }
111     }
112
113     public void echoConfig() {
114         CmdExecutor.sshExecute("mkdir -p " + Config.getConfigPath());
115
116         String hosts = hostJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
117         String hostsPath = Config.getConfigPath() + "/" + Config.getConfigHostsFileName();
118         CmdExecutor.sshExecute("echo " + hosts + " > " + hostsPath);
119 //        FileUtils.write(hostsPath, hosts);
120
121         String externals = externalJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
122         String externalsPath = Config.getConfigPath() + "/" + Config.getConfigExternalsFileName();
123         CmdExecutor.sshExecute("echo " + externals + " > " + externalsPath);
124 //        FileUtils.write(externalsPath, externals);
125
126         String nodes = nodeJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
127         String nodesPath = Config.getConfigPath() + "/" + Config.getConfigNodesFileName();
128         CmdExecutor.sshExecute("echo " + nodes + " > " + nodesPath);
129 //        FileUtils.write(nodesPath, nodes);
130
131         String links = externalJsonNode().toString().replaceAll("\"", "\\\\\"").replaceAll("\\{", "\\\\{");
132         String linksPath = Config.getConfigPath() + "/" + Config.getConfigLinksFileName();
133         CmdExecutor.sshExecute("echo " + links + " > " + linksPath);
134 //        FileUtils.write(linksPath,links);
135     }
136
137     private void traversal() {
138         externalConnectorMap.clear();
139         internalLinks.clear();
140
141         for (Link link : links) {
142             String leftNode = link.getSrcConnector().getNodeName();
143             String rightNode = link.getDstConnector().getNodeName();
144             if (hostMap.containsKey(leftNode) && switchMap.containsKey(rightNode)) {
145                 externalConnectorMap.put(link.getDstConnector(), link.getSrcConnector());
146             } else if (hostMap.containsKey(rightNode) && switchMap.containsKey(leftNode)) {
147                 externalConnectorMap.put(link.getSrcConnector(), link.getDstConnector());
148             } else if (switchMap.containsKey(leftNode) && switchMap.containsKey(rightNode)) {
149                 internalLinks.add(link);
150             } else {
151                 log.error("Illegal link: {}.", link);
152             }
153         }
154     }
155
156     public JSONObject nodeJsonNode() {
157         JSONObject root = new JSONObject();
158         JSONArray nodeArray = new JSONArray();
159         int index = 0;
160         try {
161             for (Switch sw : switchMap.values()) {
162                 JSONObject nodeJson = buildNodeJson(sw);
163                 if (nodeJson != null) {
164                     nodeArray.put(index, nodeJson);
165                     index++;
166                 }
167             }
168             root.put("node", nodeArray);
169             return root;
170         } catch (JSONException e) {
171             e.printStackTrace();
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             e.printStackTrace();
203         }
204         return null;
205     }
206
207     private JSONArray defaultNodeAttributes(String nodeId) throws JSONException {
208         JSONArray jsonArray = new JSONArray();
209         JSONObject jsonObject = new JSONObject();
210         jsonObject.put("name", "location");
211         jsonObject.put("value", nodeId);
212         jsonArray.put(0, jsonObject);
213         return jsonArray;
214     }
215
216     private JSONArray defaultPortAttributes(String portId) throws JSONException {
217         JSONArray jsonArray = new JSONArray();
218         JSONObject jsonObject = new JSONObject();
219         jsonObject.put("name", "location");
220         jsonObject.put("value", portId);
221         jsonArray.put(0, jsonObject);
222         return jsonArray;
223     }
224
225     public JSONObject externalJsonNode() {
226         JSONObject root = new JSONObject();
227         JSONArray externalMacs = new JSONArray();
228         int index = 0;
229         try {
230             for (Connector exCon : externalConnectorMap.keySet()) {
231                 Connector hostCon = externalConnectorMap.get(exCon);
232                 String hostMac = CmdExecutor.queryInterfaceMac(hostCon.getConnectorName(), hostCon.getNodeName());
233                 Switch sw = switchMap.get(exCon.getNodeName());
234                 if (hostMac != null && sw != null) {
235                     JSONObject exNode = new JSONObject();
236                     String nodeId = "openflow:" + sw.getDataPathId();
237                     String portId = nodeId + ":" + exCon.getOrder();
238
239                     exNode.put("node-id", nodeId);
240                     exNode.put("port-id", portId);
241                     exNode.put("mac-address", hostMac);
242                     externalMacs.put(index, exNode);
243                     index++;
244                 }
245             }
246             root.put("external-network-mac", externalMacs);
247             return root;
248         } catch (JSONException e) {
249             e.printStackTrace();
250         }
251         return null;
252     }
253
254     public JSONObject hostJsonNode() {
255         JSONObject root = new JSONObject();
256         JSONArray hostArray = new JSONArray();
257         int index = 0;
258         try {
259             for (Connector exCon : externalConnectorMap.keySet()) {
260                 Connector hostCon = externalConnectorMap.get(exCon);
261                 String exConMac = CmdExecutor.queryInterfaceMac(exCon.getConnectorName());
262                 Switch sw = switchMap.get(exCon.getNodeName());
263
264                 JSONObject hostJson = new JSONObject();
265                 String hostName = hostCon.getNodeName();
266                 Host host = hostMap.get(hostName);
267                 if ( exConMac != null && sw != null && host != null) {
268                     JSONArray ipv4Array = ipAddress(hostCon);
269                     String nodeId = "openflow:" + sw.getDataPathId();
270                     String connectorId = nodeId + ":" + exCon.getOrder();
271                     hostJson.put("name", hostName);
272                     hostJson.put("id", host.getUuid());
273                     hostJson.put("type", host.getNodeType());
274                     hostJson.put("ip-addresses", ipv4Array);
275                     hostJson.put("mac-address", exConMac);
276                     hostJson.put("node-id", nodeId);
277                     hostJson.put("connector-id", connectorId);
278                     hostArray.put(index, hostJson);
279                     index++;
280                 } else {
281                     log.error("Can not put host [{}] to configuration file,exMac:{}  sw:{} id:{}.", hostName, sw);
282                 }
283             }
284             root.put("host", hostArray);
285             return root;
286         } catch (JSONException e) {
287             e.printStackTrace();
288         }
289         return null;
290     }
291
292     private JSONArray ipAddress(Connector connector) throws JSONException {
293         JSONArray jsonArray = new JSONArray();
294         Host host = hostMap.get(connector.getNodeName());
295         if (host != null) {
296             String ipv4 = host.getIpv4(connector.getOrder());
297             if (ipv4 != null) {
298                 ipv4 = ipv4 + "/";
299                 ipv4 = ipv4.substring(0, ipv4.indexOf("/"));
300                 JSONObject jsonObject = new JSONObject();
301                 jsonObject.put("ip-address", ipv4);
302                 jsonArray.put(0, jsonObject);
303                 return jsonArray;
304             }
305         }
306         return null;
307     }
308
309     public JSONObject innerLinkJsonNode() {
310         JSONObject root = new JSONObject();
311         JSONArray linkArray = new JSONArray();
312         int index = 0;
313         try {
314             for (Link link : internalLinks) {
315                 JSONObject srcLinkNode = buildLinkNode(link.getSrcConnector());
316                 if (srcLinkNode != null) {
317                     linkArray.put(index, srcLinkNode);
318                     index++;
319                 }
320                 JSONObject dstLinkNode = buildLinkNode(link.getDstConnector());
321                 if (srcLinkNode != null) {
322                     linkArray.put(index, dstLinkNode);
323                     index++;
324                 }
325             }
326             root.put("link", linkArray);
327         } catch (JSONException e) {
328             e.printStackTrace();
329             return null;
330         }
331         return root;
332     }
333
334     private JSONObject buildLinkNode(Connector connector) {
335         Switch sw = switchMap.get(connector.getNodeName());
336         if (sw != null) {
337             JSONObject linkNode = new JSONObject();
338             String nodeId = "openflow:" + sw.getDataPathId();
339             String linkId = nodeId + ":" + connector.getOrder();
340             try {
341                 linkNode.put("link-id", linkId);
342                 linkNode.put("metric", "1");
343                 linkNode.put("delay", "");
344                 linkNode.put("loss-rate", "");
345                 return linkNode;
346             } catch (JSONException e) {
347                 e.printStackTrace();
348             }
349         }
350         return null;
351     }
352 }