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