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.XmlElement;
23 import javax.xml.bind.annotation.XmlRootElement;
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 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() };
42 // Order matters: JSP file expects following fields in the
50 * A.B.C.D/MM Where A.B.C.D is the Default
51 * Gateway IP (L3) or ARP Querier IP (L2)
54 private String subnet;
57 *<node-id>/a,b,c-m,r-t,y
60 private Set<String> nodePorts;
62 public SubnetConfig() {
65 public SubnetConfig(String desc, String sub, Set<String> sp) {
71 public SubnetConfig(SubnetConfig subnetConfig) {
72 name = subnetConfig.name;
73 subnet = subnetConfig.subnet;
74 nodePorts = new HashSet<String>(subnetConfig.nodePorts);
77 public String getName() {
81 public Set<String> getNodePorts() {
85 public String getSubnet() {
89 public InetAddress getIPnum() {
90 InetAddress ip = null;
92 ip = InetAddress.getByName(subnet.split("/")[0]);
93 } catch (UnknownHostException e1) {
99 public Short getIPMaskLen() {
102 String[] s = subnet.split("/");
103 maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
108 private Set<Short> getPortList(String ports) {
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]
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));
126 portList.add(Short.valueOf(elem));
132 private boolean hasValidIP() {
133 if (subnet != null) {
134 if (NetUtils.isIPv4AddressValid(subnet)) {
136 } else if (NetUtils.isIPv6AddressValid(subnet)) {
143 private boolean hasValidPorts() {
144 for (String portSet : nodePorts) {
145 if (!portSet.contains("/")) {
152 public boolean isValidSwitchPort(String sp) {
153 return sp.contains("/");
156 public boolean isValidConfig() {
157 return hasValidIP() && hasValidPorts();
161 public int hashCode() {
162 return name.hashCode();
166 public boolean equals(Object obj) {
168 * Configuration will be stored in collection only if it is valid
169 * Hence we don't check here for uninitialized fields
175 if (getClass() != obj.getClass())
177 SubnetConfig that = (SubnetConfig) obj;
178 if (this.subnet.equals(that.subnet)) {
184 public static List<String> getGuiFieldsNames() {
185 List<String> fieldList = new ArrayList<String>();
186 for (String str : prettyFields) {
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()) {
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]);
209 NodeConnector p = NodeConnectorCreator.createOFNodeConnector(port,
218 public Set<NodeConnector> getSubnetNodeConnectors() {
219 Set<NodeConnector> sp = new HashSet<NodeConnector>();
222 for (String str : nodePorts) {
223 getNodeConnectorsFromString(str, sp);
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);
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());
240 public void addNodeConnectors(String sp) {
244 public void removeNodeConnectors(String sp) {
245 nodePorts.remove(sp);
249 public String toString() {
250 return ("SubnetConfig [Description=" + name + ", Subnet=" + subnet
251 + ", NodeConnectors=" + nodePorts + "]");
255 * Implement clonable interface
258 public SubnetConfig clone() {
259 return new SubnetConfig(this);