3c51fa9dcf6957b178ef6cf2dfa2ebfad70e683d
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.segmentrouting.config;
17
18 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22
23 import org.onosproject.net.Link;
24 import org.onosproject.segmentrouting.config.NetworkConfig.LinkConfig;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.fasterxml.jackson.databind.JsonNode;
29
30 /**
31  * Reserved for future use.
32  * Configuration for a link between two packet-switches.
33  */
34 public class PktLinkConfig extends LinkConfig {
35     protected static final Logger log = LoggerFactory
36             .getLogger(PktLinkConfig.class);
37     private int port1;
38     private int port2;
39     private String nodeName1;
40     private String nodeName2;
41     private List<Link> linkTupleList;
42
43     public PktLinkConfig(LinkConfig lkc) {
44         nodeDpid1 = lkc.getNodeDpid1();
45         nodeDpid2 = lkc.getNodeDpid2();
46         dpid1 = lkc.getDpid1();
47         dpid2 = lkc.getDpid2();
48         type = lkc.getType();
49         allowed = lkc.isAllowed();
50         params = lkc.getParams();
51         publishAttributes = new ConcurrentHashMap<>();
52         parseParams();
53         validateParams();
54         setPublishAttributes();
55     }
56
57     // ********************
58     // Packet Link Configuration
59     // ********************
60
61     public int getPort1() {
62         return port1;
63     }
64
65     public void setPort1(int port1) {
66         this.port1 = port1;
67     }
68
69     public int getPort2() {
70         return port2;
71     }
72
73     public void setPort2(int port2) {
74         this.port2 = port2;
75     }
76
77     public String getNodeName1() {
78         return nodeName1;
79     }
80
81     public void setNodeName1(String nodeName1) {
82         this.nodeName1 = nodeName1;
83     }
84
85     public String getNodeName2() {
86         return nodeName2;
87     }
88
89     public void setNodeName2(String nodeName2) {
90         this.nodeName2 = nodeName2;
91     }
92
93     /**
94      * Returns the two unidirectional links corresponding to the packet-link
95      * configuration. It is possible that the ports in the LinkTuple have
96      * portnumber '0', implying that the configuration applies to all links
97      * between the two switches.
98      *
99      * @return a list of LinkTuple with exactly 2 unidirectional links
100      */
101     public List<Link> getLinkTupleList() {
102         return linkTupleList;
103     }
104
105     private void setPublishAttributes() {
106
107     }
108
109     private void parseParams() {
110         if (params == null) {
111             throw new PktLinkParamsNotSpecified(nodeDpid1, nodeDpid2);
112         }
113         Set<Entry<String, JsonNode>> m = params.entrySet();
114         for (Entry<String, JsonNode> e : m) {
115             String key = e.getKey();
116             JsonNode j = e.getValue();
117             if (key.equals("nodeName1")) {
118                 setNodeName1(j.asText());
119             } else if (key.equals("nodeName2")) {
120                 setNodeName2(j.asText());
121             } else if (key.equals("port1")) {
122                 setPort1(j.asInt());
123             } else if (key.equals("port2")) {
124                 setPort2(j.asInt());
125             } else {
126                 throw new UnknownPktLinkConfig(key, nodeDpid1, nodeDpid2);
127             }
128         }
129     }
130
131     private void validateParams() {
132         // TODO - wrong-names, duplicate links,
133         // duplicate use of port, is switch-allowed for which link is allowed?
134         // valid port numbers
135     }
136
137     public static class PktLinkParamsNotSpecified extends RuntimeException {
138         private static final long serialVersionUID = 6247582323691265513L;
139
140         public PktLinkParamsNotSpecified(String dpidA, String dpidB) {
141             super();
142             log.error("Params required for packet link - not specified "
143                     + "for link between switch1:{} and switch2:{}",
144                     dpidA, dpidB);
145         }
146     }
147
148     public static class UnknownPktLinkConfig extends RuntimeException {
149         private static final long serialVersionUID = -5750132094884129179L;
150
151         public UnknownPktLinkConfig(String key, String dpidA, String dpidB) {
152             super();
153             log.error("Unknown packet-link config {} for link between"
154                     + " dpid1: {} and dpid2: {}", key,
155                     dpidA, dpidB);
156         }
157     }
158
159 }