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.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
20 import javax.xml.bind.annotation.XmlRootElement;
21 import javax.xml.bind.annotation.XmlAccessType;
22 import javax.xml.bind.annotation.XmlAccessorType;
23 import javax.xml.bind.annotation.XmlElement;
24 import javax.xml.bind.annotation.XmlAttribute;
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;
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 prettyFields[] = { GUIField.NAME.toString(),
40 GUIField.GATEWAYIP.toString(), GUIField.NODEPORTS.toString() };
42 // Order matters: JSP file expects following fields in the
47 private String subnet; // A.B.C.D/MM Where A.B.C.D is the Default
48 // Gateway IP (L3) or ARP Querier IP (L2
50 private List<String> nodePorts; // datapath ID/port list:
51 // xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
53 public SubnetConfig() {
56 public SubnetConfig(String desc, String sub, List<String> sp) {
62 public String getName() {
66 public List<String> getNodePorts() {
70 public String getSubnet() {
74 public InetAddress getIPnum() {
75 InetAddress ip = null;
77 ip = InetAddress.getByName(subnet.split("/")[0]);
78 } catch (UnknownHostException e1) {
84 public Short getIPMaskLen() {
87 String[] s = subnet.split("/");
88 maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
93 private Set<Short> getPortList(String ports) {
97 * elemArray = ["1" "3" "5-12"]
98 * elem[2] = "5-12" --> limits = ["5" "12"]
99 * portList = [1 3 5 6 7 8 9 10 11 12]
101 Set<Short> portList = new HashSet<Short>();
102 String[] elemArray = ports.split(",");
103 for (String elem : elemArray) {
104 if (elem.contains("-")) {
105 String[] limits = elem.split("-");
106 for (short j = Short.valueOf(limits[0]); j <= Short
107 .valueOf(limits[1]); j++) {
108 portList.add(Short.valueOf(j));
111 portList.add(Short.valueOf(elem));
117 private boolean hasValidIP() {
118 if (subnet != null) {
119 if (NetUtils.isIPv4AddressValid(subnet)) {
121 } else if (NetUtils.isIPv6AddressValid(subnet)) {
128 private boolean hasValidPorts() {
129 for (String portSet : nodePorts) {
130 if (!portSet.contains("/")) {
137 public boolean isValidSwitchPort(String sp) {
138 return sp.contains("/");
141 public boolean isValidConfig() {
142 return hasValidIP() && hasValidPorts();
146 public int hashCode() {
147 return name.hashCode();
151 public boolean equals(Object obj) {
153 * Configuration will be stored in collection only if it is valid
154 * Hence we don't check here for uninitialized fields
160 if (getClass() != obj.getClass())
162 SubnetConfig that = (SubnetConfig) obj;
163 if (this.subnet.equals(that.subnet)) {
169 public static List<String> getGuiFieldsNames() {
170 List<String> fieldList = new ArrayList<String>();
171 for (String str : prettyFields) {
177 //Utility method useful for adding to a passed Set all the
178 //NodeConnectors learnt from a string
179 private void getNodeConnectorsFromString(String codedNodeConnectors,
180 Set<NodeConnector> sp) {
181 if (codedNodeConnectors == null) {
187 // codedNodeConnectors = xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
188 String pieces[] = codedNodeConnectors.split("/");
189 for (Short port : getPortList(pieces[1])) {
190 Node n = Node.fromString(pieces[0]);
194 NodeConnector p = NodeConnectorCreator.createOFNodeConnector(port,
203 public Set<NodeConnector> getSubnetNodeConnectors() {
204 Set<NodeConnector> sp = new HashSet<NodeConnector>();
207 for (String str : nodePorts) {
208 getNodeConnectorsFromString(str, sp);
213 public Set<NodeConnector> getNodeConnectors(String codedNodeConnectors) {
214 // codedNodeConnectors = xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
215 Set<NodeConnector> sp = new HashSet<NodeConnector>();
216 getNodeConnectorsFromString(codedNodeConnectors, sp);
220 public boolean isGlobal() {
221 // If no ports are specified to be part of the domain, then it's a global domain IP
222 return (nodePorts == null || nodePorts.isEmpty());
225 public void addNodeConnectors(String sp) {
229 public void removeNodeConnectors(String sp) {
230 nodePorts.remove(sp);
233 public String toString() {
234 return ("Subnet Config [Description=" + name + " Subnet=" + subnet
235 + " NodeConnectors=" + nodePorts + "]");