Remove codes breaking security rules
[nemo.git] / nemo-tools / sandbox / src / main / java / org / opendaylight / nemo / tool / sandbox / models / Switch.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.opendaylight.nemo.tool.sandbox.utils.Config;
12 import org.opendaylight.nemo.tool.sandbox.utils.HexString;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19
20 /**
21  * Created by hj on 12/8/15.
22  */
23 public class Switch extends Node {
24     private static Logger log = LoggerFactory.getLogger(Switch.class);
25     private final long dataPathId;
26     private final String mac;
27
28     public Switch(String name, long dataPathId) {
29         super(NodeType.SWITCH, name);
30         this.dataPathId = dataPathId;
31
32         this.mac = HexString.toHexString(dataPathId);
33     }
34
35     public long getDataPathId() {
36         return dataPathId;
37     }
38
39     @Override
40     protected List<String> generateCommands() {
41         List<String> commands = new ArrayList<String>();
42
43         String controllerSocket = Config.getControllerSocket();
44         String strInterface = "";
45         Collections.sort(connectors);
46         for (Connector connector : this.connectors) {
47             strInterface += ("," + connector.getConnectorName());
48         }
49         if (this.connectors.isEmpty()) {
50             log.warn("Switch {} does not have ports.");
51             strInterface = "";
52         } else {
53             strInterface = strInterface.substring(1);
54         }
55         String ofDataPathCreate = "ofdatapath";
56         if (!strInterface.equals("")) {
57             ofDataPathCreate += (" -i " + strInterface);
58         }
59         ofDataPathCreate += (" punix:/tmp/sw" + dataPathId + " -d " + mac + " 1> /tmp/sw" + dataPathId + "-ofd.log 2> /tmp/sw" + dataPathId + "-ofd.log &");
60         commands.add(ofDataPathCreate);
61
62         String ofProtocol = "ofprotocol unix:/tmp/sw" + dataPathId + " tcp:" + controllerSocket + " --fail=closed  1> /tmp/sw" + dataPathId + "-ofp.log 2>/tmp/sw" + dataPathId + "-ofp.log &";
63         commands.add(ofProtocol);
64         String lldp = "dpctl unix:/tmp/sw" + dataPathId + " flow-mod cmd=add,table=0 eth_type=0x88cc  apply:output=ctrl:0xff";
65         commands.add(lldp);
66         String arp = "dpctl unix:/tmp/sw" + dataPathId + " flow-mod cmd=add,table=0 eth_type=0x0806  apply:output=ctrl:0xff";
67         commands.add(arp);
68
69         return commands;
70     }
71
72     @Override
73     public String toString() {
74         return "Switch{" +
75                 "name=" + getName() +
76                 ", dataPathId=" + dataPathId +
77                 ", mac='" + mac + '\'' +
78                 ", connectors=" + connectors +
79                 '}';
80     }
81 }