aboutsummaryrefslogtreecommitdiffstats
path: root/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/ConfigUtil.java
blob: e0318ba32b5c02f5d2a6ed3287560a0bccd057eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*******************************************************************************
 * Copyright 2016-2017 ZTE, Inc. and others.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package org.onap.msb.apiroute.wrapper.util;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.onap.msb.apiroute.ApiRouteAppConfig;
import org.onap.msb.apiroute.api.DiscoverInfo;
import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

@SuppressWarnings("unchecked")
public class ConfigUtil {
    private final static ConfigUtil instance = new ConfigUtil();


    private ConfigUtil() {}

    public static ConfigUtil getInstance() {
        return instance;
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);

    private String serverPort = "80";

    private String IUI_ROOT_PATH = "iui";

    private String API_ROOT_PATH = "api";

    private String namespaceMatches = "all";

    private String visualRangeMatches = "0";

    private String nodeMetaQueryParam = "";

    private String network_plane_typeMatches = "";

    private String[] routeWay = {"ip"};

    private Map<String, String> labelMapMatches;

    private DiscoverInfo discoverInfo = new DiscoverInfo();

    private String consul_ip = "";

    private String metricsUrl = "http://127.0.0.1:8066/admin/metrics";

    public void initRootPath() {
        String apiRootPathConfSource = "Default";
        String iuiRootPathConfSource = "Default";

        try {

            URL urlRootPath = ConfigUtil.class.getResource("/ext/initUrlRootPath/initUrlRootPath.json");
            if (urlRootPath != null) {
                String path = urlRootPath.getPath();

                LOGGER.warn("read initUrlRootPath:" + path);

                String fileContent = FileUtil.readFile(path);
                ObjectMapper mapper = new ObjectMapper();

                Map<String, String> map = mapper.readValue(fileContent, HashMap.class);
                if (map.get("iuiRootPath") != null) {
                    IUI_ROOT_PATH = map.get("iuiRootPath");
                    iuiRootPathConfSource = "initUrlRootPath.json";
                }
                if (map.get("apiRootPath") != null) {
                    API_ROOT_PATH = map.get("apiRootPath");
                    apiRootPathConfSource = "initUrlRootPath.json";
                }

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LOGGER.error("init UrlRootPath throw exception", e);
        }

        LOGGER.warn("init IUI_ROOT_PATH from [" + iuiRootPathConfSource + "]:" + IUI_ROOT_PATH);
        LOGGER.warn("init API_ROOT_PATH from [" + apiRootPathConfSource + "]:" + API_ROOT_PATH);

    }

    public void initApiGatewayPort() {

        String env_APIGATEWAY_EXPOSE_PORT = System.getenv("APIGATEWAY_EXPOSE_PORT");
        String httpExposePortConfSource = "Default";
        try {
            // read initApiGatewayConfig
            if (StringUtils.isBlank(env_APIGATEWAY_EXPOSE_PORT)) {
                URL apiGatewayConfigPath =
                                ConfigUtil.class.getResource("/ext/initApiGatewayConfig/initApiGatewayConfig.json");
                if (apiGatewayConfigPath != null) {
                    String path = apiGatewayConfigPath.getPath();

                    LOGGER.warn("read initApiGatewayConfig:" + path);

                    String fileContent = FileUtil.readFile(path);
                    ObjectMapper mapper = new ObjectMapper();

                    Map<String, Object> labelMap = mapper.readValue(fileContent, Map.class);
                    if (labelMap.get("port") != null) {
                        serverPort = (String) labelMap.get("port");
                        httpExposePortConfSource = "initApiGatewayConfig.json";
                    }
                }
            } else {
                serverPort = env_APIGATEWAY_EXPOSE_PORT;
                httpExposePortConfSource = "env:APIGATEWAY_EXPOSE_PORT";
            }
            LOGGER.warn("init APIGATEWAY http publish Port from [" + httpExposePortConfSource + "]:" + serverPort);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOGGER.error("read  initApiGatewayConfig Files or env(APIGATEWAY_EXPOSE_PORT) throw exception", e);
        }


    }

    public void initConsulIp() {
        String sys_consulIp = System.getenv("CONSUL_IP");
        if (StringUtils.isNotBlank(sys_consulIp)) {
            consul_ip = sys_consulIp;
            LOGGER.warn("init consul_Ip  from [env:CONSUL_IP]:" + sys_consulIp);
        } else {
            LOGGER.warn("init consul_Ip  from [env:CONSUL_IP] is blank");
        }


    }

    public void initRouteNameSpaceMatches() {
        String env_NAMESPACE = System.getenv("NAMESPACE");
        String namespaceConfSource = "Default";
        try {
            // read NAMESPACE
            if (StringUtils.isBlank(env_NAMESPACE)) {
                URL routeLabelsPath = InitRouteServiceWrapper.class
                                .getResource("/ext/initRouteLabels/initRouteLabelsMatches.json");
                if (routeLabelsPath != null) {
                    String path = routeLabelsPath.getPath();

                    String fileContent = FileUtil.readFile(path);
                    ObjectMapper mapper = new ObjectMapper();

                    Map<String, Object> labelMap = mapper.readValue(fileContent, Map.class);
                    if (labelMap.get("namespace") != null) {
                        namespaceMatches = (String) labelMap.get("namespace");
                        namespaceConfSource = "initRouteLabelsMatches.json";
                    }
                }
            } else {
                namespaceMatches = env_NAMESPACE;
                namespaceConfSource = "env:NAMESPACE";
            }
            LOGGER.warn("init namespace Filter from [" + namespaceConfSource + "]:" + namespaceMatches);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOGGER.error("read  initRouteNameSpaceMatches Files or env(NAMESPACE) throw exception", e);
        }



    }

    /**
     * @Title: initRouteLabelsMatches
     * @Description: TODO(According to the environment variable or a JSON file configuration
     *               initialization Route filter conditions)
     * @return: void
     */
    public void initRouteLabelsMatches() {
        String env_ROUTE_LABELS = System.getenv("ROUTE_LABELS");
        String visualRangeConfSource = "Default";
        String networkPlaneConfSource = "Default";
        String labelConfSource = "Default";
        try {

            // read ROUTE_LABELS
            if (StringUtils.isBlank(env_ROUTE_LABELS)) {
                URL routeLabelsPath = InitRouteServiceWrapper.class
                                .getResource("/ext/initRouteLabels/initRouteLabelsMatches.json");
                if (routeLabelsPath != null) {
                    String path = routeLabelsPath.getPath();

                    String fileContent = FileUtil.readFile(path);
                    ObjectMapper mapper = new ObjectMapper();

                    Map<?, ?> labelMap = mapper.readValue(fileContent, Map.class);
                    if (labelMap.get("predefineLabels") != null) {
                        Map<String, String> predefineLabelMapMatches =
                                        (Map<String, String>) labelMap.get("predefineLabels");
                        if (predefineLabelMapMatches.get("visualRange") != null) {
                            visualRangeMatches = predefineLabelMapMatches.get("visualRange");
                            visualRangeConfSource = "initRouteLabelsMatches.json";
                        }
                        if (predefineLabelMapMatches.get("network_plane_type") != null) {
                            network_plane_typeMatches = predefineLabelMapMatches.get("network_plane_type");
                            networkPlaneConfSource = "initRouteLabelsMatches.json";
                        }
                    }

                    if (labelMap.get("customLabels") != null) {
                        labelMapMatches = (Map<String, String>) labelMap.get("customLabels");
                        labelConfSource = "initRouteLabelsMatches.json";
                    }

                }
            } else {
                String[] env_routeLabels = StringUtils.split(env_ROUTE_LABELS, ",");
                Map<String, String> labelMap = new HashMap<String, String>();

                for (int i = 0; i < env_routeLabels.length; i++) {
                    String[] labels = StringUtils.split(env_routeLabels[i], ":");

                    if ("visualRange".equals(labels[0])) {
                        visualRangeMatches = labels[1];
                        visualRangeConfSource = "env:ROUTE_LABELS";
                    } else if ("network_plane_type".equals(labels[0])) {
                        network_plane_typeMatches = labels[1];
                        networkPlaneConfSource = "env:ROUTE_LABELS";
                    } else {
                        labelMap.put(labels[0], labels[1]);
                    }

                }

                labelConfSource = "env:ROUTE_LABELS";
                labelMapMatches = labelMap;

            }
            LOGGER.warn("init visualRange Filter from [ " + visualRangeConfSource + " ]:" + visualRangeMatches);
            LOGGER.warn("init network_plane_type Filter from [ " + networkPlaneConfSource + " ]:"
                            + network_plane_typeMatches);
            LOGGER.warn("init customLabels Filter from [ " + labelConfSource + " ]:" + labelMapMatches);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LOGGER.error("read  initRouteLabelsPathMatches Files or env(ROUTE_LABELS) throw exception", e);
        }
    }

    public void initRouteWay() {
        String env_ROUTE_WAY = System.getenv("ROUTE_WAY");
        try {
            // read NAMESPACE
            if (StringUtils.isBlank(env_ROUTE_WAY)) {
                URL routeLabelsPath = InitRouteServiceWrapper.class.getResource("/ext/initRouteWay/initRouteWay.json");
                if (routeLabelsPath != null) {
                    String path = routeLabelsPath.getPath();

                    String fileContent = FileUtil.readFile(path);
                    ObjectMapper mapper = new ObjectMapper();

                    Map<String, Object> routeWayMap = mapper.readValue(fileContent, Map.class);
                    String routeWayFromConfig = (String) routeWayMap.get("routeWay");
                    if (StringUtils.isNotBlank(routeWayFromConfig)) {
                        routeWay = StringUtils.split(routeWayFromConfig, RouteUtil.SPLIT_LINE);
                        LOGGER.warn("init RouteWay from [initRouteWay.json]:" + routeWayFromConfig);
                    }
                }
            } else {
                routeWay = StringUtils.split(env_ROUTE_WAY, RouteUtil.SPLIT_LINE);
                LOGGER.warn("read initRouteWay from [env:ROUTE_WAY]:" + env_ROUTE_WAY);
            }



        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOGGER.error("read  initRouteWay Files or env(ROUTE_WAY) throw exception", e);
        }
    }


    public void initDiscoverInfo(ApiRouteAppConfig configuration) {
        DiscoverInfo config_discoverInfo = configuration.getDiscoverInfo();


        discoverInfo.setEnabled(config_discoverInfo.isEnabled());

        String discoverInfoConfSource = "yaml config";

        if (config_discoverInfo.isEnabled()) {

            String discoverIP;
            String env_SDCLIENT_IP = System.getenv("SDCLIENT_IP");

            if (StringUtils.isBlank(env_SDCLIENT_IP)) {
                // yml
                discoverIP = config_discoverInfo.getIp();
            } else {
                discoverIP = env_SDCLIENT_IP;
                discoverInfoConfSource = "env:SDCLIENT_IP";
            }

            discoverInfo.setIp(discoverIP.trim());
            discoverInfo.setPort(config_discoverInfo.getPort());
        }

        LOGGER.warn("init DiscoverInfo from [" + discoverInfoConfSource + "]--" + discoverInfo.toString() + " Enabled:"
                        + discoverInfo.isEnabled());
    }

    public void initNodeMetaQueryParam() {
        // judge consul register node:caltalog
        String env_CONSUL_REGISTER_MODE = System.getenv("CONSUL_REGISTER_MODE");

        if (env_CONSUL_REGISTER_MODE == null || !env_CONSUL_REGISTER_MODE.trim().equals("catalog")) {
            nodeMetaQueryParam = "";
            return;
        }

        // visual range
        String nodemeta_visualrange = nodemeta_visualrange(visualRangeMatches);

        LOGGER.warn("calc nodemeta_visualrange from [" + visualRangeMatches + "]:" + nodemeta_visualrange);

        nodeMetaQueryParam = nodemeta_visualrange;

        // name space
        String nodemeta_namespace = nodemeta_namespace(namespaceMatches);
        LOGGER.warn("calc nodemeta_namespace from [" + namespaceMatches + "]:" + nodemeta_namespace);

        if (!nodeMetaQueryParam.isEmpty() && !nodemeta_namespace.isEmpty()) {
            nodeMetaQueryParam += "&";
        }
        nodeMetaQueryParam += nodemeta_namespace;

        /*
         * // nodemeta = (!nodemeta_visualrange.isEmpty() && !nodemeta_namespace .isEmpty()) ?
         * nodemeta_visualrange + "&" + nodemeta_namespace : nodemeta_visualrange +
         * nodemeta_namespace;
         */

    }

    private String nodemeta_visualrange(final String visualRangeMatches) {

        if (visualRangeMatches == null || visualRangeMatches.isEmpty()) {
            return "";
        }

        // external:0
        if (visualRangeMatches.trim().equals("0")) {
            return "node-meta=external:true";
        }

        // internal:1
        if (visualRangeMatches.trim().equals("1")) {
            return "node-meta=internal:true";
        }

        return "";
    }


    private String nodemeta_namespace(final String namespaceMatches) {

        // exclude null,"",all,&,|,!
        if (namespaceMatches == null || namespaceMatches.isEmpty() || namespaceMatches.contains("all")
                        || namespaceMatches.contains("&") || namespaceMatches.contains("|")
                        || namespaceMatches.contains("!")) {
            return "";
        }

        return "node-meta=ns:" + namespaceMatches;
    }

    public String getServerPort() {
        return serverPort;
    }

    public String getIUI_ROOT_PATH() {
        return IUI_ROOT_PATH;
    }

    public String getAPI_ROOT_PATH() {
        return API_ROOT_PATH;
    }

    public String getNamespaceMatches() {
        return namespaceMatches;
    }

    public String getVisualRangeMatches() {
        return visualRangeMatches;
    }

    public String getNetwork_plane_typeMatches() {
        return network_plane_typeMatches;
    }

    public String[] getRouteWay() {
        return routeWay.clone();
    }

    public Map<String, String> getLabelMapMatches() {
        return labelMapMatches;
    }

    public DiscoverInfo getDiscoverInfo() {
        return discoverInfo;
    }

    public String getMetricsUrl() {
        return metricsUrl;
    }

    public void setMetricsUrl(String metricsUrl) {
        this.metricsUrl = metricsUrl;
    }

    public String getNodeMetaQueryParam() {
        return nodeMetaQueryParam;
    }

    public String getConsul_ip() {
        return consul_ip;
    }



}