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
11 package org.opendaylight.ovsdb.openstack.netvirt.api;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import java.util.UUID;
19 * Utilities for validating and converting OpenStack UUID's
21 public final class UuidUtils {
24 * Neutron UUID identifier length.
26 private static final int UUID_LEN = 36;
29 * Tenant id length when keystone identifier is used in neutron.
31 private static final int KEYSTONE_ID_LEN = 32;
34 * UUID version number position.
36 private static final int UUID_VERSION_POS = 12;
39 * UUID time-low field byte length in hex.
41 private static final int UUID_TIME_LOW = 8;
44 * UUID time-mid field byte length in hex.
46 private static final int UUID_TIME_MID = 4;
49 * UUID time-high and version field byte length in hex.
51 private static final int UUID_TIME_HIGH_VERSION = 4;
54 * UUID clock sequence field byte length in hex.
56 private static final int UUID_CLOCK_SEQ = 4;
59 * UUID node field byte length in hex.
61 private static final int UUID_NODE = 12;
64 * UUID time field byte length in hex.
66 private static final int UUID_TIME_LEN = (UUID_TIME_LOW +
67 UUID_TIME_MID + UUID_TIME_HIGH_VERSION);
69 static final Logger logger = LoggerFactory.getLogger(UuidUtils.class);
72 * Convert neutron object id to key syntax.
74 * @param neutronID neutron object id.
75 * @return key in compliance to object key.
77 public static String convertNeutronIDToKey(String neutronID) {
79 if (neutronID == null) {
83 logger.trace(" neutronID - {}, length - {} ",
84 neutronID, neutronID.length());
85 if (!isValidNeutronID(neutronID)) {
89 if (neutronID.length() == UUID_LEN) {
90 key = convertUUIDToKey(neutronID);
91 } else if (neutronID.length() == KEYSTONE_ID_LEN) {
92 key = convertKeystoneIDToKey(neutronID);
100 * Verify the validity of neutron object identifiers.
102 * @param id neutron object id.
103 * @return {@code true} neutron identifier is valid.
104 * {@code false} neutron identifier is invalid.
106 public static boolean isValidNeutronID(String id) {
111 logger.trace("id - {}, length - {} ", id, id.length());
113 * check the string length
114 * if length is 36 its a uuid do uuid validation
115 * if length is 32 it can be tenant id form keystone
116 * if its less than 32 can be valid ID
118 if (id.length() == UUID_LEN) {
120 UUID fromUUID = UUID.fromString(id);
121 String toUUID = fromUUID.toString();
122 isValid = toUUID.equalsIgnoreCase(id);
123 } catch(IllegalArgumentException e) {
124 logger.error(" IllegalArgumentExecption for id - {} ", id);
128 isValid = (id.length() > 0) && (id.length() <= KEYSTONE_ID_LEN);
134 * Convert UUID to key syntax.
136 * @param id neutron object UUID.
137 * @return key in compliance to object key.
139 private static String convertUUIDToKey(String id) {
145 logger.trace("id - {}, length - {} ", id, id.length());
147 * ID must be less than 32 bytes,
148 * Shorten UUID string length from 36 to 31 as follows:
149 * delete UUID Version and hyphen (see RFC4122) field in the UUID
152 StringBuilder tKey = new StringBuilder();
153 // remove all the '-'
154 for (String retkey: id.split("-")) {
157 // remove the version byte
158 tKey.deleteCharAt(UUID_VERSION_POS);
159 key = tKey.toString();
160 } catch(IllegalArgumentException ile) {
161 logger.error(" Invalid UUID - {} ", id);
168 * Convert string id to key syntax.
170 * @param id neutron object id.
171 * @return key in compliance to object key.
173 private static String convertKeystoneIDToKey(String id) {
180 * tenant ID if given from openstack keystone does not follow the
181 * generic UUID syntax, convert the ID to UUID format for validation
182 * and reconvert it to key
185 logger.trace(" id - {}, length - {} ", id, id.length());
187 StringBuilder tKey = new StringBuilder();
188 String tmpStr = id.substring(0, UUID_TIME_LOW);
191 tmpStr = id.substring(UUID_TIME_LOW,
192 (UUID_TIME_LOW + UUID_TIME_MID));
195 tmpStr = id.substring((UUID_TIME_LOW + UUID_TIME_MID),
199 tmpStr = id.substring(UUID_TIME_LEN,
200 (UUID_TIME_LEN + UUID_CLOCK_SEQ));
203 tmpStr = id.substring((UUID_TIME_LEN + UUID_CLOCK_SEQ),
204 (UUID_TIME_LEN + UUID_CLOCK_SEQ + UUID_NODE));
207 tmpStr = tKey.toString();
208 UUID fromUUID = UUID.fromString(tmpStr);
209 String toUUID = fromUUID.toString();
210 if (toUUID.equalsIgnoreCase(tmpStr)) {
211 key = convertUUIDToKey(tmpStr);
213 } catch(IndexOutOfBoundsException ibe) {
214 logger.error(" Exception! Invalid UUID - {} ", id);
216 } catch (IllegalArgumentException iae) {
217 logger.error(" Exception! Invalid object ID - {} ", id);