Added the missing Copyright headers to most of the java files.
[netvirt.git] / neutron / src / main / java / org / opendaylight / ovsdb / neutron / BaseHandler.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal, Brent Salisbury
9  */
10 package org.opendaylight.ovsdb.neutron;
11
12 import java.net.HttpURLConnection;
13 import java.util.UUID;
14
15 import org.opendaylight.controller.containermanager.IContainerManager;
16 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
17 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
18 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
19 import org.opendaylight.controller.sal.utils.Status;
20 import org.opendaylight.controller.sal.utils.StatusCode;
21 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Base utility functions used by neutron handlers.
27  */
28 public class BaseHandler {
29
30     /**
31      * Logger instance.
32      */
33     static final Logger logger = LoggerFactory.getLogger(BaseHandler.class);
34
35     /**
36      * Neutron UUID identifier length.
37      */
38     private static final int UUID_LEN = 36;
39
40     /**
41      * Tenant id length when keystone identifier is used in neutron.
42      */
43     private static final int KEYSTONE_ID_LEN = 32;
44
45     /**
46      * UUID version number position.
47      */
48     private static final int UUID_VERSION_POS = 12;
49
50     /**
51      * UUID time-low field byte length in hex.
52      */
53     private static final int UUID_TIME_LOW = 8;
54
55     /**
56      * UUID time-mid field byte length in hex.
57      */
58     private static final int UUID_TIME_MID = 4;
59
60     /**
61      * UUID time-high and version field byte length in hex.
62      */
63     private static final int UUID_TIME_HIGH_VERSION = 4;
64
65     /**
66      * UUID clock sequence field byte length in hex.
67      */
68     private static final int UUID_CLOCK_SEQ = 4;
69
70     /**
71      * UUID node field byte length in hex.
72      */
73     private static final int UUID_NODE = 12;
74
75     /**
76      * UUID time field byte length in hex.
77      */
78     private static final int UUID_TIME_LEN = (UUID_TIME_LOW +
79             UUID_TIME_MID + UUID_TIME_HIGH_VERSION);
80
81     /**
82      * Convert failure status returned by the  manager into
83      * neutron API service errors.
84      *
85      * @param status  manager status
86      * @return  An error to be returned to neutron API service.
87      */
88     protected static final int getException(Status status) {
89         int result = HttpURLConnection.HTTP_INTERNAL_ERROR;
90
91         assert !status.isSuccess();
92
93         StatusCode code = status.getCode();
94         logger.debug(" Execption code - {}, description - {}",
95                 code, status.getDescription());
96
97         if (code == StatusCode.BADREQUEST) {
98             result = HttpURLConnection.HTTP_BAD_REQUEST;
99         } else if (code == StatusCode.CONFLICT) {
100             result = HttpURLConnection.HTTP_CONFLICT;
101         } else if (code == StatusCode.NOTACCEPTABLE) {
102             result = HttpURLConnection.HTTP_NOT_ACCEPTABLE;
103         } else if (code == StatusCode.NOTFOUND) {
104             result = HttpURLConnection.HTTP_NOT_FOUND;
105         } else {
106             result = HttpURLConnection.HTTP_INTERNAL_ERROR;
107         }
108
109         return result;
110     }
111
112     /**
113      * Verify the validity of neutron object identifiers.
114      *
115      * @param id neutron object id.
116      * @return {@code true} neutron identifier is valid.
117      *         {@code false} neutron identifier is invalid.
118      */
119     protected static final boolean isValidNeutronID(String id) {
120         if (id == null) {
121             return false;
122         }
123         boolean isValid = false;
124         logger.trace("id - {}, length - {} ", id, id.length());
125         /**
126          * check the string length
127          * if length is 36 its a uuid do uuid validation
128          * if length is 32 it can be tenant id form keystone
129          * if its less than 32  can be valid  ID
130          */
131         if (id.length() == UUID_LEN) {
132             try {
133                 UUID fromUUID = UUID.fromString(id);
134                 String toUUID = fromUUID.toString();
135                 isValid = toUUID.equalsIgnoreCase(id);
136             } catch(IllegalArgumentException e) {
137                 logger.error(" IllegalArgumentExecption for id - {} ", id);
138                 isValid = false;
139             }
140         } else if ((id.length() > 0) && (id.length() <= KEYSTONE_ID_LEN)) {
141             isValid = true;
142         } else {
143             isValid = false;
144         }
145         return isValid;
146     }
147
148     /**
149      * Convert UUID to  key syntax.
150      *
151      * @param id neutron object UUID.
152      * @return key in compliance to  object key.
153      */
154     private static String convertUUIDToKey(String id) {
155
156         String key = null;
157         if (id == null) {
158             return key;
159         }
160         logger.trace("id - {}, length - {} ", id, id.length());
161         /**
162          *  ID must be less than 32 bytes,
163          * Shorten UUID string length from 36 to 31 as follows:
164          * delete UUID Version and hyphen (see RFC4122) field in the UUID
165          */
166         try {
167             StringBuilder tKey = new StringBuilder();
168             // remove all the '-'
169             for (String retkey: id.split("-")) {
170                 tKey.append(retkey);
171             }
172             // remove the version byte
173             tKey.deleteCharAt(UUID_VERSION_POS);
174             key = tKey.toString();
175         } catch(IllegalArgumentException ile) {
176             logger.error(" Invalid UUID - {} ", id);
177             key = null;
178         }
179         return key;
180     }
181
182     /**
183      * Convert string id to  key syntax.
184      *
185      * @param id neutron object id.
186      * @return key in compliance to  object key.
187      */
188     private static String convertKeystoneIDToKey(String id) {
189         String key = null;
190         if (id == null) {
191             return key;
192         }
193
194         /**
195          * tenant ID if given from openstack keystone does not follow the
196          * generic UUID syntax, convert the ID to UUID format for validation
197          * and reconvert it to  key
198          */
199
200         logger.trace(" id - {}, length - {} ", id, id.length());
201         try {
202             StringBuilder tKey = new StringBuilder();
203             String tmpStr = id.substring(0, UUID_TIME_LOW);
204             tKey.append(tmpStr);
205             tKey.append("-");
206             tmpStr = id.substring(UUID_TIME_LOW,
207                     (UUID_TIME_LOW + UUID_TIME_MID));
208             tKey.append(tmpStr);
209             tKey.append("-");
210             tmpStr = id.substring((UUID_TIME_LOW + UUID_TIME_MID),
211                     UUID_TIME_LEN);
212             tKey.append(tmpStr);
213             tKey.append("-");
214             tmpStr = id.substring(UUID_TIME_LEN,
215                     (UUID_TIME_LEN + UUID_CLOCK_SEQ));
216             tKey.append(tmpStr);
217             tKey.append("-");
218             tmpStr = id.substring((UUID_TIME_LEN + UUID_CLOCK_SEQ),
219                     (UUID_TIME_LEN + UUID_CLOCK_SEQ + UUID_NODE));
220             tKey.append(tmpStr);
221
222             tmpStr = tKey.toString();
223             UUID fromUUID = UUID.fromString(tmpStr);
224             String toUUID = fromUUID.toString();
225             if (toUUID.equalsIgnoreCase(tmpStr)) {
226                 key = convertUUIDToKey(tmpStr);
227             }
228         } catch(IndexOutOfBoundsException ibe) {
229             logger.error(" Execption! Invalid UUID - {} ", id);
230             key = null;
231         } catch (IllegalArgumentException iae) {
232             logger.error(" Execption! Invalid object ID - {} ", id);
233             key = null;
234         }
235         return key;
236     }
237
238     /**
239      * Convert neutron object id to  key syntax.
240      *
241      * @param neutronID neutron object id.
242      * @return key in compliance to  object key.
243      */
244     protected static final String convertNeutronIDToKey(String neutronID) {
245         String key = null;
246         if (neutronID == null) {
247             return key;
248         }
249
250         logger.trace(" neutronID - {}, length - {} ",
251                 neutronID, neutronID.length());
252         if (!isValidNeutronID(neutronID)) {
253             return key;
254         }
255
256         if (neutronID.length() == UUID_LEN) {
257             key = convertUUIDToKey(neutronID);
258         } else if (neutronID.length() == KEYSTONE_ID_LEN) {
259             key = convertKeystoneIDToKey(neutronID);
260         } else {
261             key = neutronID;
262         }
263         return key;
264     }
265
266     protected IContainerManager containerManager;
267
268     public IContainerManager getContainerManager() {
269         return containerManager;
270     }
271
272     public void unsetContainerManager(IContainerManager s) {
273         if (s == this.containerManager) {
274             this.containerManager = null;
275         }
276     }
277
278     public void setContainerManager(IContainerManager s) {
279         this.containerManager = s;
280     }
281
282     protected OVSDBConfigService ovsdbConfigService;
283
284     public OVSDBConfigService getOVSDBConfigService() {
285         return ovsdbConfigService;
286     }
287
288     public void unsetOVSDBConfigService(OVSDBConfigService s) {
289         if (s == this.ovsdbConfigService) {
290             this.ovsdbConfigService = null;
291         }
292     }
293
294     public void setOVSDBConfigService(OVSDBConfigService s) {
295         this.ovsdbConfigService = s;
296     }
297
298
299     protected INeutronPortCRUD neutronPortCache;
300     public INeutronPortCRUD getNeutronPortCRUD() {
301         return neutronPortCache;
302     }
303
304     public void unsetNeutronPortCRUD(INeutronPortCRUD s) {
305         if (s == this.neutronPortCache) {
306             this.neutronPortCache = null;
307         }
308     }
309
310     public void setNeutronPortCRUD(INeutronPortCRUD s) {
311         this.neutronPortCache = s;
312     }
313
314     protected INeutronSubnetCRUD neutronSubnetCache;
315     public INeutronSubnetCRUD getNeutronSubnetCRUD() {
316         return neutronSubnetCache;
317     }
318
319     public void unsetNeutronSubnetCRUD(INeutronSubnetCRUD s) {
320         if (s == this.neutronSubnetCache) {
321             this.neutronSubnetCache = null;
322         }
323     }
324
325     public void setNeutronSubnetCRUD(INeutronSubnetCRUD s) {
326         this.neutronSubnetCache = s;
327     }
328
329     protected INeutronNetworkCRUD neutronNetworkCache;
330     public INeutronNetworkCRUD getNeutronNetworkCRUD() {
331         return neutronNetworkCache;
332     }
333
334     public void unsetNeutronNetworkCRUD(INeutronNetworkCRUD s) {
335         if (s == this.neutronNetworkCache) {
336             this.neutronNetworkCache = null;
337         }
338     }
339
340     public void setNeutronNetworkCRUD(INeutronNetworkCRUD s) {
341         this.neutronNetworkCache = s;
342     }
343 }