Change the header to SO
[so.git] / common / src / main / java / org / openecomp / mso / utils / UUIDChecker.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.utils;
22
23 import org.openecomp.mso.logger.MessageEnum;
24 import org.openecomp.mso.logger.MsoLogger;
25
26 import java.util.UUID;
27
28 /**
29  */
30 public class UUIDChecker {
31
32     private UUIDChecker() {
33
34     }
35
36     public static boolean isValidUUID (String id) {
37         try {
38             if (null == id) {
39                 return false;
40             }
41             UUID uuid = UUID.fromString(id);
42             return uuid.toString().equalsIgnoreCase(id);
43         } catch (IllegalArgumentException iae) {
44             return false;
45         }
46     }
47
48     private static String getUUID () {
49         return UUID.randomUUID().toString();
50     }
51
52     public static String verifyOldUUID (String oldId, MsoLogger msoLogger) {
53         if (!UUIDChecker.isValidUUID(oldId)) {
54             String newId = UUIDChecker.getUUID();
55             MsoLogger.setLogContext(newId, null);
56             msoLogger.info(MessageEnum.APIH_REPLACE_REQUEST_ID, oldId, "", "");
57             return newId;
58         }
59         MsoLogger.setLogContext(oldId, null);
60         return oldId;
61     }
62
63     public static String generateUUID (MsoLogger msoLogger) {
64         String newId = UUIDChecker.getUUID();
65         MsoLogger.setLogContext(newId, null);
66         msoLogger.info(MessageEnum.APIH_GENERATED_REQUEST_ID, newId, "", "");
67         return newId;
68     }
69
70     public static String generateServiceInstanceID (MsoLogger msoLogger) {
71         String newId = UUIDChecker.getUUID();
72         MsoLogger.setLogContext(null, newId);
73         msoLogger.info(MessageEnum.APIH_GENERATED_SERVICE_INSTANCE_ID, newId, "", "");
74         return newId;
75     }
76 }