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.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
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;
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;
33 * The class represents a subnet configuration.
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() };
43 // Order matters: JSP file expects following fields in the
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
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
54 public SubnetConfig() {
57 public SubnetConfig(String desc, String sub, List<String> sp) {
63 public String getName() {
67 public List<String> getNodePorts() {
71 public String getSubnet() {
75 public InetAddress getIPnum() {
76 InetAddress ip = null;
78 ip = InetAddress.getByName(subnet.split("/")[0]);
79 } catch (UnknownHostException e1) {
85 public Short getIPMaskLen() {
88 String[] s = subnet.split("/");
89 maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
94 private Set<Short> getPortList(String ports) {
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]
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));
112 portList.add(Short.valueOf(elem));
118 private boolean hasValidIP() {
119 if (subnet != null) {
120 if (NetUtils.isIPv4AddressValid(subnet)) {
122 } else if (NetUtils.isIPv6AddressValid(subnet)) {
129 private boolean hasValidPorts() {
130 for (String portSet : nodePorts) {
131 if (!portSet.contains("/")) {
138 public boolean isValidSwitchPort(String sp) {
139 return sp.contains("/");
142 public boolean isValidConfig() {
143 return hasValidIP() && hasValidPorts();
147 public int hashCode() {
148 return name.hashCode();
152 public boolean equals(Object obj) {
154 * Configuration will be stored in collection only if it is valid
155 * Hence we don't check here for uninitialized fields
161 if (getClass() != obj.getClass())
163 SubnetConfig that = (SubnetConfig) obj;
164 if (this.subnet.equals(that.subnet)) {
170 public static List<String> getGuiFieldsNames() {
171 List<String> fieldList = new ArrayList<String>();
172 for (String str : prettyFields) {
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) {
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]);
195 NodeConnector p = NodeConnectorCreator.createOFNodeConnector(port,
204 public Set<NodeConnector> getSubnetNodeConnectors() {
205 Set<NodeConnector> sp = new HashSet<NodeConnector>();
208 for (String str : nodePorts) {
209 getNodeConnectorsFromString(str, sp);
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);
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());
226 public void addNodeConnectors(String sp) {
230 public void removeNodeConnectors(String sp) {
231 nodePorts.remove(sp);
234 public String toString() {
235 return ("Subnet Config [Description=" + name + " Subnet=" + subnet
236 + " NodeConnectors=" + nodePorts + "]");