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