Remove all redundant public modifiers in interfaces
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / api / UuidUtils.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
11 package org.opendaylight.ovsdb.openstack.netvirt.api;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.util.UUID;
17
18 /**
19  * Utilities for validating and converting OpenStack UUID's
20  */
21 public final class UuidUtils {
22
23     /**
24      * Neutron UUID identifier length.
25      */
26     private static final int UUID_LEN = 36;
27
28     /**
29      * Tenant id length when keystone identifier is used in neutron.
30      */
31     private static final int KEYSTONE_ID_LEN = 32;
32
33     /**
34      * UUID version number position.
35      */
36     private static final int UUID_VERSION_POS = 12;
37
38     /**
39      * UUID time-low field byte length in hex.
40      */
41     private static final int UUID_TIME_LOW = 8;
42
43     /**
44      * UUID time-mid field byte length in hex.
45      */
46     private static final int UUID_TIME_MID = 4;
47
48     /**
49      * UUID time-high and version field byte length in hex.
50      */
51     private static final int UUID_TIME_HIGH_VERSION = 4;
52
53     /**
54      * UUID clock sequence field byte length in hex.
55      */
56     private static final int UUID_CLOCK_SEQ = 4;
57
58     /**
59      * UUID node field byte length in hex.
60      */
61     private static final int UUID_NODE = 12;
62
63     /**
64      * UUID time field byte length in hex.
65      */
66     private static final int UUID_TIME_LEN = (UUID_TIME_LOW +
67                                               UUID_TIME_MID + UUID_TIME_HIGH_VERSION);
68
69     static final Logger logger = LoggerFactory.getLogger(UuidUtils.class);
70
71     /**
72      * Convert neutron object id to  key syntax.
73      *
74      * @param neutronID neutron object id.
75      * @return key in compliance to  object key.
76      */
77     public static String convertNeutronIDToKey(String neutronID) {
78         String key;
79         if (neutronID == null) {
80             return null;
81         }
82
83         logger.trace(" neutronID - {}, length - {} ",
84                      neutronID, neutronID.length());
85         if (!isValidNeutronID(neutronID)) {
86             return null;
87         }
88
89         if (neutronID.length() == UUID_LEN) {
90             key = convertUUIDToKey(neutronID);
91         } else if (neutronID.length() == KEYSTONE_ID_LEN) {
92             key = convertKeystoneIDToKey(neutronID);
93         } else {
94             key = neutronID;
95         }
96         return key;
97     }
98
99     /**
100      * Verify the validity of neutron object identifiers.
101      *
102      * @param id neutron object id.
103      * @return {@code true} neutron identifier is valid.
104      *         {@code false} neutron identifier is invalid.
105      */
106     public static boolean isValidNeutronID(String id) {
107         if (id == null) {
108             return false;
109         }
110         boolean isValid;
111         logger.trace("id - {}, length - {} ", id, id.length());
112         /**
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
117          */
118         if (id.length() == UUID_LEN) {
119             try {
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);
125                 isValid = false;
126             }
127         } else {
128             isValid = (id.length() > 0) && (id.length() <= KEYSTONE_ID_LEN);
129         }
130         return isValid;
131     }
132
133     /**
134      * Convert UUID to  key syntax.
135      *
136      * @param id neutron object UUID.
137      * @return key in compliance to  object key.
138      */
139     private static String convertUUIDToKey(String id) {
140
141         String key;
142         if (id == null) {
143             return null;
144         }
145         logger.trace("id - {}, length - {} ", id, id.length());
146         /**
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
150          */
151         try {
152             StringBuilder tKey = new StringBuilder();
153             // remove all the '-'
154             for (String retkey: id.split("-")) {
155                 tKey.append(retkey);
156             }
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);
162             key = null;
163         }
164         return key;
165     }
166
167     /**
168      * Convert string id to  key syntax.
169      *
170      * @param id neutron object id.
171      * @return key in compliance to  object key.
172      */
173     private static String convertKeystoneIDToKey(String id) {
174         String key = null;
175         if (id == null) {
176             return null;
177         }
178
179         /**
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
183          */
184
185         logger.trace(" id - {}, length - {} ", id, id.length());
186         try {
187             StringBuilder tKey = new StringBuilder();
188             String tmpStr = id.substring(0, UUID_TIME_LOW);
189             tKey.append(tmpStr);
190             tKey.append("-");
191             tmpStr = id.substring(UUID_TIME_LOW,
192                                   (UUID_TIME_LOW + UUID_TIME_MID));
193             tKey.append(tmpStr);
194             tKey.append("-");
195             tmpStr = id.substring((UUID_TIME_LOW + UUID_TIME_MID),
196                                   UUID_TIME_LEN);
197             tKey.append(tmpStr);
198             tKey.append("-");
199             tmpStr = id.substring(UUID_TIME_LEN,
200                                   (UUID_TIME_LEN + UUID_CLOCK_SEQ));
201             tKey.append(tmpStr);
202             tKey.append("-");
203             tmpStr = id.substring((UUID_TIME_LEN + UUID_CLOCK_SEQ),
204                                   (UUID_TIME_LEN + UUID_CLOCK_SEQ + UUID_NODE));
205             tKey.append(tmpStr);
206
207             tmpStr = tKey.toString();
208             UUID fromUUID = UUID.fromString(tmpStr);
209             String toUUID = fromUUID.toString();
210             if (toUUID.equalsIgnoreCase(tmpStr)) {
211                 key = convertUUIDToKey(tmpStr);
212             }
213         } catch(IndexOutOfBoundsException ibe) {
214             logger.error(" Exception! Invalid UUID - {} ", id);
215             key = null;
216         } catch (IllegalArgumentException iae) {
217             logger.error(" Exception! Invalid object ID - {} ", id);
218             key = null;
219         }
220         return key;
221     }
222
223 }