2 * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
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
9 package org.opendaylight.nemo.tool.sandbox.models;
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;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
25 * Created by hj on 12/9/15.
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;
36 hostMap = new HashMap<String, Host>();
37 switchMap = new HashMap<String, Switch>();
38 links = new ArrayList<Link>();
40 externalConnectorMap = new HashMap<Connector, Connector>();
41 internalLinks = new ArrayList<Link>();
44 public void addHost(Host host) {
45 hostMap.put(host.getName(), host);
48 public void addSwitch(Switch sw) {
49 switchMap.put(sw.getName(), sw);
52 public void addLink(Link link) {
56 public String execute(String name, String command) {
57 Host host = hostMap.get(name);
59 if (CmdExecutor.open()) {
60 String result = CmdExecutor.sshExecute("ip netns exec " + name + " " + command);
64 return "Can not open ssh right now,please try again.";
67 return name + " " + "is not a firewall,can not execute " + command;
70 public void install() {
73 for (Link link : links) {
77 for (Node node : switchMap.values()) {
81 for (Node node : hostMap.values()) {
86 public void uninstall() {
87 for (Link link : links) {
93 for (Host host : hostMap.values()) {
98 private void pKill() {
100 CmdExecutor.sshExecute("pkill -9 ofdatapath");
101 CmdExecutor.sshExecute("pkill -9 ofprotocol");
103 CmdExecutor.sshExecute("pkill -9 fail-ofdatapath");
104 CmdExecutor.sshExecute("pkill -9 fail-ofprotocol");
106 CmdExecutor.sshExecute("pkill -9 ext-ofdatapath");
107 CmdExecutor.sshExecute("pkill -9 ext-ofprotocol");
108 } catch (Exception e) {
113 public void echoConfig() {
114 CmdExecutor.sshExecute("mkdir -p " + Config.getConfigPath());
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);
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);
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);
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);
137 private void traversal() {
138 externalConnectorMap.clear();
139 internalLinks.clear();
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);
151 log.error("Illegal link: {}.", link);
156 public JSONObject nodeJsonNode() {
157 JSONObject root = new JSONObject();
158 JSONArray nodeArray = new JSONArray();
161 for (Switch sw : switchMap.values()) {
162 JSONObject nodeJson = buildNodeJson(sw);
163 if (nodeJson != null) {
164 nodeArray.put(index, nodeJson);
168 root.put("node", nodeArray);
170 } catch (JSONException e) {
176 private JSONObject buildNodeJson(Switch sw) {
177 JSONObject nodeJson = new JSONObject();
179 String nodeId = "openflow:" + sw.getDataPathId();
180 String type = sw instanceof Router ? "router" : "switch";
181 JSONArray attributes = defaultNodeAttributes(nodeId);
182 JSONArray ports = new JSONArray();
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);
196 nodeJson.put("node-id", nodeId);
197 nodeJson.put("node-type", type);
198 nodeJson.put("attribute", attributes);
199 nodeJson.put("port", ports);
201 } catch (Exception e) {
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);
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);
225 public JSONObject externalJsonNode() {
226 JSONObject root = new JSONObject();
227 JSONArray externalMacs = new JSONArray();
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();
239 exNode.put("node-id", nodeId);
240 exNode.put("port-id", portId);
241 exNode.put("mac-address", hostMac);
242 externalMacs.put(index, exNode);
246 root.put("external-network-mac", externalMacs);
248 } catch (JSONException e) {
254 public JSONObject hostJsonNode() {
255 JSONObject root = new JSONObject();
256 JSONArray hostArray = new JSONArray();
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());
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);
281 log.error("Can not put host [{}] to configuration file,exMac:{} sw:{} id:{}.", hostName, sw);
284 root.put("host", hostArray);
286 } catch (JSONException e) {
292 private JSONArray ipAddress(Connector connector) throws JSONException {
293 JSONArray jsonArray = new JSONArray();
294 Host host = hostMap.get(connector.getNodeName());
296 String ipv4 = host.getIpv4(connector.getOrder());
299 ipv4 = ipv4.substring(0, ipv4.indexOf("/"));
300 JSONObject jsonObject = new JSONObject();
301 jsonObject.put("ip-address", ipv4);
302 jsonArray.put(0, jsonObject);
309 public JSONObject innerLinkJsonNode() {
310 JSONObject root = new JSONObject();
311 JSONArray linkArray = new JSONArray();
314 for (Link link : internalLinks) {
315 JSONObject srcLinkNode = buildLinkNode(link.getSrcConnector());
316 if (srcLinkNode != null) {
317 linkArray.put(index, srcLinkNode);
320 JSONObject dstLinkNode = buildLinkNode(link.getDstConnector());
321 if (srcLinkNode != null) {
322 linkArray.put(index, dstLinkNode);
326 root.put("link", linkArray);
327 } catch (JSONException e) {
334 private JSONObject buildLinkNode(Connector connector) {
335 Switch sw = switchMap.get(connector.getNodeName());
337 JSONObject linkNode = new JSONObject();
338 String nodeId = "openflow:" + sw.getDataPathId();
339 String linkId = nodeId + ":" + connector.getOrder();
341 linkNode.put("link-id", linkId);
342 linkNode.put("metric", "1");
343 linkNode.put("delay", "");
344 linkNode.put("loss-rate", "");
346 } catch (JSONException e) {