Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / util / AAIMechIdConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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.aai.util;
22
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Set;
29
30 import org.json.simple.JSONArray;
31 import org.json.simple.JSONObject;
32 import org.json.simple.parser.JSONParser;
33
34 import org.openecomp.aai.logging.ErrorLogHelper;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 public class AAIMechIdConfig {
39         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIMechIdConfig.class);
40         private static final String mechIdConfigFileName = AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "mechid-config.json";
41         
42         public static final String SYSTEM_GFP_IP = "GFP";
43         public static final String SYSTEM_GCP = "GCP";
44         public static final String SYSTEM_DCAE = "DCAE";
45         public static final String SYSTEM_RUBY = "RUBY";
46         public static final String SYSTEM_ACTION = "ACTION";
47         public static final String SYSTEM_INSTAR = "INSTAR-LPP-AMS";
48         public static final String FILE_CLASS_GFP_IP = "GFP-IP";
49         public static final String FILE_CLASS_INSTAR = "INSTAR-LPP-AMS";
50         
51         public static HashMap<String, String> mechIdtoSystem = new HashMap<String, String>();
52         public static HashMap<String, ArrayList<String>> fileClassToMechId = new HashMap<String, ArrayList<String>>();
53         
54         /**
55          * Inits the.
56          *
57          * @param tId the t id
58          * @param appId the app id
59          * @param logger the logger
60          */
61         public static void init() {
62                 LOGGER.debug("Initializing AAIMechIdConfig");
63                 Boolean enable;
64                 String systemMechId = "";
65                 JSONParser parser = new JSONParser();
66                 
67                 try {
68                         Object obj = parser.parse(new FileReader(mechIdConfigFileName));
69                         JSONObject jsonObject = (JSONObject) obj;
70                         JSONObject mechIds = (JSONObject) jsonObject.get("mech-ids");
71                         
72                         @SuppressWarnings("unchecked")
73                         Set<String> systemSet = mechIds.keySet();
74                         for (String system : systemSet) {
75                                 JSONObject systemJsonObj = (JSONObject) mechIds.get(system);
76                                 systemMechId = (String) systemJsonObj.get("mechid");
77                                 enable = (Boolean) systemJsonObj.get("enable");
78                                 if (systemMechId != null && !systemMechId.isEmpty() && enable != null && enable == true) {
79                                         mechIdtoSystem.put(systemMechId, system);
80                                         JSONArray fileClasses = (JSONArray) systemJsonObj.get("file-classes");
81                                         if (fileClasses != null ) {
82                                                 String fileClass = "";
83                                                 for (Object fileClassObj : fileClasses) {
84                                                         fileClass = (String) fileClassObj;
85                                                 
86                                                         if (!fileClassToMechId.containsKey(fileClass)) {
87                                                                 fileClassToMechId.put(fileClass, new ArrayList<String>());
88                                                                 fileClassToMechId.get(fileClass).add(systemMechId);
89                                                         } else {
90                                                                 if(!fileClassToMechId.get(fileClass).contains(systemMechId)){
91                                                                         fileClassToMechId.get(fileClass).add(systemMechId);
92                                                                 
93                                                                 }
94                                                         }
95                                                 }
96                                         }
97                                 }
98                         }
99                         
100                 } catch (FileNotFoundException fnfe) {
101                         ErrorLogHelper.logError("AAI_4001", 
102                                         " " + mechIdConfigFileName + ". Exception: " + fnfe.getMessage());
103                 } catch (Exception e) {
104                         ErrorLogHelper.logError("AAI_4004", 
105                                         " " + mechIdConfigFileName + ". Exception: " + e.getMessage());
106                 }
107         }
108
109
110         /**
111          * Transform mech id to pickup dir.
112          *
113          * @param systemMechId the system mech id
114          * @return the string
115          */
116         public static String transformMechIdToPickupDir(String systemMechId) {
117                 String pickupDir = "";
118                 if (systemMechId != null && !systemMechId.isEmpty()) {
119                         pickupDir = "/opt/aaihome/" + systemMechId + "/pickup";
120                         
121                         if (pickupDir != null && !pickupDir.isEmpty() && new File(pickupDir).isDirectory()) {
122                                 return pickupDir;
123                         }
124                         
125                 }
126                 return null;
127         }
128         
129 }