1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
16 import java.io.IOException;
18 import java.util.HashMap;
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;
28 import com.fasterxml.jackson.databind.ObjectMapper;
30 @SuppressWarnings("unchecked")
31 public class ConfigUtil {
32 private final static ConfigUtil instance = new ConfigUtil();
35 private ConfigUtil() {}
37 public static ConfigUtil getInstance() {
41 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);
43 private String serverPort = "80";
45 private String IUI_ROOT_PATH = "iui";
47 private String API_ROOT_PATH = "api";
49 private String namespaceMatches = "all";
51 private String visualRangeMatches = "0";
53 private String nodeMetaQueryParam = "";
55 private String network_plane_typeMatches = "";
57 private String[] routeWay = {"ip"};
59 private Map<String, String> labelMapMatches;
61 private DiscoverInfo discoverInfo = new DiscoverInfo();
63 private String consul_ip = "";
65 private String metricsUrl = "http://127.0.0.1:8066/admin/metrics";
67 public void initRootPath() {
68 String apiRootPathConfSource = "Default";
69 String iuiRootPathConfSource = "Default";
73 URL urlRootPath = ConfigUtil.class.getResource("/ext/initUrlRootPath/initUrlRootPath.json");
74 if (urlRootPath != null) {
75 String path = urlRootPath.getPath();
77 LOGGER.warn("read initUrlRootPath:" + path);
79 String fileContent = FileUtil.readFile(path);
80 ObjectMapper mapper = new ObjectMapper();
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";
87 if (map.get("apiRootPath") != null) {
88 API_ROOT_PATH = map.get("apiRootPath");
89 apiRootPathConfSource = "initUrlRootPath.json";
93 } catch (IOException e) {
94 // TODO Auto-generated catch block
95 LOGGER.error("init UrlRootPath throw exception", e);
98 LOGGER.warn("init IUI_ROOT_PATH from [" + iuiRootPathConfSource + "]:" + IUI_ROOT_PATH);
99 LOGGER.warn("init API_ROOT_PATH from [" + apiRootPathConfSource + "]:" + API_ROOT_PATH);
103 public void initApiGatewayPort() {
105 String env_APIGATEWAY_EXPOSE_PORT = System.getenv("APIGATEWAY_EXPOSE_PORT");
106 String httpExposePortConfSource = "Default";
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();
115 LOGGER.warn("read initApiGatewayConfig:" + path);
117 String fileContent = FileUtil.readFile(path);
118 ObjectMapper mapper = new ObjectMapper();
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";
127 serverPort = env_APIGATEWAY_EXPOSE_PORT;
128 httpExposePortConfSource = "env:APIGATEWAY_EXPOSE_PORT";
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);
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);
145 LOGGER.warn("init consul_Ip from [env:CONSUL_IP] is blank");
151 public void initRouteNameSpaceMatches() {
152 String env_NAMESPACE = System.getenv("NAMESPACE");
153 String namespaceConfSource = "Default";
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();
162 String fileContent = FileUtil.readFile(path);
163 ObjectMapper mapper = new ObjectMapper();
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";
172 namespaceMatches = env_NAMESPACE;
173 namespaceConfSource = "env:NAMESPACE";
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);
186 * @Title: initRouteLabelsMatches
187 * @Description: TODO(According to the environment variable or a JSON file configuration
188 * initialization Route filter conditions)
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";
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();
205 String fileContent = FileUtil.readFile(path);
206 ObjectMapper mapper = new ObjectMapper();
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";
216 if (predefineLabelMapMatches.get("network_plane_type") != null) {
217 network_plane_typeMatches = predefineLabelMapMatches.get("network_plane_type");
218 networkPlaneConfSource = "initRouteLabelsMatches.json";
222 if (labelMap.get("customLabels") != null) {
223 labelMapMatches = (Map<String, String>) labelMap.get("customLabels");
224 labelConfSource = "initRouteLabelsMatches.json";
229 String[] env_routeLabels = StringUtils.split(env_ROUTE_LABELS, ",");
230 Map<String, String> labelMap = new HashMap<String, String>();
232 for (int i = 0; i < env_routeLabels.length; i++) {
233 String[] labels = StringUtils.split(env_routeLabels[i], ":");
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";
242 labelMap.put(labels[0], labels[1]);
247 labelConfSource = "env:ROUTE_LABELS";
248 labelMapMatches = labelMap;
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);
261 public void initRouteWay() {
262 String env_ROUTE_WAY = System.getenv("ROUTE_WAY");
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();
270 String fileContent = FileUtil.readFile(path);
271 ObjectMapper mapper = new ObjectMapper();
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);
281 routeWay = StringUtils.split(env_ROUTE_WAY, RouteUtil.SPLIT_LINE);
282 LOGGER.warn("read initRouteWay from [env:ROUTE_WAY]:" + env_ROUTE_WAY);
287 } catch (Exception e) {
288 // TODO Auto-generated catch block
289 LOGGER.error("read initRouteWay Files or env(ROUTE_WAY) throw exception", e);
294 public void initDiscoverInfo(ApiRouteAppConfig configuration) {
295 DiscoverInfo config_discoverInfo = configuration.getDiscoverInfo();
298 discoverInfo.setEnabled(config_discoverInfo.isEnabled());
300 String discoverInfoConfSource = "yaml config";
302 if (config_discoverInfo.isEnabled()) {
305 String env_SDCLIENT_IP = System.getenv("SDCLIENT_IP");
307 if (StringUtils.isBlank(env_SDCLIENT_IP)) {
309 discoverIP = config_discoverInfo.getIp();
311 discoverIP = env_SDCLIENT_IP;
312 discoverInfoConfSource = "env:SDCLIENT_IP";
315 discoverInfo.setIp(discoverIP.trim());
316 discoverInfo.setPort(config_discoverInfo.getPort());
319 LOGGER.warn("init DiscoverInfo from [" + discoverInfoConfSource + "]--" + discoverInfo.toString() + " Enabled:"
320 + discoverInfo.isEnabled());
323 public void initNodeMetaQueryParam() {
324 // judge consul register node:caltalog
325 String env_CONSUL_REGISTER_MODE = System.getenv("CONSUL_REGISTER_MODE");
327 if (env_CONSUL_REGISTER_MODE == null || !env_CONSUL_REGISTER_MODE.trim().equals("catalog")) {
328 nodeMetaQueryParam = "";
333 String nodemeta_visualrange = nodemeta_visualrange(visualRangeMatches);
335 LOGGER.warn("calc nodemeta_visualrange from [" + visualRangeMatches + "]:" + nodemeta_visualrange);
337 nodeMetaQueryParam = nodemeta_visualrange;
340 String nodemeta_namespace = nodemeta_namespace(namespaceMatches);
341 LOGGER.warn("calc nodemeta_namespace from [" + namespaceMatches + "]:" + nodemeta_namespace);
343 if (!nodeMetaQueryParam.isEmpty() && !nodemeta_namespace.isEmpty()) {
344 nodeMetaQueryParam += "&";
346 nodeMetaQueryParam += nodemeta_namespace;
349 * // nodemeta = (!nodemeta_visualrange.isEmpty() && !nodemeta_namespace .isEmpty()) ?
350 * nodemeta_visualrange + "&" + nodemeta_namespace : nodemeta_visualrange +
351 * nodemeta_namespace;
356 private String nodemeta_visualrange(final String visualRangeMatches) {
358 if (visualRangeMatches == null || visualRangeMatches.isEmpty()) {
363 if (visualRangeMatches.trim().equals("0")) {
364 return "node-meta=external:true";
368 if (visualRangeMatches.trim().equals("1")) {
369 return "node-meta=internal:true";
376 private String nodemeta_namespace(final String namespaceMatches) {
378 // exclude null,"",all,&,|,!
379 if (namespaceMatches == null || namespaceMatches.isEmpty() || namespaceMatches.contains("all")
380 || namespaceMatches.contains("&") || namespaceMatches.contains("|")
381 || namespaceMatches.contains("!")) {
385 return "node-meta=ns:" + namespaceMatches;
388 public String getServerPort() {
392 public String getIUI_ROOT_PATH() {
393 return IUI_ROOT_PATH;
396 public String getAPI_ROOT_PATH() {
397 return API_ROOT_PATH;
400 public String getNamespaceMatches() {
401 return namespaceMatches;
404 public String getVisualRangeMatches() {
405 return visualRangeMatches;
408 public String getNetwork_plane_typeMatches() {
409 return network_plane_typeMatches;
412 public String[] getRouteWay() {
413 return routeWay.clone();
416 public Map<String, String> getLabelMapMatches() {
417 return labelMapMatches;
420 public DiscoverInfo getDiscoverInfo() {
424 public String getMetricsUrl() {
428 public void setMetricsUrl(String metricsUrl) {
429 this.metricsUrl = metricsUrl;
432 public String getNodeMetaQueryParam() {
433 return nodeMetaQueryParam;
436 public String getConsul_ip() {