Checkstyle enforcer
[controller.git] / opendaylight / forwarding / staticrouting / src / main / java / org / opendaylight / controller / forwarding / staticrouting / StaticRouteConfig.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.forwarding.staticrouting;
11
12 import java.io.Serializable;
13 import java.lang.reflect.Field;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.opendaylight.controller.sal.utils.GUIField;
22 import org.opendaylight.controller.sal.utils.Status;
23 import org.opendaylight.controller.sal.utils.StatusCode;
24
25 /**
26  * This class defines all the necessary configuration information for a static route.
27  */
28 public class StaticRouteConfig implements Serializable {
29     private static final long serialVersionUID = 1L;
30     private static final String regexSubnet = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
31             + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
32             + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
33             + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])[/](\\d|[12]\\d|3[0-2])$";
34     private static final String regexIP = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
35             + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
36             + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
37             + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
38     private static final String regexDatapathID = "^([0-9a-fA-F]{1,2}[:-]){7}[0-9a-fA-F]{1,2}$";
39     private static final String regexDatapathIDLong = "^[0-9a-fA-F]{1,16}$";
40     private static final String prettyFields[] = { GUIField.NAME.toString(),
41             GUIField.STATICROUTE.toString(), GUIField.NEXTHOP.toString() };
42     private transient String nextHopType; // Ignoring NextHopType for now. Supporting just the next-hop IP-Address feature for now.
43     // Order matters: JSP file expects following fields in the following order
44     private String name;
45     private String staticRoute; // A.B.C.D/MM  Where A.B.C.D is the Default Gateway IP (L3) or ARP Querier IP (L2)
46     private String nextHop; // NextHop IP-Address (or) datapath ID/port list: xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
47
48     /**
49      * Create a static route configuration  with no specific information.
50      */
51     public StaticRouteConfig() {
52         super();
53         nextHopType = StaticRoute.NextHopType.IPADDRESS.toString();
54     }
55
56     /**
57      * Create a static route configuration with all the information.
58      * @param name The name (String) of the static route config
59      * @param staticRoute The string representation of the route. e.g. 192.168.1.1/24
60      * @param nextHop The string representation of the next hop IP address. e.g. 10.10.1.1
61      */
62     public StaticRouteConfig(String name, String staticRoute, String nextHop) {
63         super();
64         this.name = name;
65         this.staticRoute = staticRoute;
66         this.nextHop = nextHop;
67     }
68
69     /**
70      * Get the name of the StaticRouteConfig.
71      * @return: The name
72      */
73     public String getName() {
74         return name;
75     }
76
77     /**
78      * Set the name of the StaticRouteConfig.
79      * @param name The name to set
80      */
81     public void setName(String name) {
82         this.name = name;
83     }
84
85     /**
86      * Get the string representation of the static route.
87      * @return The string representation of the static route
88      */
89     public String getStaticRoute() {
90         return staticRoute;
91     }
92
93     /**
94      * Set the static route of the StaticRouteConfig.
95      * @param staticRoute The string representation of the static route
96      */
97     public void setStaticRoute(String staticRoute) {
98         this.staticRoute = staticRoute;
99     }
100
101     /**
102      * Get the string representation of the next hop address type.
103      * @return The string representation of the next hop address type
104      */
105     public String getNextHopType() {
106         if (nextHopType == null)
107             return StaticRoute.NextHopType.IPADDRESS.toString();
108         return nextHopType;
109     }
110
111     /**
112      * Set the next hop address type.
113      * @param nextHopType The string representation of the next hop address type
114      */
115     public void setNextHopType(String nextHopType) {
116         this.nextHopType = nextHopType;
117     }
118
119     /**
120      * Get all the supported next hop address types.
121      * @return The list of supported next hop address types
122      */
123     public static List<String> getSupportedNextHopTypes() {
124         List<String> s = new ArrayList<String>();
125         for (StaticRoute.NextHopType nh : StaticRoute.NextHopType.values()) {
126             s.add(nh.toString());
127         }
128         return s;
129     }
130
131     /**
132      * Get the next hop address
133      * @return The string represenation of the next hop address
134      */
135     public String getNextHop() {
136         return nextHop;
137     }
138
139     /**
140      * Set the next hop address.
141      * @param nextHop: The string representation of the next hop address to be set
142      */
143     public void setNextHop(String nextHop) {
144         this.nextHop = nextHop;
145     }
146
147     /**
148      * Return a string with text indicating if the config is valid.
149      * @return SUCCESS if the config is valid
150      */
151     public Status isValid() {
152         if ((name == null) || (name.trim().length() < 1)) {
153             return new Status(StatusCode.BADREQUEST,
154                         "Invalid Static Route name");
155         }
156         if (!isValidStaticRouteEntry()) {
157             return new Status(StatusCode.BADREQUEST,
158                         "Invalid Static Route entry. Please use the " +
159                         "IPAddress/mask format. Default gateway " +
160                         "(0.0.0.0/0) is NOT supported.");
161         }
162         if (!isValidNextHop()) {
163             return new Status(StatusCode.BADREQUEST,
164                         "Invalid NextHop IP Address configuration. " +
165                                         "Please use the X.X.X.X format.");
166         }
167
168         return new Status(StatusCode.SUCCESS, null);
169     }
170
171     private boolean isValidAddress(String address) {
172         if ((address != null) && address.matches(regexIP)) {
173             return true;
174         }
175         return false;
176     }
177
178     private boolean isValidStaticRouteEntry() {
179         if ((staticRoute != null) && staticRoute.matches(regexSubnet)) {
180             return true;
181         }
182         return false;
183     }
184
185     private boolean isValidNextHop() {
186         if (getNextHopType().equalsIgnoreCase(
187                 StaticRoute.NextHopType.IPADDRESS.toString())) {
188             return isValidNextHopIP();
189         } else if (getNextHopType().equalsIgnoreCase(
190                 StaticRoute.NextHopType.SWITCHPORT.toString())) {
191             return isValidSwitchId();
192         }
193         return false;
194     }
195
196     private boolean isValidNextHopIP() {
197         return isValidAddress(nextHop);
198     }
199
200     private boolean isValidSwitchId(String switchId) {
201         return (switchId != null && (switchId.matches(regexDatapathID) || switchId
202                 .matches(regexDatapathIDLong)));
203     }
204
205     private boolean isValidSwitchId() {
206         if (getNextHopType().equalsIgnoreCase(
207                 StaticRoute.NextHopType.SWITCHPORT.toString())) {
208             String pieces[] = nextHop.split("/");
209             if (pieces.length < 2)
210                 return false;
211             return isValidSwitchId(pieces[0]);
212         }
213         return false;
214     }
215
216     /**
217      * Return the IP address of the static route.
218      * @return The IP address
219      */
220     public InetAddress getStaticRouteIP() {
221         if (!isValidStaticRouteEntry())
222             return null;
223         InetAddress ip = null;
224         try {
225             ip = InetAddress.getByName(staticRoute.split("/")[0]);
226         } catch (UnknownHostException e1) {
227             return null;
228         }
229         return ip;
230     }
231
232     /**
233      * Return the bit-mask length of the static route.
234      * @return The bit-mask length
235      */
236     public Short getStaticRouteMask() {
237         Short maskLen = 0;
238         if (isValidStaticRouteEntry()) {
239             String[] s = staticRoute.split("/");
240             maskLen = (s.length == 2) ? Short.valueOf(s[1]) : 32;
241         }
242         return maskLen;
243     }
244
245     /**
246      * Return the IP address of the next hop.
247      * @return the IP address
248      */
249     public InetAddress getNextHopIP() {
250         if ((getNextHopType()
251                 .equalsIgnoreCase(StaticRoute.NextHopType.IPADDRESS.toString()))
252                 && isValidNextHopIP()) {
253             InetAddress ip = null;
254             try {
255                 ip = InetAddress.getByName(nextHop);
256             } catch (UnknownHostException e1) {
257                 return null;
258             }
259             return ip;
260         }
261         return null;
262     }
263
264 /**
265  * Return the switch ID and the port ID of the next hop address.
266  * @return The switchID (Long) and PortID (Short) in the map
267  */
268     public Map<Long, Short> getNextHopSwitchPorts() {
269         // codedSwitchPorts = xx:xx:xx:xx:xx:xx:xx:xx/port-number
270         if (getNextHopType().equalsIgnoreCase(
271                 StaticRoute.NextHopType.SWITCHPORT.toString())) {
272             Map<Long, Short> sp = new HashMap<Long, Short>(1);
273             String pieces[] = nextHop.split("/");
274             sp.put(getSwitchIDLong(pieces[0]), Short.valueOf(pieces[1]));
275             return sp;
276         }
277         return null;
278     }
279
280     private long getSwitchIDLong(String switchId) {
281         int radix = 16;
282         String switchString = "0";
283
284         if (isValidSwitchId(switchId)) {
285             if (switchId.contains(":")) {
286                 // Handle the 00:00:AA:BB:CC:DD:EE:FF notation
287                 switchString = switchId.replace(":", "");
288             } else if (switchId.contains("-")) {
289                 // Handle the 00-00-AA-BB-CC-DD-EE-FF notation
290                 switchString = switchId.replace("-", "");
291             } else {
292                 // Handle the 0123456789ABCDEF notation
293                 switchString = switchId;
294             }
295         }
296         return Long.parseLong(switchString, radix);
297     }
298
299     /**
300      * Return all the field names of the config.
301      * @return The list containing all the field names
302      */
303     public static List<String> getFieldsNames() {
304         List<String> fieldList = new ArrayList<String>();
305         for (Field fld : StaticRouteConfig.class.getDeclaredFields()) {
306             fieldList.add(fld.getName());
307         }
308         //remove the 6 static fields + NextHopType
309         for (short i = 0; i < 7; i++) {
310             fieldList.remove(0);
311         }
312         return fieldList;
313     }
314
315     /**
316      * Return all the GUI field names of the config.
317      * @return The list containing all the GUI field names
318      */
319     public static List<String> getGuiFieldsNames() {
320         List<String> fieldList = new ArrayList<String>();
321         for (String str : prettyFields) {
322             fieldList.add(str);
323         }
324         return fieldList;
325     }
326
327     @Override
328     public int hashCode() {
329         final int prime = 31;
330         int result = 1;
331         result = prime * result + ((name == null) ? 0 : name.hashCode());
332         result = prime * result + ((nextHop == null) ? 0 : nextHop.hashCode());
333         result = prime * result
334                 + ((staticRoute == null) ? 0 : staticRoute.hashCode());
335         return result;
336     }
337
338     @Override
339     public boolean equals(Object obj) {
340         if (this == obj)
341             return true;
342         if (obj == null)
343             return false;
344         if (getClass() != obj.getClass())
345             return false;
346         StaticRouteConfig other = (StaticRouteConfig) obj;
347         if (name == null) {
348             if (other.name != null)
349                 return false;
350         } else if (!name.equals(other.name))
351             return false;
352         if (nextHop == null) {
353             if (other.nextHop != null)
354                 return false;
355         } else if (!nextHop.equals(other.nextHop))
356             return false;
357         if (staticRoute == null) {
358             if (other.staticRoute != null)
359                 return false;
360         } else if (!staticRoute.equals(other.staticRoute))
361             return false;
362         return true;
363     }
364
365     @Override
366     public String toString() {
367         return "StaticRouteConfig [name=" + name + ", staticRoute="
368                 + staticRoute + ", nextHopType=" + nextHopType + ", nextHop="
369                 + nextHop + "]";
370     }
371 }