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) {
171 // TODO Auto-generated catch block
177 private JSONObject buildNodeJson(Switch sw) {
178 JSONObject nodeJson = new JSONObject();
180 String nodeId = "openflow:" + sw.getDataPathId();
181 String type = sw instanceof Router ? "router" : "switch";
182 JSONArray attributes = defaultNodeAttributes(nodeId);
183 JSONArray ports = new JSONArray();
185 List<Connector> connectors = sw.getConnectors();
186 for (Connector connector : connectors) {
187 JSONObject portJson = new JSONObject();
188 String portId = nodeId + ":" + connector.getOrder();
189 String portType = externalConnectorMap.containsKey(connector) ? "external" : "internal";
190 JSONArray portAttributes = defaultPortAttributes(portId);
191 portJson.put("port-id", portId);
192 portJson.put("port-type", portType);
193 portJson.put("attribute", portAttributes);
194 ports.put(index, portJson);
197 nodeJson.put("node-id", nodeId);
198 nodeJson.put("node-type", type);
199 nodeJson.put("attribute", attributes);
200 nodeJson.put("port", ports);
202 } catch (Exception e) {
203 // TODO Auto-generated catch block
209 private JSONArray defaultNodeAttributes(String nodeId) throws JSONException {
210 JSONArray jsonArray = new JSONArray();
211 JSONObject jsonObject = new JSONObject();
212 jsonObject.put("name", "location");
213 jsonObject.put("value", nodeId);
214 jsonArray.put(0, jsonObject);
218 private JSONArray defaultPortAttributes(String portId) throws JSONException {
219 JSONArray jsonArray = new JSONArray();
220 JSONObject jsonObject = new JSONObject();
221 jsonObject.put("name", "location");
222 jsonObject.put("value", portId);
223 jsonArray.put(0, jsonObject);
227 public JSONObject externalJsonNode() {
228 JSONObject root = new JSONObject();
229 JSONArray externalMacs = new JSONArray();
232 for (Connector exCon : externalConnectorMap.keySet()) {
233 Connector hostCon = externalConnectorMap.get(exCon);
234 String hostMac = CmdExecutor.queryInterfaceMac(hostCon.getConnectorName(), hostCon.getNodeName());
235 Switch sw = switchMap.get(exCon.getNodeName());
236 if (hostMac != null && sw != null) {
237 JSONObject exNode = new JSONObject();
238 String nodeId = "openflow:" + sw.getDataPathId();
239 String portId = nodeId + ":" + exCon.getOrder();
241 exNode.put("node-id", nodeId);
242 exNode.put("port-id", portId);
243 exNode.put("mac-address", hostMac);
244 externalMacs.put(index, exNode);
248 root.put("external-network-mac", externalMacs);
250 } catch (JSONException e) {
251 // TODO Auto-generated catch block
257 public JSONObject hostJsonNode() {
258 JSONObject root = new JSONObject();
259 JSONArray hostArray = new JSONArray();
262 for (Connector exCon : externalConnectorMap.keySet()) {
263 Connector hostCon = externalConnectorMap.get(exCon);
264 String exConMac = CmdExecutor.queryInterfaceMac(exCon.getConnectorName());
265 Switch sw = switchMap.get(exCon.getNodeName());
267 JSONObject hostJson = new JSONObject();
268 String hostName = hostCon.getNodeName();
269 Host host = hostMap.get(hostName);
270 if ( exConMac != null && sw != null && host != null) {
271 JSONArray ipv4Array = ipAddress(hostCon);
272 String nodeId = "openflow:" + sw.getDataPathId();
273 String connectorId = nodeId + ":" + exCon.getOrder();
274 hostJson.put("name", hostName);
275 hostJson.put("id", host.getUuid());
276 hostJson.put("type", host.getNodeType());
277 hostJson.put("ip-addresses", ipv4Array);
278 hostJson.put("mac-address", exConMac);
279 hostJson.put("node-id", nodeId);
280 hostJson.put("connector-id", connectorId);
281 hostArray.put(index, hostJson);
284 log.error("Can not put host [{}] to configuration file,exMac:{} sw:{} id:{}.", hostName, sw);
287 root.put("host", hostArray);
289 } catch (JSONException e) {
290 // TODO Auto-generated catch block
296 private JSONArray ipAddress(Connector connector) throws JSONException {
297 JSONArray jsonArray = new JSONArray();
298 Host host = hostMap.get(connector.getNodeName());
300 String ipv4 = host.getIpv4(connector.getOrder());
303 ipv4 = ipv4.substring(0, ipv4.indexOf("/"));
304 JSONObject jsonObject = new JSONObject();
305 jsonObject.put("ip-address", ipv4);
306 jsonArray.put(0, jsonObject);
313 public JSONObject innerLinkJsonNode() {
314 JSONObject root = new JSONObject();
315 JSONArray linkArray = new JSONArray();
318 for (Link link : internalLinks) {
319 JSONObject srcLinkNode = buildLinkNode(link.getSrcConnector());
320 if (srcLinkNode != null) {
321 linkArray.put(index, srcLinkNode);
324 JSONObject dstLinkNode = buildLinkNode(link.getDstConnector());
325 if (srcLinkNode != null) {
326 linkArray.put(index, dstLinkNode);
330 root.put("link", linkArray);
331 } catch (JSONException e) {
332 // TODO Auto-generated catch block
339 private JSONObject buildLinkNode(Connector connector) {
340 Switch sw = switchMap.get(connector.getNodeName());
342 JSONObject linkNode = new JSONObject();
343 String nodeId = "openflow:" + sw.getDataPathId();
344 String linkId = nodeId + ":" + connector.getOrder();
346 linkNode.put("link-id", linkId);
347 linkNode.put("metric", "1");
348 linkNode.put("delay", "");
349 linkNode.put("loss-rate", "");
351 } catch (JSONException e) {
352 // TODO Auto-generated catch block