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