3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.switchmanager;
12 import java.io.Serializable;
13 import java.lang.reflect.Field;
14 import java.net.Inet6Address;
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
21 import javax.xml.bind.annotation.XmlRootElement;
22 import javax.xml.bind.annotation.XmlAccessType;
23 import javax.xml.bind.annotation.XmlAccessorType;
24 import javax.xml.bind.annotation.XmlElement;
25 import javax.xml.bind.annotation.XmlAttribute;
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.NodeConnectorCreator;
32 * The class represents a subnet configuration.
35 @XmlAccessorType(XmlAccessType.NONE)
36 public class SubnetConfig implements Serializable {
37 //static fields are by default excluded by Gson parser
38 private static final long serialVersionUID = 1L;
39 private static final String regexIP = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
40 + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
41 + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
42 + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])[/](\\d|[12]\\d|3[0-2])$";
43 private static final String prettyFields[] = { GUIField.NAME.toString(),
44 GUIField.GATEWAYIP.toString(), GUIField.NODEPORTS.toString() };
46 // Order matters: JSP file expects following fields in the
51 private String subnet; // A.B.C.D/MM Where A.B.C.D is the Default
52 // Gateway IP (L3) or ARP Querier IP (L2
54 private List<String> nodePorts; // datapath ID/port list:
55 // xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
57 public SubnetConfig() {
60 public SubnetConfig(String desc, String sub, List<String> sp) {
66 public String getName() {
70 public List<String> getNodePorts() {
74 public String getSubnet() {
78 public InetAddress getIPnum() {
79 InetAddress ip = null;
81 ip = InetAddress.getByName(subnet.split("/")[0]);
82 } catch (UnknownHostException e1) {
88 public Short getIPMaskLen() {
91 String[] s = subnet.split("/");
92 maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
97 public boolean isIPv6AddressValid() {
100 String values[] = subnet.split("/");
102 //when given an IP address, InetAddress.getByName validates the ip address
103 InetAddress addr = InetAddress.getByName(values[0]);
104 if (!(addr instanceof Inet6Address)) {
107 } catch (UnknownHostException ex) {
110 if (values.length >= 2) {
111 int prefix = Integer.valueOf(values[1]);
112 if ((prefix < 0) || (prefix > 128)) {
119 private Set<Short> getPortList(String ports) {
123 * elemArray = ["1" "3" "5-12"]
124 * elem[2] = "5-12" --> limits = ["5" "12"]
125 * portList = [1 3 5 6 7 8 9 10 11 12]
127 Set<Short> portList = new HashSet<Short>();
128 String[] elemArray = ports.split(",");
129 for (String elem : elemArray) {
130 if (elem.contains("-")) {
131 String[] limits = elem.split("-");
132 for (short j = Short.valueOf(limits[0]); j <= Short
133 .valueOf(limits[1]); j++) {
134 portList.add(Short.valueOf(j));
137 portList.add(Short.valueOf(elem));
143 private boolean hasValidIP() {
144 if (subnet != null) {
145 if (subnet.matches(regexIP)) {
147 } else if (isIPv6AddressValid()) {
154 private boolean hasValidPorts() {
155 for (String portSet : nodePorts) {
156 if (!portSet.contains("/")) {
163 public boolean isValidSwitchPort(String sp) {
164 return sp.contains("/");
167 public boolean isValidConfig() {
168 return hasValidIP() && hasValidPorts();
172 public int hashCode() {
173 return name.hashCode();
177 public boolean equals(Object obj) {
179 * Configuration will be stored in collection only if it is valid
180 * Hence we don't check here for uninitialized fields
186 if (getClass() != obj.getClass())
188 SubnetConfig that = (SubnetConfig) obj;
189 if (this.subnet.equals(that.subnet)) {
195 public static List<String> getFieldsNames() {
196 List<String> fieldList = new ArrayList<String>();
197 for (Field fld : SubnetConfig.class.getDeclaredFields()) {
198 fieldList.add(fld.getName());
200 //remove the four static fields
201 for (short i = 0; i < 3; i++) {
207 public static List<String> getGuiFieldsNames() {
208 List<String> fieldList = new ArrayList<String>();
209 for (String str : prettyFields) {
215 //Utility method useful for adding to a passed Set all the
216 //NodeConnectors learnt from a string
217 private void getNodeConnectorsFromString(String codedNodeConnectors,
218 Set<NodeConnector> sp) {
219 if (codedNodeConnectors == null) {
225 // codedNodeConnectors = xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
226 String pieces[] = codedNodeConnectors.split("/");
227 for (Short port : getPortList(pieces[1])) {
228 Node n = Node.fromString(pieces[0]);
232 NodeConnector p = NodeConnectorCreator.createOFNodeConnector(port,
241 public Set<NodeConnector> getSubnetNodeConnectors() {
242 Set<NodeConnector> sp = new HashSet<NodeConnector>();
245 for (String str : nodePorts) {
246 getNodeConnectorsFromString(str, sp);
251 public Set<NodeConnector> getNodeConnectors(String codedNodeConnectors) {
252 // codedNodeConnectors = xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
253 Set<NodeConnector> sp = new HashSet<NodeConnector>();
254 getNodeConnectorsFromString(codedNodeConnectors, sp);
258 public boolean isGlobal() {
259 // If no ports are specified to be part of the domain, then it's a global domain IP
260 return (nodePorts == null || nodePorts.isEmpty());
263 public void addNodeConnectors(String sp) {
267 public void removeNodeConnectors(String sp) {
268 nodePorts.remove(sp);
271 public String toString() {
272 return ("Subnet Config [Description=" + name + " Subnet=" + subnet
273 + " NodeConnectors=" + nodePorts + "]");