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.configuration.ConfigurationObject;
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 extends ConfigurationObject implements Cloneable, Serializable {
37 private static final long serialVersionUID = 1L;
38 private static final String prettyFields[] = { GUIField.NAME.toString(), GUIField.GATEWAYIP.toString(),
39 GUIField.NODEPORTS.toString() };
47 * A.B.C.D/MM Where A.B.C.D is the Default Gateway IP (L3) or ARP Querier IP
51 private String subnet;
53 * Set of node connectors in the format: Port Type|Port Id@Node Type|Node Id
56 private List<String> nodeConnectors;
58 public SubnetConfig() {
61 public SubnetConfig(String name, String subnet, List<String> nodeConnectors) {
64 this.nodeConnectors = nodeConnectors;
67 public SubnetConfig(SubnetConfig subnetConfig) {
68 name = subnetConfig.name;
69 subnet = subnetConfig.subnet;
70 nodeConnectors = (subnetConfig.nodeConnectors == null) ? null : new ArrayList<String>(
71 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("/");
101 maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
102 } catch (NumberFormatException e) {
108 private Status validateSubnetAddress() {
109 if (!NetUtils.isIPAddressValid(subnet)) {
110 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s",
113 if ((this.getIPMaskLen() == 0) || (this.getIPMaskLen() == 32)) {
114 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid mask: /%s",
115 this.getIPMaskLen()));
118 //checks that address doesn't start with 0 or 255
119 String address = subnet.split("/")[0];
120 if (address.startsWith("0.") || address.startsWith("255.")) {
121 return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s", address));
124 byte[] bytePrefix = NetUtils.getSubnetPrefix(this.getIPAddress(), this.getIPMaskLen()).getAddress();
125 long prefix = BitBufferHelper.getLong(bytePrefix);
127 return new Status(StatusCode.BADREQUEST, "Invalid network source address: subnet zero");
130 //check that host is not set to all 0's or 1's
131 long hostAddress = BitBufferHelper.getLong(this.getIPAddress().getAddress()) - prefix;
132 if (hostAddress == 0 || hostAddress == Math.pow(2, 32-this.getIPMaskLen()) - 1) {
133 return new Status(StatusCode.BADREQUEST, String.format("Invalid subnet gateway address: /%s", subnet));
136 return new Status(StatusCode.SUCCESS);
139 public static Status validatePorts(List<String> nodeConnectors) {
140 if (nodeConnectors != null) {
141 for (String port : nodeConnectors) {
142 if (null == NodeConnector.fromString(port)) {
143 return new Status(StatusCode.BADREQUEST,
144 "Invalid Subnet configuration: Not parsable node connector: " + port);
148 return new Status(StatusCode.SUCCESS);
151 private Status validateName() {
152 if (!isValidResourceName(name)) {
153 return new Status(StatusCode.BADREQUEST, "Invalid name");
155 return new Status(StatusCode.SUCCESS);
158 public Status validate() {
159 Status status = validateName();
160 if (status.isSuccess()) {
161 status = validateSubnetAddress();
162 if (status.isSuccess()) {
163 status = validatePorts(this.nodeConnectors);
169 public static List<String> getGuiFieldsNames() {
170 List<String> fieldList = new ArrayList<String>();
171 for (String str : prettyFields) {
177 public Set<NodeConnector> getNodeConnectors() {
178 return NodeConnector.fromString(this.nodeConnectors);
181 public boolean isGlobal() {
182 // If no ports are specified to be part of the domain, then it's a
184 return (nodeConnectors == null || nodeConnectors.isEmpty());
187 public void addNodeConnectors(List<String> nc) {
189 if (nodeConnectors == null) {
190 nodeConnectors = new ArrayList<String>(nc);
192 nodeConnectors.addAll(nc);
197 public void removeNodeConnectors(List<String> nc) {
198 if (nc != null && nodeConnectors != null) {
199 nodeConnectors.removeAll(nc);
204 public String toString() {
205 return ("SubnetConfig [Name=" + name + ", Subnet=" + subnet + ", NodeConnectors=" + nodeConnectors + "]");
209 * Implement clonable interface
212 public SubnetConfig clone() {
213 return new SubnetConfig(this);
217 public int hashCode() {
218 final int prime = 31;
220 result = prime * result + ((name == null) ? 0 : name.hashCode());
221 result = prime * result + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
222 result = prime * result + ((subnet == null) ? 0 : subnet.hashCode());
227 public boolean equals(Object obj) {
234 if (getClass() != obj.getClass()) {
237 SubnetConfig other = (SubnetConfig) obj;
239 if (other.name != null) {
242 } else if (!name.equals(other.name)) {
245 if (nodeConnectors == null) {
246 if (other.nodeConnectors != null) {
249 } else if (!nodeConnectors.equals(other.nodeConnectors)) {
252 if (subnet == null) {
253 if (other.subnet != null) {
256 } else if (!subnet.equals(other.subnet)) {