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;
24 * Created by hj on 12/9/15.
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;
35 hostMap = new HashMap<String, Host>();
36 switchMap = new HashMap<String, Switch>();
37 links = new ArrayList<Link>();
39 externalConnectorMap = new HashMap<Connector, Connector>();
40 internalLinks = new ArrayList<Link>();
43 public void addHost(Host host) {
44 hostMap.put(host.getName(), host);
47 public void addSwitch(Switch sw) {
48 switchMap.put(sw.getName(), sw);
51 public void addLink(Link link) {
55 public String execute(String name, String command) {
56 Host host = hostMap.get(name);
58 if (CmdExecutor.open()) {
59 String result = CmdExecutor.sshExecute("ip netns exec " + name + " " + command);
63 return "Can not open ssh right now,please try again.";
66 return name + " " + "is not a firewall,can not execute " + command;
69 public void install() {
72 for (Link link : links) {
76 for (Node node : switchMap.values()) {
80 for (Node node : hostMap.values()) {
85 public void uninstall() {
86 for (Link link : links) {
92 for (Host host : hostMap.values()) {
97 private void pKill() {
99 CmdExecutor.sshExecute("pkill -9 ofdatapath");
100 CmdExecutor.sshExecute("pkill -9 ofprotocol");
102 CmdExecutor.sshExecute("pkill -9 fail-ofdatapath");
103 CmdExecutor.sshExecute("pkill -9 fail-ofprotocol");
105 CmdExecutor.sshExecute("pkill -9 ext-ofdatapath");
106 CmdExecutor.sshExecute("pkill -9 ext-ofprotocol");
107 } catch (Exception e) {
112 public void echoConfig() {
113 CmdExecutor.sshExecute("mkdir -p " + Config.getConfigPath());
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);
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);
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);
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);
136 private void traversal() {
137 externalConnectorMap.clear();
138 internalLinks.clear();
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);
150 log.error("Illegal link: {}.", link);
155 public JSONObject nodeJsonNode() {
156 JSONObject root = new JSONObject();
157 JSONArray nodeArray = new JSONArray();
160 for (Switch sw : switchMap.values()) {
161 JSONObject nodeJson = buildNodeJson(sw);
162 if (nodeJson != null) {
163 nodeArray.put(index, nodeJson);
167 root.put("node", nodeArray);
169 } catch (JSONException e) {
170 // TODO Auto-generated catch block
171 log.error("Exception:",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) {
202 // TODO Auto-generated catch block
203 log.error("Exception:",e);
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);
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);
226 public JSONObject externalJsonNode() {
227 JSONObject root = new JSONObject();
228 JSONArray externalMacs = new JSONArray();
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();
240 exNode.put("node-id", nodeId);
241 exNode.put("port-id", portId);
242 exNode.put("mac-address", hostMac);
243 externalMacs.put(index, exNode);
247 root.put("external-network-mac", externalMacs);
249 } catch (JSONException e) {
250 // TODO Auto-generated catch block
251 log.error("Exception:",e);
256 public JSONObject hostJsonNode() {
257 JSONObject root = new JSONObject();
258 JSONArray hostArray = new JSONArray();
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());
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);
283 log.error("Can not put host [{}] to configuration file,exMac:{} sw:{} id:{}.", hostName, sw);
286 root.put("host", hostArray);
288 } catch (JSONException e) {
289 // TODO Auto-generated catch block
290 log.error("Exception:",e);
295 private JSONArray ipAddress(Connector connector) throws JSONException {
296 JSONArray jsonArray = new JSONArray();
297 Host host = hostMap.get(connector.getNodeName());
299 String ipv4 = host.getIpv4(connector.getOrder());
302 ipv4 = ipv4.substring(0, ipv4.indexOf("/"));
303 JSONObject jsonObject = new JSONObject();
304 jsonObject.put("ip-address", ipv4);
305 jsonArray.put(0, jsonObject);
312 public JSONObject innerLinkJsonNode() {
313 JSONObject root = new JSONObject();
314 JSONArray linkArray = new JSONArray();
317 for (Link link : internalLinks) {
318 JSONObject srcLinkNode = buildLinkNode(link.getSrcConnector());
319 if (srcLinkNode != null) {
320 linkArray.put(index, srcLinkNode);
323 JSONObject dstLinkNode = buildLinkNode(link.getDstConnector());
324 if (srcLinkNode != null) {
325 linkArray.put(index, dstLinkNode);
329 root.put("link", linkArray);
330 } catch (JSONException e) {
331 // TODO Auto-generated catch block
332 log.error("Exception:",e);
338 private JSONObject buildLinkNode(Connector connector) {
339 Switch sw = switchMap.get(connector.getNodeName());
341 JSONObject linkNode = new JSONObject();
342 String nodeId = "openflow:" + sw.getDataPathId();
343 String linkId = nodeId + ":" + connector.getOrder();
345 linkNode.put("link-id", linkId);
346 linkNode.put("metric", "1");
347 linkNode.put("delay", "");
348 linkNode.put("loss-rate", "");
350 } catch (JSONException e) {
351 // TODO Auto-generated catch block
352 log.error("Exception:",e);