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 private Status validateName() {
131 if (name == null || name.trim().isEmpty()) {
132 return new Status(StatusCode.BADREQUEST, "Invalid name");
134 return new Status(StatusCode.SUCCESS);
137 public Status validate() {
138 Status status = validateName();
139 if (status.isSuccess()) {
140 status = validateSubnetAddress();
141 if (status.isSuccess()) {
142 status = validatePorts(this.nodeConnectors);
148 public static List<String> getGuiFieldsNames() {
149 List<String> fieldList = new ArrayList<String>();
150 for (String str : prettyFields) {
156 public Set<NodeConnector> getNodeConnectors() {
157 return NodeConnector.fromString(this.nodeConnectors);
160 public boolean isGlobal() {
161 // If no ports are specified to be part of the domain, then it's a global domain IP
162 return (nodeConnectors == null || nodeConnectors.isEmpty());
165 public void addNodeConnectors(List<String> nc) {
167 if (nodeConnectors == null) {
168 nodeConnectors = new ArrayList<String>(nc);
170 nodeConnectors.addAll(nc);
175 public void removeNodeConnectors(List<String> nc) {
176 if (nc != null && nodeConnectors != null) {
177 nodeConnectors.removeAll(nc);
182 public String toString() {
183 return ("SubnetConfig [Name=" + name + ", Subnet=" + subnet
184 + ", NodeConnectors=" + nodeConnectors + "]");
188 * Implement clonable interface
191 public SubnetConfig clone() {
192 return new SubnetConfig(this);
196 public int hashCode() {
197 final int prime = 31;
199 result = prime * result + ((name == null) ? 0 : name.hashCode());
200 result = prime * result + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
201 result = prime * result + ((subnet == null) ? 0 : subnet.hashCode());
206 public boolean equals(Object obj) {
213 if (getClass() != obj.getClass()) {
216 SubnetConfig other = (SubnetConfig) obj;
218 if (other.name != null) {
221 } else if (!name.equals(other.name)) {
224 if (nodeConnectors == null) {
225 if (other.nodeConnectors != null) {
228 } else if (!nodeConnectors.equals(other.nodeConnectors)) {
231 if (subnet == null) {
232 if (other.subnet != null) {
235 } else if (!subnet.equals(other.subnet)) {