2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.switchmanager;
11 import java.io.Serializable;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlRootElement;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.packet.BitBufferHelper;
25 import org.opendaylight.controller.sal.utils.GUIField;
26 import org.opendaylight.controller.sal.utils.NetUtils;
27 import org.opendaylight.controller.sal.utils.Status;
28 import org.opendaylight.controller.sal.utils.StatusCode;
31 * The class represents a subnet configuration.
34 @XmlAccessorType(XmlAccessType.NONE)
35 public class SubnetConfig implements Cloneable, Serializable {
36 private static final long serialVersionUID = 1L;
37 private static final String prettyFields[] = { GUIField.NAME.toString(), GUIField.GATEWAYIP.toString(),
38 GUIField.NODEPORTS.toString() };
46 * A.B.C.D/MM Where A.B.C.D is the Default Gateway IP (L3) or ARP Querier IP
50 private String subnet;
52 * Set of node connectors in the format: Port Type|Port Id@Node Type|Node Id
55 private List<String> nodeConnectors;
57 public SubnetConfig() {
60 public SubnetConfig(String name, String subnet, List<String> nodeConnectors) {
63 this.nodeConnectors = nodeConnectors;
66 public SubnetConfig(SubnetConfig subnetConfig) {
67 name = subnetConfig.name;
68 subnet = subnetConfig.subnet;
69 nodeConnectors = (subnetConfig.nodeConnectors == null) ? null : new ArrayList<String>(
70 subnetConfig.nodeConnectors);
73 public String getName() {
77 public List<String> getNodePorts() {
78 return (nodeConnectors == null) ? new ArrayList<String>(0) : new ArrayList<String>(nodeConnectors);
81 public String getSubnet() {
85 public InetAddress getIPAddress() {
86 InetAddress ip = null;
88 ip = InetAddress.getByName(subnet.split("/")[0]);
89 } catch (UnknownHostException e1) {
95 public Short getIPMaskLen() {
97 String[] s = subnet.split("/");
100 maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
101 } catch (NumberFormatException e) {
107 private Status validateSubnetAddress() {
108 if (!NetUtils.isIPAddressValid(subnet)) {
109 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s",
112 if ((this.getIPMaskLen() == 0) || (this.getIPMaskLen() == 32)) {
113 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid mask: /%s",
114 this.getIPMaskLen()));
117 //checks that address doesn't start with 0 or 255
118 String address = subnet.split("/")[0];
119 if (address.startsWith("0.") || address.startsWith("255.")) {
120 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s", address));
123 byte[] bytePrefix = NetUtils.getSubnetPrefix(this.getIPAddress(), this.getIPMaskLen()).getAddress();
124 long prefix = BitBufferHelper.getLong(bytePrefix);
126 return new Status(StatusCode.BADREQUEST, "Invalid network source address: subnet zero");
129 //check that host is not set to all 0's or 1's
130 long hostAddress = BitBufferHelper.getLong(this.getIPAddress().getAddress()) - prefix;
131 if (hostAddress == 0 || hostAddress == Math.pow(2, 32-this.getIPMaskLen()) - 1) {
132 return new Status(StatusCode.BADREQUEST, String.format("Invalid subnet gateway address: /%s", subnet));
135 return new Status(StatusCode.SUCCESS);
138 public static Status validatePorts(List<String> nodeConnectors) {
139 if (nodeConnectors != null) {
140 for (String port : nodeConnectors) {
141 if (null == NodeConnector.fromString(port)) {
142 return new Status(StatusCode.BADREQUEST,
143 "Invalid Subnet configuration: Not parsable node connector: " + port);
147 return new Status(StatusCode.SUCCESS);
150 private Status validateName() {
151 if (name == null || name.trim().isEmpty()) {
152 return new Status(StatusCode.BADREQUEST, "Invalid name");
154 return new Status(StatusCode.SUCCESS);
157 public Status validate() {
158 Status status = validateName();
159 if (status.isSuccess()) {
160 status = validateSubnetAddress();
161 if (status.isSuccess()) {
162 status = validatePorts(this.nodeConnectors);
168 public static List<String> getGuiFieldsNames() {
169 List<String> fieldList = new ArrayList<String>();
170 for (String str : prettyFields) {
176 public Set<NodeConnector> getNodeConnectors() {
177 return NodeConnector.fromString(this.nodeConnectors);
180 public boolean isGlobal() {
181 // If no ports are specified to be part of the domain, then it's a
183 return (nodeConnectors == null || nodeConnectors.isEmpty());
186 public void addNodeConnectors(List<String> nc) {
188 if (nodeConnectors == null) {
189 nodeConnectors = new ArrayList<String>(nc);
191 nodeConnectors.addAll(nc);
196 public void removeNodeConnectors(List<String> nc) {
197 if (nc != null && nodeConnectors != null) {
198 nodeConnectors.removeAll(nc);
203 public String toString() {
204 return ("SubnetConfig [Name=" + name + ", Subnet=" + subnet + ", NodeConnectors=" + nodeConnectors + "]");
208 * Implement clonable interface
211 public SubnetConfig clone() {
212 return new SubnetConfig(this);
216 public int hashCode() {
217 final int prime = 31;
219 result = prime * result + ((name == null) ? 0 : name.hashCode());
220 result = prime * result + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
221 result = prime * result + ((subnet == null) ? 0 : subnet.hashCode());
226 public boolean equals(Object obj) {
233 if (getClass() != obj.getClass()) {
236 SubnetConfig other = (SubnetConfig) obj;
238 if (other.name != null) {
241 } else if (!name.equals(other.name)) {
244 if (nodeConnectors == null) {
245 if (other.nodeConnectors != null) {
248 } else if (!nodeConnectors.equals(other.nodeConnectors)) {
251 if (subnet == null) {
252 if (other.subnet != null) {
255 } else if (!subnet.equals(other.subnet)) {