Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / ConfigUtil.java
1 package org.onap.msb.apiroute.wrapper.util;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.apache.commons.lang3.StringUtils;
9 import org.onap.msb.apiroute.ApiRouteAppConfig;
10 import org.onap.msb.apiroute.api.DiscoverInfo;
11 import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import com.fasterxml.jackson.databind.ObjectMapper;
16
17 @SuppressWarnings("unchecked")
18 public class ConfigUtil {
19   private final static ConfigUtil instance = new ConfigUtil();
20
21
22   private ConfigUtil() {}
23
24   public static ConfigUtil getInstance() {
25     return instance;
26   }
27   
28   private static final Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);
29   
30   private String serverPort="80";
31   
32   private String IUI_ROOT_PATH="iui";  
33   
34   private  String API_ROOT_PATH="api"; 
35   
36   private String namespaceMatches="all";
37   
38   private String visualRangeMatches="0";
39   
40   private String nodeMetaQueryParam="";
41   
42   private String network_plane_typeMatches="";
43   
44   private String[] routeWay={"ip"};
45   
46   private Map<String,String> labelMapMatches;
47   
48   private DiscoverInfo discoverInfo=new DiscoverInfo();  
49   
50   private String consul_ip="";
51   
52   private  String metricsUrl = "http://127.0.0.1:8066/admin/metrics";
53   
54   public void initRootPath() {
55     String apiRootPathConfSource="Default";
56     String iuiRootPathConfSource="Default";
57     
58     try {
59       
60       URL urlRootPath =
61           ConfigUtil.class.getResource("/ext/initUrlRootPath/initUrlRootPath.json");
62       if (urlRootPath != null) {
63         String path = urlRootPath.getPath();
64
65         LOGGER.warn("read initUrlRootPath:" + path);
66
67         String fileContent = FileUtil.readFile(path);
68         ObjectMapper mapper = new ObjectMapper();
69       
70         Map<String, String> map = mapper.readValue(fileContent, HashMap.class);
71         if (map.get("iuiRootPath") != null) {
72           IUI_ROOT_PATH = map.get("iuiRootPath");
73           iuiRootPathConfSource="initUrlRootPath.json";
74         }
75         if (map.get("apiRootPath") != null) {
76           API_ROOT_PATH = map.get("apiRootPath");
77           apiRootPathConfSource="initUrlRootPath.json";
78         }
79        
80       }
81     } catch (IOException e) {
82       // TODO Auto-generated catch block
83       LOGGER.error("init UrlRootPath throw exception", e);
84     }
85     
86     LOGGER.warn("init IUI_ROOT_PATH from ["+iuiRootPathConfSource+"]:"+IUI_ROOT_PATH);
87     LOGGER.warn("init API_ROOT_PATH from ["+apiRootPathConfSource+"]:"+API_ROOT_PATH);
88
89   }
90   
91   public void initApiGatewayPort() {
92     
93     String env_APIGATEWAY_EXPOSE_PORT=System.getenv("APIGATEWAY_EXPOSE_PORT");
94     String httpExposePortConfSource="Default";
95     try {
96       // read initApiGatewayConfig
97       if (StringUtils.isBlank(env_APIGATEWAY_EXPOSE_PORT)) {
98         URL apiGatewayConfigPath =
99             ConfigUtil.class
100                 .getResource("/ext/initApiGatewayConfig/initApiGatewayConfig.json");
101         if (apiGatewayConfigPath != null) {
102           String path = apiGatewayConfigPath.getPath();
103
104           LOGGER.warn("read initApiGatewayConfig:" + path);
105
106           String fileContent = FileUtil.readFile(path);
107           ObjectMapper mapper = new ObjectMapper();
108
109           Map<String, Object> labelMap = mapper.readValue(fileContent, Map.class);
110           if (labelMap.get("port") != null) {
111             serverPort = (String) labelMap.get("port");
112             httpExposePortConfSource="initApiGatewayConfig.json";
113           }
114         }
115       } else {
116            serverPort = env_APIGATEWAY_EXPOSE_PORT;
117            httpExposePortConfSource="env:APIGATEWAY_EXPOSE_PORT";
118       }
119       LOGGER.warn("init APIGATEWAY http publish Port from ["+httpExposePortConfSource+"]:"+serverPort);
120     } catch (Exception e) {
121       // TODO Auto-generated catch block
122       LOGGER.error(
123           "read  initApiGatewayConfig Files or env(APIGATEWAY_EXPOSE_PORT) throw exception", e);
124     }
125     
126    
127   }
128   
129   public void initConsulIp() {
130     String sys_consulIp=System.getenv("CONSUL_IP");
131     if (StringUtils.isNotBlank(sys_consulIp)) {
132       consul_ip=sys_consulIp;
133       LOGGER.warn("init consul_Ip  from [env:CONSUL_IP]:" + sys_consulIp);
134     }
135     else{
136       LOGGER.warn("init consul_Ip  from [env:CONSUL_IP] is blank");
137     }
138     
139
140   }
141   
142   public void initRouteNameSpaceMatches() {
143     String env_NAMESPACE=System.getenv("NAMESPACE");
144     String namespaceConfSource="Default";
145     try {
146       // read NAMESPACE
147       if (StringUtils.isBlank(env_NAMESPACE)) {
148         URL routeLabelsPath =
149             InitRouteServiceWrapper.class
150                 .getResource("/ext/initRouteLabels/initRouteLabelsMatches.json");
151         if (routeLabelsPath != null) {
152           String path = routeLabelsPath.getPath();
153
154           String fileContent = FileUtil.readFile(path);
155           ObjectMapper mapper = new ObjectMapper();
156
157           Map<String, Object> labelMap = mapper.readValue(fileContent, Map.class);
158           if (labelMap.get("namespace") != null) {
159             namespaceMatches = (String) labelMap.get("namespace");
160             namespaceConfSource="initRouteLabelsMatches.json";
161           }
162         }
163       } else {
164         namespaceMatches =env_NAMESPACE;
165         namespaceConfSource="env:NAMESPACE";
166       }
167       LOGGER.warn("init namespace Filter from ["+namespaceConfSource+"]:" + namespaceMatches);
168     }
169     catch (Exception e) {
170       // TODO Auto-generated catch block
171       LOGGER.error("read  initRouteNameSpaceMatches Files or env(NAMESPACE) throw exception",
172           e);
173     }
174     
175   
176
177  }
178   /**
179    * @Title: initRouteLabelsMatches
180    * @Description: TODO(According to the environment variable or a JSON file configuration
181    *               initialization Route filter conditions)
182    * @return: void
183    */
184   public void initRouteLabelsMatches() {
185     String env_ROUTE_LABELS=System.getenv("ROUTE_LABELS");
186     String visualRangeConfSource="Default";
187     String networkPlaneConfSource="Default";
188     String labelConfSource="Default";
189     try {
190      
191       // read ROUTE_LABELS
192       if (StringUtils.isBlank(env_ROUTE_LABELS)) {
193         URL routeLabelsPath =
194             InitRouteServiceWrapper.class
195                 .getResource("/ext/initRouteLabels/initRouteLabelsMatches.json");
196         if (routeLabelsPath != null) {
197           String path = routeLabelsPath.getPath();
198
199           String fileContent = FileUtil.readFile(path);
200           ObjectMapper mapper = new ObjectMapper();
201
202           Map<?, ?> labelMap = mapper.readValue(fileContent, Map.class);
203           if (labelMap.get("predefineLabels") != null) {
204             Map<String, String> predefineLabelMapMatches =
205                 (Map<String, String>) labelMap.get("predefineLabels");
206             if (predefineLabelMapMatches.get("visualRange") != null) {
207               visualRangeMatches = predefineLabelMapMatches.get("visualRange");
208               visualRangeConfSource="initRouteLabelsMatches.json";
209             }
210             if (predefineLabelMapMatches.get("network_plane_type") != null) {
211               network_plane_typeMatches =
212                   predefineLabelMapMatches.get("network_plane_type");
213               networkPlaneConfSource="initRouteLabelsMatches.json";
214             }
215           }
216
217           if (labelMap.get("customLabels") != null) {
218             labelMapMatches = (Map<String, String>) labelMap.get("customLabels");
219             labelConfSource="initRouteLabelsMatches.json";
220           }
221
222         }
223       } else {
224         String[] env_routeLabels = StringUtils.split(env_ROUTE_LABELS, ",");
225         Map<String, String> labelMap = new HashMap<String, String>();
226
227         for (int i = 0; i < env_routeLabels.length; i++) {
228           String[] labels = StringUtils.split(env_routeLabels[i], ":");
229
230           if ("visualRange".equals(labels[0])) {
231             visualRangeMatches = labels[1];
232             visualRangeConfSource="env:ROUTE_LABELS";
233           } else if ("network_plane_type".equals(labels[0])) {
234             network_plane_typeMatches = labels[1];
235             networkPlaneConfSource="env:ROUTE_LABELS";
236           } else {
237             labelMap.put(labels[0], labels[1]);
238           }
239
240         }
241
242         labelConfSource="env:ROUTE_LABELS";
243         labelMapMatches = labelMap;
244
245       }
246       LOGGER.warn("init visualRange Filter from [ "+visualRangeConfSource+" ]:" + visualRangeMatches);
247       LOGGER.warn("init network_plane_type Filter from [ "+networkPlaneConfSource+" ]:" + network_plane_typeMatches);
248       LOGGER.warn("init customLabels Filter from [ "+labelConfSource+" ]:" + labelMapMatches);
249     } catch (IOException e) {
250       // TODO Auto-generated catch block
251       LOGGER.error(
252           "read  initRouteLabelsPathMatches Files or env(ROUTE_LABELS) throw exception",
253           e);
254     }
255   }
256   
257   public void initRouteWay() {
258     String env_ROUTE_WAY=System.getenv("ROUTE_WAY");
259     try {
260       // read NAMESPACE
261       if (StringUtils.isBlank(env_ROUTE_WAY)) {
262         URL routeLabelsPath =
263             InitRouteServiceWrapper.class.getResource("/ext/initRouteWay/initRouteWay.json");
264         if (routeLabelsPath != null) {
265           String path = routeLabelsPath.getPath();
266
267           String fileContent = FileUtil.readFile(path);
268           ObjectMapper mapper = new ObjectMapper();
269
270           Map<String, Object> routeWayMap = mapper.readValue(fileContent, Map.class);
271           String routeWayFromConfig=(String)routeWayMap.get("routeWay");
272           if (StringUtils.isNotBlank(routeWayFromConfig)) {
273             routeWay =
274                 StringUtils.split(routeWayFromConfig, RouteUtil.SPLIT_LINE);
275             LOGGER.warn("init RouteWay from [initRouteWay.json]:" + routeWayFromConfig);
276           }
277         }
278       } else {
279         routeWay = StringUtils.split(env_ROUTE_WAY, RouteUtil.SPLIT_LINE);
280         LOGGER.warn("read initRouteWay from [env:ROUTE_WAY]:" + env_ROUTE_WAY);
281       }
282       
283
284   
285
286
287     } catch (Exception e) {
288       // TODO Auto-generated catch block
289       LOGGER.error("read  initRouteWay Files or env(ROUTE_WAY) throw exception", e);
290     }
291   }
292
293   
294   public void initDiscoverInfo(ApiRouteAppConfig configuration){
295     DiscoverInfo config_discoverInfo = configuration.getDiscoverInfo();
296
297
298     discoverInfo.setEnabled(config_discoverInfo.isEnabled());
299
300     String discoverInfoConfSource="yaml config";
301
302     if (config_discoverInfo.isEnabled()) {
303       
304      String discoverIP;
305      String env_SDCLIENT_IP=System.getenv("SDCLIENT_IP");
306     
307         if (StringUtils.isBlank(env_SDCLIENT_IP)) {
308           // yml
309           discoverIP = config_discoverInfo.getIp();
310         } else {
311           discoverIP = env_SDCLIENT_IP;
312           discoverInfoConfSource="env:SDCLIENT_IP";
313         }
314
315        discoverInfo.setIp(discoverIP.trim());
316        discoverInfo.setPort(config_discoverInfo.getPort());
317     }
318     
319     LOGGER.warn("init DiscoverInfo from ["+discoverInfoConfSource+"]--" + discoverInfo.toString()+" Enabled:"+discoverInfo.isEnabled());
320   }
321   
322         public void initNodeMetaQueryParam() {
323                 // judge consul register node:caltalog
324                 String env_CONSUL_REGISTER_MODE = System.getenv("CONSUL_REGISTER_MODE");
325
326                 if (env_CONSUL_REGISTER_MODE == null
327                                 || !env_CONSUL_REGISTER_MODE.trim().equals("catalog")) {
328                         nodeMetaQueryParam = "";
329                         return;
330                 }
331
332                 // visual range
333                 String nodemeta_visualrange = nodemeta_visualrange(visualRangeMatches);
334
335                 LOGGER.warn("calc nodemeta_visualrange from [" + visualRangeMatches
336                                 + "]:" + nodemeta_visualrange);
337
338                 nodeMetaQueryParam = nodemeta_visualrange;
339
340                 // name space
341                 String nodemeta_namespace = nodemeta_namespace(namespaceMatches);
342                 LOGGER.warn("calc nodemeta_namespace from [" + namespaceMatches + "]:"
343                                 + nodemeta_namespace);
344
345                 if (!nodeMetaQueryParam.isEmpty() && !nodemeta_namespace.isEmpty()) {
346                         nodeMetaQueryParam += "&";
347                 }
348                 nodeMetaQueryParam += nodemeta_namespace;
349
350                 /*
351                  * // nodemeta = (!nodemeta_visualrange.isEmpty() && !nodemeta_namespace
352                  * .isEmpty()) ? nodemeta_visualrange + "&" + nodemeta_namespace :
353                  * nodemeta_visualrange + nodemeta_namespace;
354                  */
355
356         }
357
358         private String nodemeta_visualrange(final String visualRangeMatches) {
359
360                 if (visualRangeMatches == null || visualRangeMatches.isEmpty()) {
361                         return "";
362                 }
363
364                 // external:0
365                 if (visualRangeMatches.trim().equals("0")) {
366                         return "node-meta=external:true";
367                 }
368
369                 // internal:1
370                 if (visualRangeMatches.trim().equals("1")) {
371                         return "node-meta=internal:true";
372                 }
373
374                 return "";
375         }
376         
377
378         private String nodemeta_namespace(final String namespaceMatches) {
379
380                 // exclude null,"",all,&,|,!
381                 if (namespaceMatches == null || namespaceMatches.isEmpty()
382                                 || namespaceMatches.contains("all")
383                                 || namespaceMatches.contains("&")
384                                 || namespaceMatches.contains("|")
385                                 || namespaceMatches.contains("!")) {
386                         return "";
387                 }
388
389                 return "node-meta=ns:" + namespaceMatches;
390         }
391   
392   public String getServerPort() {
393     return serverPort;
394   }
395
396   public String getIUI_ROOT_PATH() {
397     return IUI_ROOT_PATH;
398   }
399
400   public String getAPI_ROOT_PATH() {
401     return API_ROOT_PATH;
402   }
403
404   public String getNamespaceMatches() {
405     return namespaceMatches;
406   }
407
408   public String getVisualRangeMatches() {
409     return visualRangeMatches;
410   }
411
412   public String getNetwork_plane_typeMatches() {
413     return network_plane_typeMatches;
414   }
415   
416   public String[] getRouteWay() {
417     return routeWay.clone();
418   }
419
420   public Map<String, String> getLabelMapMatches() {
421     return labelMapMatches;
422   }
423
424   public DiscoverInfo getDiscoverInfo() {
425     return discoverInfo;
426   }
427
428   public String getMetricsUrl() {
429     return metricsUrl;
430   }
431
432   public void setMetricsUrl(String metricsUrl) {
433     this.metricsUrl = metricsUrl;
434   }
435
436         public String getNodeMetaQueryParam() {
437                 return nodeMetaQueryParam;
438         }
439
440   public String getConsul_ip() {
441     return consul_ip;
442   }
443
444
445
446 }