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