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