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