36ab101a79a40525549543869f22befef25c679a
[controller.git] / opendaylight / switchmanager / api / src / main / java / org / opendaylight / controller / switchmanager / SubnetConfig.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.switchmanager;
11
12 import java.io.Serializable;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import javax.xml.bind.annotation.XmlAccessType;
21 import javax.xml.bind.annotation.XmlAccessorType;
22 import javax.xml.bind.annotation.XmlElement;
23 import javax.xml.bind.annotation.XmlRootElement;
24
25 import org.opendaylight.controller.sal.core.Node;
26 import org.opendaylight.controller.sal.core.NodeConnector;
27 import org.opendaylight.controller.sal.utils.GUIField;
28 import org.opendaylight.controller.sal.utils.NetUtils;
29 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
30
31 /**
32  * The class represents a subnet configuration.
33  */
34 @XmlRootElement
35 @XmlAccessorType(XmlAccessType.NONE)
36 public class SubnetConfig implements Cloneable, Serializable {
37     //static fields are by default excluded by Gson parser
38     private static final long serialVersionUID = 1L;
39     private static final String prettyFields[] = { GUIField.NAME.toString(),
40             GUIField.GATEWAYIP.toString(), GUIField.NODEPORTS.toString() };
41
42     // Order matters: JSP file expects following fields in the
43     // following order
44     /**
45      * Name of the subnet
46      */
47     @XmlElement
48     private String name;
49     /**
50      * A.B.C.D/MM  Where A.B.C.D is the Default
51      * Gateway IP (L3) or ARP Querier IP (L2)
52      */
53     @XmlElement
54     private String subnet;
55     /**
56      * node ID/port list:
57      *<node-id>/a,b,c-m,r-t,y
58      */
59     @XmlElement
60     private Set<String> nodePorts;
61
62     public SubnetConfig() {
63     }
64
65     public SubnetConfig(String desc, String sub, Set<String> sp) {
66         name = desc;
67         subnet = sub;
68         nodePorts = sp;
69     }
70
71     public SubnetConfig(SubnetConfig subnetConfig) {
72         name = subnetConfig.name;
73         subnet = subnetConfig.subnet;
74         nodePorts = new HashSet<String>(subnetConfig.nodePorts);
75     }
76
77     public String getName() {
78         return name;
79     }
80
81     public Set<String> getNodePorts() {
82         return nodePorts;
83     }
84
85     public String getSubnet() {
86         return subnet;
87     }
88
89     public InetAddress getIPnum() {
90         InetAddress ip = null;
91         try {
92             ip = InetAddress.getByName(subnet.split("/")[0]);
93         } catch (UnknownHostException e1) {
94             return null;
95         }
96         return ip;
97     }
98
99     public Short getIPMaskLen() {
100         Short maskLen = 0;
101         if (hasValidIP()) {
102             String[] s = subnet.split("/");
103             maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
104         }
105         return maskLen;
106     }
107
108     private Set<Short> getPortList(String ports) {
109         /*
110          * example:
111          *     ports = "1,3,5-12"
112          *     elemArray = ["1" "3" "5-12"]
113          *     elem[2] = "5-12" --> limits = ["5" "12"]
114          *     portList = [1 3 5 6 7 8 9 10 11 12]
115          */
116         Set<Short> portList = new HashSet<Short>();
117         String[] elemArray = ports.split(",");
118         for (String elem : elemArray) {
119             if (elem.contains("-")) {
120                 String[] limits = elem.split("-");
121                 for (short j = Short.valueOf(limits[0]); j <= Short
122                         .valueOf(limits[1]); j++) {
123                     portList.add(Short.valueOf(j));
124                 }
125             } else {
126                 portList.add(Short.valueOf(elem));
127             }
128         }
129         return portList;
130     }
131
132     private boolean hasValidIP() {
133         if (subnet != null) {
134             if (NetUtils.isIPv4AddressValid(subnet)) {
135                 return true;
136             } else if (NetUtils.isIPv6AddressValid(subnet)) {
137                 return true;
138             }
139         }
140         return false;
141     }
142
143     private boolean hasValidPorts() {
144         for (String portSet : nodePorts) {
145             if (!portSet.contains("/")) {
146                 return false;
147             }
148         }
149         return true;
150     }
151
152     public boolean isValidSwitchPort(String sp) {
153         return sp.contains("/");
154     }
155
156     public boolean isValidConfig() {
157         return hasValidIP() && hasValidPorts();
158     }
159
160     @Override
161     public int hashCode() {
162         return name.hashCode();
163     }
164
165     @Override
166     public boolean equals(Object obj) {
167         /*
168          * Configuration will be stored in collection only if it is valid
169          * Hence we don't check here for uninitialized fields
170          */
171         if (this == obj)
172             return true;
173         if (obj == null)
174             return false;
175         if (getClass() != obj.getClass())
176             return false;
177         SubnetConfig that = (SubnetConfig) obj;
178         if (this.subnet.equals(that.subnet)) {
179             return true;
180         }
181         return false;
182     }
183
184     public static List<String> getGuiFieldsNames() {
185         List<String> fieldList = new ArrayList<String>();
186         for (String str : prettyFields) {
187             fieldList.add(str);
188         }
189         return fieldList;
190     }
191
192     //Utility method useful for adding to a passed Set all the
193     //NodeConnectors learnt from a string
194     private void getNodeConnectorsFromString(String codedNodeConnectors,
195             Set<NodeConnector> sp) {
196         if (codedNodeConnectors == null || codedNodeConnectors.isEmpty()) {
197             return;
198         }
199         if (sp == null) {
200             return;
201         }
202         // codedNodeConnectors = nodeId/a,b,c-m,r-t,y
203         String pieces[] = codedNodeConnectors.split("/");
204         for (Short port : getPortList(pieces[1])) {
205             Node n = Node.fromString(pieces[0]);
206             if (n == null) {
207                 continue;
208             }
209             NodeConnector p = NodeConnectorCreator.createOFNodeConnector(port,
210                     n);
211             if (p == null) {
212                 continue;
213             }
214             sp.add(p);
215         }
216     }
217
218     public Set<NodeConnector> getSubnetNodeConnectors() {
219         Set<NodeConnector> sp = new HashSet<NodeConnector>();
220         if (isGlobal())
221             return sp;
222         for (String str : nodePorts) {
223             getNodeConnectorsFromString(str, sp);
224         }
225         return sp;
226     }
227
228     public Set<NodeConnector> getNodeConnectors(String codedNodeConnectors) {
229         // codedNodeConnectors = nodeId/a,b,c-m,r-t,y
230         Set<NodeConnector> sp = new HashSet<NodeConnector>();
231         getNodeConnectorsFromString(codedNodeConnectors, sp);
232         return sp;
233     }
234
235     public boolean isGlobal() {
236         // If no ports are specified to be part of the domain, then it's a global domain IP
237         return (nodePorts == null || nodePorts.isEmpty());
238     }
239
240     public void addNodeConnectors(String sp) {
241         nodePorts.add(sp);
242     }
243
244     public void removeNodeConnectors(String sp) {
245         nodePorts.remove(sp);
246     }
247
248     @Override
249     public String toString() {
250         return ("SubnetConfig [Description=" + name + ", Subnet=" + subnet
251                 + ", NodeConnectors=" + nodePorts + "]");
252     }
253
254     /**
255      * Implement clonable interface
256      */
257     @Override
258     public SubnetConfig clone() {
259         return new SubnetConfig(this);
260     }
261
262 }