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.List;
19 import javax.xml.bind.annotation.XmlAccessType;
20 import javax.xml.bind.annotation.XmlAccessorType;
21 import javax.xml.bind.annotation.XmlElement;
22 import javax.xml.bind.annotation.XmlRootElement;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.packet.BitBufferHelper;
26 import org.opendaylight.controller.sal.utils.GUIField;
27 import org.opendaylight.controller.sal.utils.NetUtils;
28 import org.opendaylight.controller.sal.utils.Status;
29 import org.opendaylight.controller.sal.utils.StatusCode;
32 * The class represents a subnet configuration.
35 @XmlAccessorType(XmlAccessType.NONE)
36 public class SubnetConfig implements Cloneable, Serializable {
37 private static final long serialVersionUID = 1L;
38 private static final String prettyFields[] = { GUIField.NAME.toString(),
39 GUIField.GATEWAYIP.toString(), GUIField.NODEPORTS.toString() };
47 * A.B.C.D/MM Where A.B.C.D is the Default
48 * Gateway IP (L3) or ARP Querier IP (L2)
51 private String subnet;
53 * Set of node connectors in the format:
54 * Port Type|Port Id@Node Type|Node Id
57 private List<String> nodeConnectors;
59 public SubnetConfig() {
62 public SubnetConfig(String name, String subnet, List<String> nodeConnectors) {
65 this.nodeConnectors = nodeConnectors;
68 public SubnetConfig(SubnetConfig subnetConfig) {
69 name = subnetConfig.name;
70 subnet = subnetConfig.subnet;
71 nodeConnectors = (subnetConfig.nodeConnectors == null) ? null : new ArrayList<String>(subnetConfig.nodeConnectors);
74 public String getName() {
78 public List<String> getNodePorts() {
79 return (nodeConnectors == null) ? new ArrayList<String>(0) : new ArrayList<String>(nodeConnectors);
82 public String getSubnet() {
86 public InetAddress getIPAddress() {
87 InetAddress ip = null;
89 ip = InetAddress.getByName(subnet.split("/")[0]);
90 } catch (UnknownHostException e1) {
96 public Short getIPMaskLen() {
98 String[] s = subnet.split("/");
99 maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
103 private Status validateSubnetAddress() {
104 if (!NetUtils.isIPAddressValid(subnet)) {
105 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s", subnet));
107 if((this.getIPMaskLen() == 0) || (this.getIPMaskLen() == 32)) {
108 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid mask: /%s", this.getIPMaskLen()));
110 byte[] bytePrefix = NetUtils.getSubnetPrefix(this.getIPAddress(), this.getIPMaskLen()).getAddress();
111 long prefix = BitBufferHelper.getLong(bytePrefix);
113 return new Status(StatusCode.BADREQUEST, "Invalid network source address: subnet zero");
115 return new Status(StatusCode.SUCCESS);
118 public static Status validatePorts(List<String> nodeConnectors) {
119 if (nodeConnectors != null) {
120 for (String port : nodeConnectors) {
121 if (null == NodeConnector.fromString(port)) {
122 return new Status(StatusCode.BADREQUEST,
123 "Invalid Subnet configuration: Not parsable node connector: " + port);
127 return new Status(StatusCode.SUCCESS);
130 public Status validate() {
131 Status status = validateSubnetAddress();
132 if (status.isSuccess()) {
133 status = validatePorts(this.nodeConnectors);
138 public static List<String> getGuiFieldsNames() {
139 List<String> fieldList = new ArrayList<String>();
140 for (String str : prettyFields) {
146 public Set<NodeConnector> getNodeConnectors() {
147 return NodeConnector.fromString(this.nodeConnectors);
150 public boolean isGlobal() {
151 // If no ports are specified to be part of the domain, then it's a global domain IP
152 return (nodeConnectors == null || nodeConnectors.isEmpty());
155 public void addNodeConnectors(List<String> nc) {
157 if (nodeConnectors == null) {
158 nodeConnectors = new ArrayList<String>(nc);
160 nodeConnectors.addAll(nc);
165 public void removeNodeConnectors(List<String> nc) {
166 if (nc != null && nodeConnectors != null) {
167 nodeConnectors.removeAll(nc);
172 public String toString() {
173 return ("SubnetConfig [Name=" + name + ", Subnet=" + subnet
174 + ", NodeConnectors=" + nodeConnectors + "]");
178 * Implement clonable interface
181 public SubnetConfig clone() {
182 return new SubnetConfig(this);
186 public int hashCode() {
187 final int prime = 31;
189 result = prime * result + ((name == null) ? 0 : name.hashCode());
190 result = prime * result + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
191 result = prime * result + ((subnet == null) ? 0 : subnet.hashCode());
196 public boolean equals(Object obj) {
203 if (getClass() != obj.getClass()) {
206 SubnetConfig other = (SubnetConfig) obj;
208 if (other.name != null) {
211 } else if (!name.equals(other.name)) {
214 if (nodeConnectors == null) {
215 if (other.nodeConnectors != null) {
218 } else if (!nodeConnectors.equals(other.nodeConnectors)) {
221 if (subnet == null) {
222 if (other.subnet != null) {
225 } else if (!subnet.equals(other.subnet)) {