blob: e205e77812f621ab8bb47c3724eddbbf952087ab [file] [log] [blame]
Saurav Dasfb93c252014-08-18 20:40:13 -07001package net.onrc.onos.core.configmanager;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Map.Entry;
6import java.util.Set;
7import java.util.concurrent.ConcurrentHashMap;
8
9import net.onrc.onos.core.configmanager.NetworkConfig.LinkConfig;
10import net.onrc.onos.core.util.Dpid;
11import net.onrc.onos.core.util.LinkTuple;
12import net.onrc.onos.core.util.PortNumber;
13
14import org.codehaus.jackson.JsonNode;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
18/**
19 * Configuration for a link between two packet-switches.
20 */
21public class PktLinkConfig extends LinkConfig {
22 protected static final Logger log = LoggerFactory
23 .getLogger(PktLinkConfig.class);
24 private int port1;
25 private int port2;
26 private String nodeName1;
27 private String nodeName2;
28 private List<LinkTuple> linkTupleList;
29
30 public PktLinkConfig(LinkConfig lkc) {
31 nodeDpid1 = lkc.getNodeDpid1();
32 nodeDpid2 = lkc.getNodeDpid2();
33 dpid1 = lkc.getDpid1();
34 dpid2 = lkc.getDpid2();
35 type = lkc.getType();
36 allowed = lkc.isAllowed();
37 params = lkc.getParams();
38 publishAttributes = new ConcurrentHashMap<String, String>();
39 parseParams();
40 validateParams();
41 setPublishAttributes();
42 }
43
44 // ********************
45 // Packet Link Configuration
46 // ********************
47
48 public int getPort1() {
49 return port1;
50 }
51
52 public void setPort1(int port1) {
53 this.port1 = port1;
54 }
55
56 public int getPort2() {
57 return port2;
58 }
59
60 public void setPort2(int port2) {
61 this.port2 = port2;
62 }
63
64 public String getNodeName1() {
65 return nodeName1;
66 }
67
68 public void setNodeName1(String nodeName1) {
69 this.nodeName1 = nodeName1;
70 }
71
72 public String getNodeName2() {
73 return nodeName2;
74 }
75
76 public void setNodeName2(String nodeName2) {
77 this.nodeName2 = nodeName2;
78 }
79
80 /**
81 * Returns the two unidirectional links corresponding to the packet-link
82 * configuration. It is possible that the ports in the LinkTuple have
83 * portnumber '0', implying that the configuration applies to all links
84 * between the two switches.
85 *
86 * @return a list of LinkTuple with exactly 2 unidirectional links
87 */
88 public List<LinkTuple> getLinkTupleList() {
89 if (linkTupleList != null) {
90 return linkTupleList;
91 } else {
92 linkTupleList = new ArrayList<LinkTuple>();
93 }
94
95 // first one direction
96 linkTupleList.add(new LinkTuple(
97 new Dpid(nodeDpid1), PortNumber.uint32(port1),
98 new Dpid(nodeDpid2), PortNumber.uint32(port2)));
99 // then the reverse direction
100 linkTupleList.add(new LinkTuple(
101 new Dpid(nodeDpid2), PortNumber.uint32(port2),
102 new Dpid(nodeDpid1), PortNumber.uint32(port1)));
103 return linkTupleList;
104 }
105
106 private void setPublishAttributes() {
107
108 }
109
110 private void parseParams() {
111 if (params == null) {
112 throw new PktLinkParamsNotSpecified(nodeDpid1, nodeDpid2);
113 }
114 Set<Entry<String, JsonNode>> m = params.entrySet();
115 for (Entry<String, JsonNode> e : m) {
116 String key = e.getKey();
117 JsonNode j = e.getValue();
118 if (key.equals("nodeName1")) {
119 setNodeName1(j.asText());
120 } else if (key.equals("nodeName2")) {
121 setNodeName2(j.asText());
122 } else if (key.equals("port1")) {
123 setPort1(j.asInt());
124 } else if (key.equals("port2")) {
125 setPort2(j.asInt());
126 } else {
127 throw new UnknownPktLinkConfig(key, nodeDpid1, nodeDpid2);
128 }
129 }
130 }
131
132 private void validateParams() {
133 // TODO - wrong-names, duplicate links,
134 // duplicate use of port, is switch-allowed for which link is allowed?
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700135 // valid port numbers
Saurav Dasfb93c252014-08-18 20:40:13 -0700136 }
137
138 public static class PktLinkParamsNotSpecified extends RuntimeException {
139 private static final long serialVersionUID = 6247582323691265513L;
140
141 public PktLinkParamsNotSpecified(String dpidA, String dpidB) {
142 super();
143 log.error("Params required for packet link - not specified "
144 + "for link between switch1:{} and switch2:{}",
145 dpidA, dpidB);
146 }
147 }
148
149 public static class UnknownPktLinkConfig extends RuntimeException {
150 private static final long serialVersionUID = -5750132094884129179L;
151
152 public UnknownPktLinkConfig(String key, String dpidA, String dpidB) {
153 super();
154 log.error("Unknown packet-link config {} for link between"
155 + " dpid1: {} and dpid2: {}", key,
156 dpidA, dpidB);
157 }
158 }
159
160}