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.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;
26 * Base utility functions used by neutron handlers.
28 public class BaseHandler {
33 static final Logger logger = LoggerFactory.getLogger(BaseHandler.class);
36 * Neutron UUID identifier length.
38 private static final int UUID_LEN = 36;
41 * Tenant id length when keystone identifier is used in neutron.
43 private static final int KEYSTONE_ID_LEN = 32;
46 * UUID version number position.
48 private static final int UUID_VERSION_POS = 12;
51 * UUID time-low field byte length in hex.
53 private static final int UUID_TIME_LOW = 8;
56 * UUID time-mid field byte length in hex.
58 private static final int UUID_TIME_MID = 4;
61 * UUID time-high and version field byte length in hex.
63 private static final int UUID_TIME_HIGH_VERSION = 4;
66 * UUID clock sequence field byte length in hex.
68 private static final int UUID_CLOCK_SEQ = 4;
71 * UUID node field byte length in hex.
73 private static final int UUID_NODE = 12;
76 * UUID time field byte length in hex.
78 private static final int UUID_TIME_LEN = (UUID_TIME_LOW +
79 UUID_TIME_MID + UUID_TIME_HIGH_VERSION);
82 * Convert failure status returned by the manager into
83 * neutron API service errors.
85 * @param status manager status
86 * @return An error to be returned to neutron API service.
88 protected static final int getException(Status status) {
89 int result = HttpURLConnection.HTTP_INTERNAL_ERROR;
91 assert !status.isSuccess();
93 StatusCode code = status.getCode();
94 logger.debug(" Execption code - {}, description - {}",
95 code, status.getDescription());
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;
106 result = HttpURLConnection.HTTP_INTERNAL_ERROR;
113 * Verify the validity of neutron object identifiers.
115 * @param id neutron object id.
116 * @return {@code true} neutron identifier is valid.
117 * {@code false} neutron identifier is invalid.
119 protected static final boolean isValidNeutronID(String id) {
123 boolean isValid = false;
124 logger.trace("id - {}, length - {} ", id, id.length());
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
131 if (id.length() == UUID_LEN) {
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);
140 } else if ((id.length() > 0) && (id.length() <= KEYSTONE_ID_LEN)) {
149 * Convert UUID to key syntax.
151 * @param id neutron object UUID.
152 * @return key in compliance to object key.
154 private static String convertUUIDToKey(String id) {
160 logger.trace("id - {}, length - {} ", id, id.length());
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
167 StringBuilder tKey = new StringBuilder();
168 // remove all the '-'
169 for (String retkey: id.split("-")) {
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);
183 * Convert string id to key syntax.
185 * @param id neutron object id.
186 * @return key in compliance to object key.
188 private static String convertKeystoneIDToKey(String id) {
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
200 logger.trace(" id - {}, length - {} ", id, id.length());
202 StringBuilder tKey = new StringBuilder();
203 String tmpStr = id.substring(0, UUID_TIME_LOW);
206 tmpStr = id.substring(UUID_TIME_LOW,
207 (UUID_TIME_LOW + UUID_TIME_MID));
210 tmpStr = id.substring((UUID_TIME_LOW + UUID_TIME_MID),
214 tmpStr = id.substring(UUID_TIME_LEN,
215 (UUID_TIME_LEN + UUID_CLOCK_SEQ));
218 tmpStr = id.substring((UUID_TIME_LEN + UUID_CLOCK_SEQ),
219 (UUID_TIME_LEN + UUID_CLOCK_SEQ + UUID_NODE));
222 tmpStr = tKey.toString();
223 UUID fromUUID = UUID.fromString(tmpStr);
224 String toUUID = fromUUID.toString();
225 if (toUUID.equalsIgnoreCase(tmpStr)) {
226 key = convertUUIDToKey(tmpStr);
228 } catch(IndexOutOfBoundsException ibe) {
229 logger.error(" Execption! Invalid UUID - {} ", id);
231 } catch (IllegalArgumentException iae) {
232 logger.error(" Execption! Invalid object ID - {} ", id);
239 * Convert neutron object id to key syntax.
241 * @param neutronID neutron object id.
242 * @return key in compliance to object key.
244 protected static final String convertNeutronIDToKey(String neutronID) {
246 if (neutronID == null) {
250 logger.trace(" neutronID - {}, length - {} ",
251 neutronID, neutronID.length());
252 if (!isValidNeutronID(neutronID)) {
256 if (neutronID.length() == UUID_LEN) {
257 key = convertUUIDToKey(neutronID);
258 } else if (neutronID.length() == KEYSTONE_ID_LEN) {
259 key = convertKeystoneIDToKey(neutronID);
266 protected IContainerManager containerManager;
268 public IContainerManager getContainerManager() {
269 return containerManager;
272 public void unsetContainerManager(IContainerManager s) {
273 if (s == this.containerManager) {
274 this.containerManager = null;
278 public void setContainerManager(IContainerManager s) {
279 this.containerManager = s;
282 protected OVSDBConfigService ovsdbConfigService;
284 public OVSDBConfigService getOVSDBConfigService() {
285 return ovsdbConfigService;
288 public void unsetOVSDBConfigService(OVSDBConfigService s) {
289 if (s == this.ovsdbConfigService) {
290 this.ovsdbConfigService = null;
294 public void setOVSDBConfigService(OVSDBConfigService s) {
295 this.ovsdbConfigService = s;
299 protected INeutronPortCRUD neutronPortCache;
300 public INeutronPortCRUD getNeutronPortCRUD() {
301 return neutronPortCache;
304 public void unsetNeutronPortCRUD(INeutronPortCRUD s) {
305 if (s == this.neutronPortCache) {
306 this.neutronPortCache = null;
310 public void setNeutronPortCRUD(INeutronPortCRUD s) {
311 this.neutronPortCache = s;
314 protected INeutronSubnetCRUD neutronSubnetCache;
315 public INeutronSubnetCRUD getNeutronSubnetCRUD() {
316 return neutronSubnetCache;
319 public void unsetNeutronSubnetCRUD(INeutronSubnetCRUD s) {
320 if (s == this.neutronSubnetCache) {
321 this.neutronSubnetCache = null;
325 public void setNeutronSubnetCRUD(INeutronSubnetCRUD s) {
326 this.neutronSubnetCache = s;
329 protected INeutronNetworkCRUD neutronNetworkCache;
330 public INeutronNetworkCRUD getNeutronNetworkCRUD() {
331 return neutronNetworkCache;
334 public void unsetNeutronNetworkCRUD(INeutronNetworkCRUD s) {
335 if (s == this.neutronNetworkCache) {
336 this.neutronNetworkCache = null;
340 public void setNeutronNetworkCRUD(INeutronNetworkCRUD s) {
341 this.neutronNetworkCache = s;