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