1 package org.onap.msb.apiroute.wrapper.util;
3 import java.io.IOException;
5 import java.util.HashMap;
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;
15 import com.fasterxml.jackson.databind.ObjectMapper;
17 @SuppressWarnings("unchecked")
18 public class ConfigUtil {
19 private final static ConfigUtil instance = new ConfigUtil();
22 private ConfigUtil() {}
24 public static ConfigUtil getInstance() {
28 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);
30 private String serverPort="80";
32 private String IUI_ROOT_PATH="iui";
34 private String API_ROOT_PATH="api";
36 private String namespaceMatches="all";
38 private String visualRangeMatches="0";
40 private String nodeMetaQueryParam="";
42 private String network_plane_typeMatches="";
44 private String[] routeWay={"ip"};
46 private Map<String,String> labelMapMatches;
48 private DiscoverInfo discoverInfo=new DiscoverInfo();
50 private String consul_ip="";
52 private String metricsUrl = "http://127.0.0.1:8066/admin/metrics";
54 public void initRootPath() {
55 String apiRootPathConfSource="Default";
56 String iuiRootPathConfSource="Default";
61 ConfigUtil.class.getResource("/ext/initUrlRootPath/initUrlRootPath.json");
62 if (urlRootPath != null) {
63 String path = urlRootPath.getPath();
65 LOGGER.warn("read initUrlRootPath:" + path);
67 String fileContent = FileUtil.readFile(path);
68 ObjectMapper mapper = new ObjectMapper();
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";
75 if (map.get("apiRootPath") != null) {
76 API_ROOT_PATH = map.get("apiRootPath");
77 apiRootPathConfSource="initUrlRootPath.json";
81 } catch (IOException e) {
82 // TODO Auto-generated catch block
83 LOGGER.error("init UrlRootPath throw exception", e);
86 LOGGER.warn("init IUI_ROOT_PATH from ["+iuiRootPathConfSource+"]:"+IUI_ROOT_PATH);
87 LOGGER.warn("init API_ROOT_PATH from ["+apiRootPathConfSource+"]:"+API_ROOT_PATH);
91 public void initApiGatewayPort() {
93 String env_APIGATEWAY_EXPOSE_PORT=System.getenv("APIGATEWAY_EXPOSE_PORT");
94 String httpExposePortConfSource="Default";
96 // read initApiGatewayConfig
97 if (StringUtils.isBlank(env_APIGATEWAY_EXPOSE_PORT)) {
98 URL apiGatewayConfigPath =
100 .getResource("/ext/initApiGatewayConfig/initApiGatewayConfig.json");
101 if (apiGatewayConfigPath != null) {
102 String path = apiGatewayConfigPath.getPath();
104 LOGGER.warn("read initApiGatewayConfig:" + path);
106 String fileContent = FileUtil.readFile(path);
107 ObjectMapper mapper = new ObjectMapper();
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";
116 serverPort = env_APIGATEWAY_EXPOSE_PORT;
117 httpExposePortConfSource="env:APIGATEWAY_EXPOSE_PORT";
119 LOGGER.warn("init APIGATEWAY http publish Port from ["+httpExposePortConfSource+"]:"+serverPort);
120 } catch (Exception e) {
121 // TODO Auto-generated catch block
123 "read initApiGatewayConfig Files or env(APIGATEWAY_EXPOSE_PORT) throw exception", e);
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);
136 LOGGER.warn("init consul_Ip from [env:CONSUL_IP] is blank");
142 public void initRouteNameSpaceMatches() {
143 String env_NAMESPACE=System.getenv("NAMESPACE");
144 String namespaceConfSource="Default";
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();
154 String fileContent = FileUtil.readFile(path);
155 ObjectMapper mapper = new ObjectMapper();
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";
164 namespaceMatches =env_NAMESPACE;
165 namespaceConfSource="env:NAMESPACE";
167 LOGGER.warn("init namespace Filter from ["+namespaceConfSource+"]:" + namespaceMatches);
169 catch (Exception e) {
170 // TODO Auto-generated catch block
171 LOGGER.error("read initRouteNameSpaceMatches Files or env(NAMESPACE) throw exception",
179 * @Title: initRouteLabelsMatches
180 * @Description: TODO(According to the environment variable or a JSON file configuration
181 * initialization Route filter conditions)
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";
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();
199 String fileContent = FileUtil.readFile(path);
200 ObjectMapper mapper = new ObjectMapper();
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";
210 if (predefineLabelMapMatches.get("network_plane_type") != null) {
211 network_plane_typeMatches =
212 predefineLabelMapMatches.get("network_plane_type");
213 networkPlaneConfSource="initRouteLabelsMatches.json";
217 if (labelMap.get("customLabels") != null) {
218 labelMapMatches = (Map<String, String>) labelMap.get("customLabels");
219 labelConfSource="initRouteLabelsMatches.json";
224 String[] env_routeLabels = StringUtils.split(env_ROUTE_LABELS, ",");
225 Map<String, String> labelMap = new HashMap<String, String>();
227 for (int i = 0; i < env_routeLabels.length; i++) {
228 String[] labels = StringUtils.split(env_routeLabels[i], ":");
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";
237 labelMap.put(labels[0], labels[1]);
242 labelConfSource="env:ROUTE_LABELS";
243 labelMapMatches = labelMap;
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
252 "read initRouteLabelsPathMatches Files or env(ROUTE_LABELS) throw exception",
257 public void initRouteWay() {
258 String env_ROUTE_WAY=System.getenv("ROUTE_WAY");
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();
267 String fileContent = FileUtil.readFile(path);
268 ObjectMapper mapper = new ObjectMapper();
270 Map<String, Object> routeWayMap = mapper.readValue(fileContent, Map.class);
271 String routeWayFromConfig=(String)routeWayMap.get("routeWay");
272 if (StringUtils.isNotBlank(routeWayFromConfig)) {
274 StringUtils.split(routeWayFromConfig, RouteUtil.SPLIT_LINE);
275 LOGGER.warn("init RouteWay from [initRouteWay.json]:" + routeWayFromConfig);
279 routeWay = StringUtils.split(env_ROUTE_WAY, RouteUtil.SPLIT_LINE);
280 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:"+discoverInfo.isEnabled());
322 public void initNodeMetaQueryParam() {
323 // judge consul register node:caltalog
324 String env_CONSUL_REGISTER_MODE = System.getenv("CONSUL_REGISTER_MODE");
326 if (env_CONSUL_REGISTER_MODE == null
327 || !env_CONSUL_REGISTER_MODE.trim().equals("catalog")) {
328 nodeMetaQueryParam = "";
333 String nodemeta_visualrange = nodemeta_visualrange(visualRangeMatches);
335 LOGGER.warn("calc nodemeta_visualrange from [" + visualRangeMatches
336 + "]:" + nodemeta_visualrange);
338 nodeMetaQueryParam = nodemeta_visualrange;
341 String nodemeta_namespace = nodemeta_namespace(namespaceMatches);
342 LOGGER.warn("calc nodemeta_namespace from [" + namespaceMatches + "]:"
343 + nodemeta_namespace);
345 if (!nodeMetaQueryParam.isEmpty() && !nodemeta_namespace.isEmpty()) {
346 nodeMetaQueryParam += "&";
348 nodeMetaQueryParam += nodemeta_namespace;
351 * // nodemeta = (!nodemeta_visualrange.isEmpty() && !nodemeta_namespace
352 * .isEmpty()) ? nodemeta_visualrange + "&" + nodemeta_namespace :
353 * nodemeta_visualrange + nodemeta_namespace;
358 private String nodemeta_visualrange(final String visualRangeMatches) {
360 if (visualRangeMatches == null || visualRangeMatches.isEmpty()) {
365 if (visualRangeMatches.trim().equals("0")) {
366 return "node-meta=external:true";
370 if (visualRangeMatches.trim().equals("1")) {
371 return "node-meta=internal:true";
378 private String nodemeta_namespace(final String namespaceMatches) {
380 // exclude null,"",all,&,|,!
381 if (namespaceMatches == null || namespaceMatches.isEmpty()
382 || namespaceMatches.contains("all")
383 || namespaceMatches.contains("&")
384 || namespaceMatches.contains("|")
385 || namespaceMatches.contains("!")) {
389 return "node-meta=ns:" + namespaceMatches;
392 public String getServerPort() {
396 public String getIUI_ROOT_PATH() {
397 return IUI_ROOT_PATH;
400 public String getAPI_ROOT_PATH() {
401 return API_ROOT_PATH;
404 public String getNamespaceMatches() {
405 return namespaceMatches;
408 public String getVisualRangeMatches() {
409 return visualRangeMatches;
412 public String getNetwork_plane_typeMatches() {
413 return network_plane_typeMatches;
416 public String[] getRouteWay() {
417 return routeWay.clone();
420 public Map<String, String> getLabelMapMatches() {
421 return labelMapMatches;
424 public DiscoverInfo getDiscoverInfo() {
428 public String getMetricsUrl() {
432 public void setMetricsUrl(String metricsUrl) {
433 this.metricsUrl = metricsUrl;
436 public String getNodeMetaQueryParam() {
437 return nodeMetaQueryParam;
440 public String getConsul_ip() {