Merge "Added DELETE support for Bridge and Port resources"
[controller.git] / opendaylight / switchmanager / api / src / main / java / org / opendaylight / controller / switchmanager / SubnetConfig.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.switchmanager;
11
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;
17 import java.util.Set;
18
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;
23
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;
30
31 /**
32  * The class represents a subnet configuration.
33  */
34 @XmlRootElement
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() };
40
41     /**
42      * Name of the subnet
43      */
44     @XmlElement
45     private String name;
46     /**
47      * A.B.C.D/MM  Where A.B.C.D is the Default
48      * Gateway IP (L3) or ARP Querier IP (L2)
49      */
50     @XmlElement
51     private String subnet;
52     /**
53      * Set of node connectors in the format:
54      * Port Type|Port Id@Node Type|Node Id
55      */
56     @XmlElement
57     private List<String> nodeConnectors;
58
59     public SubnetConfig() {
60     }
61
62     public SubnetConfig(String name, String subnet, List<String> nodeConnectors) {
63         this.name = name;
64         this.subnet = subnet;
65         this.nodeConnectors = nodeConnectors;
66     }
67
68     public SubnetConfig(SubnetConfig subnetConfig) {
69         name = subnetConfig.name;
70         subnet = subnetConfig.subnet;
71         nodeConnectors = (subnetConfig.nodeConnectors == null) ? null : new ArrayList<String>(subnetConfig.nodeConnectors);
72     }
73
74     public String getName() {
75         return name;
76     }
77
78     public List<String> getNodePorts() {
79         return (nodeConnectors == null) ? new ArrayList<String>(0) : new ArrayList<String>(nodeConnectors);
80     }
81
82     public String getSubnet() {
83         return subnet;
84     }
85
86     public InetAddress getIPAddress() {
87         InetAddress ip = null;
88         try {
89             ip = InetAddress.getByName(subnet.split("/")[0]);
90         } catch (UnknownHostException e1) {
91             return null;
92         }
93         return ip;
94     }
95
96     public Short getIPMaskLen() {
97         Short maskLen = 0;
98         String[] s = subnet.split("/");
99         maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
100         return maskLen;
101     }
102
103     private Status validateSubnetAddress() {
104         if (!NetUtils.isIPAddressValid(subnet)) {
105             return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid address: %s", subnet));
106         }
107         if((this.getIPMaskLen() == 0) || (this.getIPMaskLen() == 32)) {
108             return new Status(StatusCode.BADREQUEST, String.format("Invalid Subnet configuration: Invalid mask: /%s", this.getIPMaskLen()));
109         }
110         byte[] bytePrefix = NetUtils.getSubnetPrefix(this.getIPAddress(), this.getIPMaskLen()).getAddress();
111         long prefix = BitBufferHelper.getLong(bytePrefix);
112         if (prefix == 0) {
113             return new Status(StatusCode.BADREQUEST, "Invalid network source address: subnet zero");
114         }
115         return new Status(StatusCode.SUCCESS);
116     }
117
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);
124                 }
125             }
126         }
127         return new Status(StatusCode.SUCCESS);
128     }
129
130     private Status validateName() {
131         if (name == null || name.trim().isEmpty()) {
132             return new Status(StatusCode.BADREQUEST, "Invalid name");
133         }
134         return new Status(StatusCode.SUCCESS);
135     }
136
137     public Status validate() {
138         Status status = validateName();
139         if (status.isSuccess()) {
140             status = validateSubnetAddress();
141             if (status.isSuccess()) {
142                 status = validatePorts(this.nodeConnectors);
143             }
144         }
145         return status;
146     }
147
148     public static List<String> getGuiFieldsNames() {
149         List<String> fieldList = new ArrayList<String>();
150         for (String str : prettyFields) {
151             fieldList.add(str);
152         }
153         return fieldList;
154     }
155
156     public Set<NodeConnector> getNodeConnectors() {
157         return NodeConnector.fromString(this.nodeConnectors);
158     }
159
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());
163     }
164
165     public void addNodeConnectors(List<String> nc) {
166         if (nc != null) {
167             if (nodeConnectors == null) {
168                 nodeConnectors = new ArrayList<String>(nc);
169             } else {
170                 nodeConnectors.addAll(nc);
171             }
172         }
173     }
174
175     public void removeNodeConnectors(List<String> nc) {
176         if (nc != null && nodeConnectors != null) {
177             nodeConnectors.removeAll(nc);
178         }
179     }
180
181     @Override
182     public String toString() {
183         return ("SubnetConfig [Name=" + name + ", Subnet=" + subnet
184                 + ", NodeConnectors=" + nodeConnectors + "]");
185     }
186
187     /**
188      * Implement clonable interface
189      */
190     @Override
191     public SubnetConfig clone() {
192         return new SubnetConfig(this);
193     }
194
195     @Override
196     public int hashCode() {
197         final int prime = 31;
198         int result = 1;
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());
202         return result;
203     }
204
205     @Override
206     public boolean equals(Object obj) {
207         if (this == obj) {
208             return true;
209         }
210         if (obj == null) {
211             return false;
212         }
213         if (getClass() != obj.getClass()) {
214             return false;
215         }
216         SubnetConfig other = (SubnetConfig) obj;
217         if (name == null) {
218             if (other.name != null) {
219                 return false;
220             }
221         } else if (!name.equals(other.name)) {
222             return false;
223         }
224         if (nodeConnectors == null) {
225             if (other.nodeConnectors != null) {
226                 return false;
227             }
228         } else if (!nodeConnectors.equals(other.nodeConnectors)) {
229             return false;
230         }
231         if (subnet == null) {
232             if (other.subnet != null) {
233                 return false;
234             }
235         } else if (!subnet.equals(other.subnet)) {
236             return false;
237         }
238         return true;
239     }
240 }