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.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;
30 * Base utility functions used by neutron handlers.
32 public class BaseHandler {
37 static final Logger logger = LoggerFactory.getLogger(BaseHandler.class);
40 * Neutron UUID identifier length.
42 private static final int UUID_LEN = 36;
45 * Tenant id length when keystone identifier is used in neutron.
47 private static final int KEYSTONE_ID_LEN = 32;
50 * UUID version number position.
52 private static final int UUID_VERSION_POS = 12;
55 * UUID time-low field byte length in hex.
57 private static final int UUID_TIME_LOW = 8;
60 * UUID time-mid field byte length in hex.
62 private static final int UUID_TIME_MID = 4;
65 * UUID time-high and version field byte length in hex.
67 private static final int UUID_TIME_HIGH_VERSION = 4;
70 * UUID clock sequence field byte length in hex.
72 private static final int UUID_CLOCK_SEQ = 4;
75 * UUID node field byte length in hex.
77 private static final int UUID_NODE = 12;
80 * UUID time field byte length in hex.
82 private static final int UUID_TIME_LEN = (UUID_TIME_LOW +
83 UUID_TIME_MID + UUID_TIME_HIGH_VERSION);
86 * Convert failure status returned by the manager into
87 * neutron API service errors.
89 * @param status manager status
90 * @return An error to be returned to neutron API service.
92 protected static final int getException(Status status) {
93 int result = HttpURLConnection.HTTP_INTERNAL_ERROR;
95 assert !status.isSuccess();
97 StatusCode code = status.getCode();
98 logger.debug(" Execption code - {}, description - {}",
99 code, status.getDescription());
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;
110 result = HttpURLConnection.HTTP_INTERNAL_ERROR;
117 * Verify the validity of neutron object identifiers.
119 * @param id neutron object id.
120 * @return {@code true} neutron identifier is valid.
121 * {@code false} neutron identifier is invalid.
123 protected static final boolean isValidNeutronID(String id) {
127 boolean isValid = false;
128 logger.trace("id - {}, length - {} ", id, id.length());
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
135 if (id.length() == UUID_LEN) {
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);
144 } else if ((id.length() > 0) && (id.length() <= KEYSTONE_ID_LEN)) {
153 * Convert UUID to key syntax.
155 * @param id neutron object UUID.
156 * @return key in compliance to object key.
158 private static String convertUUIDToKey(String id) {
164 logger.trace("id - {}, length - {} ", id, id.length());
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
171 StringBuilder tKey = new StringBuilder();
172 // remove all the '-'
173 for (String retkey: id.split("-")) {
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);
187 * Convert string id to key syntax.
189 * @param id neutron object id.
190 * @return key in compliance to object key.
192 private static String convertKeystoneIDToKey(String id) {
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
204 logger.trace(" id - {}, length - {} ", id, id.length());
206 StringBuilder tKey = new StringBuilder();
207 String tmpStr = id.substring(0, UUID_TIME_LOW);
210 tmpStr = id.substring(UUID_TIME_LOW,
211 (UUID_TIME_LOW + UUID_TIME_MID));
214 tmpStr = id.substring((UUID_TIME_LOW + UUID_TIME_MID),
218 tmpStr = id.substring(UUID_TIME_LEN,
219 (UUID_TIME_LEN + UUID_CLOCK_SEQ));
222 tmpStr = id.substring((UUID_TIME_LEN + UUID_CLOCK_SEQ),
223 (UUID_TIME_LEN + UUID_CLOCK_SEQ + UUID_NODE));
226 tmpStr = tKey.toString();
227 UUID fromUUID = UUID.fromString(tmpStr);
228 String toUUID = fromUUID.toString();
229 if (toUUID.equalsIgnoreCase(tmpStr)) {
230 key = convertUUIDToKey(tmpStr);
232 } catch(IndexOutOfBoundsException ibe) {
233 logger.error(" Execption! Invalid UUID - {} ", id);
235 } catch (IllegalArgumentException iae) {
236 logger.error(" Execption! Invalid object ID - {} ", id);
243 * Convert neutron object id to key syntax.
245 * @param neutronID neutron object id.
246 * @return key in compliance to object key.
248 protected static final String convertNeutronIDToKey(String neutronID) {
250 if (neutronID == null) {
254 logger.trace(" neutronID - {}, length - {} ",
255 neutronID, neutronID.length());
256 if (!isValidNeutronID(neutronID)) {
260 if (neutronID.length() == UUID_LEN) {
261 key = convertUUIDToKey(neutronID);
262 } else if (neutronID.length() == KEYSTONE_ID_LEN) {
263 key = convertKeystoneIDToKey(neutronID);
270 protected IContainerManager containerManager;
272 public IContainerManager getContainerManager() {
273 return containerManager;
276 public void unsetContainerManager(IContainerManager s) {
277 if (s == this.containerManager) {
278 this.containerManager = null;
282 public void setContainerManager(IContainerManager s) {
283 this.containerManager = s;
286 protected IForwardingRulesManager frm;
288 public IForwardingRulesManager getForwardingRulesManager() {
292 public void unsetForwardingRulesManager(IForwardingRulesManager s) {
298 public void setForwardingRulesManager(IForwardingRulesManager s) {
302 protected OVSDBConfigService ovsdbConfigService;
304 public OVSDBConfigService getOVSDBConfigService() {
305 return ovsdbConfigService;
308 public void unsetOVSDBConfigService(OVSDBConfigService s) {
309 if (s == this.ovsdbConfigService) {
310 this.ovsdbConfigService = null;
314 public void setOVSDBConfigService(OVSDBConfigService s) {
315 this.ovsdbConfigService = s;
318 protected IConnectionServiceInternal connectionService;
320 public IConnectionServiceInternal getConnectionService() {
321 return connectionService;
324 public void unsetConnectionService(IConnectionServiceInternal s) {
325 if (s == this.connectionService) {
326 this.connectionService = null;
330 public void setConnectionService(IConnectionServiceInternal s) {
331 this.connectionService = s;
334 protected INeutronPortCRUD neutronPortCache;
335 public INeutronPortCRUD getNeutronPortCRUD() {
336 return neutronPortCache;
339 public void unsetNeutronPortCRUD(INeutronPortCRUD s) {
340 if (s == this.neutronPortCache) {
341 this.neutronPortCache = null;
345 public void setNeutronPortCRUD(INeutronPortCRUD s) {
346 this.neutronPortCache = s;
349 protected INeutronSubnetCRUD neutronSubnetCache;
350 public INeutronSubnetCRUD getNeutronSubnetCRUD() {
351 return neutronSubnetCache;
354 public void unsetNeutronSubnetCRUD(INeutronSubnetCRUD s) {
355 if (s == this.neutronSubnetCache) {
356 this.neutronSubnetCache = null;
360 public void setNeutronSubnetCRUD(INeutronSubnetCRUD s) {
361 this.neutronSubnetCache = s;
364 protected INeutronNetworkCRUD neutronNetworkCache;
365 public INeutronNetworkCRUD getNeutronNetworkCRUD() {
366 return neutronNetworkCache;
369 public void unsetNeutronNetworkCRUD(INeutronNetworkCRUD s) {
370 if (s == this.neutronNetworkCache) {
371 this.neutronNetworkCache = null;
375 public void setNeutronNetworkCRUD(INeutronNetworkCRUD s) {
376 this.neutronNetworkCache = s;
379 protected INeutronSecurityRuleCRUD neutronSecurityRuleCache;
380 public void setNeutronSecurityRuleCache(INeutronSecurityRuleCRUD s) {
381 this.neutronSecurityRuleCache = s;
384 protected INeutronSecurityGroupCRUD neutronSecurityGroupCache;
385 public void setNeutronSecurityGroupCache(INeutronSecurityGroupCRUD s) {
386 this.neutronSecurityGroupCache = s;