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