2 * Copyright (C) 2013 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal, Brent Salisbury
10 package org.opendaylight.ovsdb.neutron;
12 import java.net.HttpURLConnection;
13 import java.util.UUID;
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;
28 * Base utility functions used by neutron handlers.
30 public class BaseHandler {
35 static final Logger logger = LoggerFactory.getLogger(BaseHandler.class);
38 * Neutron UUID identifier length.
40 private static final int UUID_LEN = 36;
43 * Tenant id length when keystone identifier is used in neutron.
45 private static final int KEYSTONE_ID_LEN = 32;
48 * UUID version number position.
50 private static final int UUID_VERSION_POS = 12;
53 * UUID time-low field byte length in hex.
55 private static final int UUID_TIME_LOW = 8;
58 * UUID time-mid field byte length in hex.
60 private static final int UUID_TIME_MID = 4;
63 * UUID time-high and version field byte length in hex.
65 private static final int UUID_TIME_HIGH_VERSION = 4;
68 * UUID clock sequence field byte length in hex.
70 private static final int UUID_CLOCK_SEQ = 4;
73 * UUID node field byte length in hex.
75 private static final int UUID_NODE = 12;
78 * UUID time field byte length in hex.
80 private static final int UUID_TIME_LEN = (UUID_TIME_LOW +
81 UUID_TIME_MID + UUID_TIME_HIGH_VERSION);
84 * Convert failure status returned by the manager into
85 * neutron API service errors.
87 * @param status manager status
88 * @return An error to be returned to neutron API service.
90 protected static final int getException(Status status) {
91 int result = HttpURLConnection.HTTP_INTERNAL_ERROR;
93 assert !status.isSuccess();
95 StatusCode code = status.getCode();
96 logger.debug(" Execption code - {}, description - {}",
97 code, status.getDescription());
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;
108 result = HttpURLConnection.HTTP_INTERNAL_ERROR;
115 * Verify the validity of neutron object identifiers.
117 * @param id neutron object id.
118 * @return {@code true} neutron identifier is valid.
119 * {@code false} neutron identifier is invalid.
121 protected static final boolean isValidNeutronID(String id) {
125 boolean isValid = false;
126 logger.trace("id - {}, length - {} ", id, id.length());
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
133 if (id.length() == UUID_LEN) {
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);
142 } else if ((id.length() > 0) && (id.length() <= KEYSTONE_ID_LEN)) {
151 * Convert UUID to key syntax.
153 * @param id neutron object UUID.
154 * @return key in compliance to object key.
156 private static String convertUUIDToKey(String id) {
162 logger.trace("id - {}, length - {} ", id, id.length());
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
169 StringBuilder tKey = new StringBuilder();
170 // remove all the '-'
171 for (String retkey: id.split("-")) {
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);
185 * Convert string id to key syntax.
187 * @param id neutron object id.
188 * @return key in compliance to object key.
190 private static String convertKeystoneIDToKey(String id) {
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
202 logger.trace(" id - {}, length - {} ", id, id.length());
204 StringBuilder tKey = new StringBuilder();
205 String tmpStr = id.substring(0, UUID_TIME_LOW);
208 tmpStr = id.substring(UUID_TIME_LOW,
209 (UUID_TIME_LOW + UUID_TIME_MID));
212 tmpStr = id.substring((UUID_TIME_LOW + UUID_TIME_MID),
216 tmpStr = id.substring(UUID_TIME_LEN,
217 (UUID_TIME_LEN + UUID_CLOCK_SEQ));
220 tmpStr = id.substring((UUID_TIME_LEN + UUID_CLOCK_SEQ),
221 (UUID_TIME_LEN + UUID_CLOCK_SEQ + UUID_NODE));
224 tmpStr = tKey.toString();
225 UUID fromUUID = UUID.fromString(tmpStr);
226 String toUUID = fromUUID.toString();
227 if (toUUID.equalsIgnoreCase(tmpStr)) {
228 key = convertUUIDToKey(tmpStr);
230 } catch(IndexOutOfBoundsException ibe) {
231 logger.error(" Execption! Invalid UUID - {} ", id);
233 } catch (IllegalArgumentException iae) {
234 logger.error(" Execption! Invalid object ID - {} ", id);
241 * Convert neutron object id to key syntax.
243 * @param neutronID neutron object id.
244 * @return key in compliance to object key.
246 protected static final String convertNeutronIDToKey(String neutronID) {
248 if (neutronID == null) {
252 logger.trace(" neutronID - {}, length - {} ",
253 neutronID, neutronID.length());
254 if (!isValidNeutronID(neutronID)) {
258 if (neutronID.length() == UUID_LEN) {
259 key = convertUUIDToKey(neutronID);
260 } else if (neutronID.length() == KEYSTONE_ID_LEN) {
261 key = convertKeystoneIDToKey(neutronID);
268 protected IContainerManager containerManager;
270 public IContainerManager getContainerManager() {
271 return containerManager;
274 public void unsetContainerManager(IContainerManager s) {
275 if (s == this.containerManager) {
276 this.containerManager = null;
280 public void setContainerManager(IContainerManager s) {
281 this.containerManager = s;
284 protected IForwardingRulesManager frm;
286 public IForwardingRulesManager getForwardingRulesManager() {
290 public void unsetForwardingRulesManager(IForwardingRulesManager s) {
296 public void setForwardingRulesManager(IForwardingRulesManager s) {
300 protected OVSDBConfigService ovsdbConfigService;
302 public OVSDBConfigService getOVSDBConfigService() {
303 return ovsdbConfigService;
306 public void unsetOVSDBConfigService(OVSDBConfigService s) {
307 if (s == this.ovsdbConfigService) {
308 this.ovsdbConfigService = null;
312 public void setOVSDBConfigService(OVSDBConfigService s) {
313 this.ovsdbConfigService = s;
316 protected IConnectionServiceInternal connectionService;
318 public IConnectionServiceInternal getConnectionService() {
319 return connectionService;
322 public void unsetConnectionService(IConnectionServiceInternal s) {
323 if (s == this.connectionService) {
324 this.connectionService = null;
328 public void setConnectionService(IConnectionServiceInternal s) {
329 this.connectionService = s;
332 protected INeutronPortCRUD neutronPortCache;
333 public INeutronPortCRUD getNeutronPortCRUD() {
334 return neutronPortCache;
337 public void unsetNeutronPortCRUD(INeutronPortCRUD s) {
338 if (s == this.neutronPortCache) {
339 this.neutronPortCache = null;
343 public void setNeutronPortCRUD(INeutronPortCRUD s) {
344 this.neutronPortCache = s;
347 protected INeutronSubnetCRUD neutronSubnetCache;
348 public INeutronSubnetCRUD getNeutronSubnetCRUD() {
349 return neutronSubnetCache;
352 public void unsetNeutronSubnetCRUD(INeutronSubnetCRUD s) {
353 if (s == this.neutronSubnetCache) {
354 this.neutronSubnetCache = null;
358 public void setNeutronSubnetCRUD(INeutronSubnetCRUD s) {
359 this.neutronSubnetCache = s;
362 protected INeutronNetworkCRUD neutronNetworkCache;
363 public INeutronNetworkCRUD getNeutronNetworkCRUD() {
364 return neutronNetworkCache;
367 public void unsetNeutronNetworkCRUD(INeutronNetworkCRUD s) {
368 if (s == this.neutronNetworkCache) {
369 this.neutronNetworkCache = null;
373 public void setNeutronNetworkCRUD(INeutronNetworkCRUD s) {
374 this.neutronNetworkCache = s;