Release 1.7.3 DCAEGEN2 VESCollector container
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / configuration / ConfigParsing.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2020 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.dcae.configuration;
22
23 import static io.vavr.API.Try;
24 import static io.vavr.API.Tuple;
25 import static org.onap.dcae.common.publishing.VavrUtils.f;
26 import static org.onap.dcae.configuration.Conversions.toList;
27
28 import io.vavr.collection.Map;
29 import io.vavr.control.Option;
30 import org.json.JSONObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 interface ConfigParsing {
35
36     Logger log = LoggerFactory.getLogger(ConfigParsing.class);
37
38     static Option<JSONObject> getDMaaPConfig(JSONObject configuration) {
39         log.info(f("Getting DMaaP configuration from app configuration: '%s'", configuration));
40         return toList(configuration.toMap().entrySet().iterator())
41             .filter(t -> t.getKey().startsWith("streams_publishes"))
42             .headOption()
43             .flatMap(e -> Try(() -> configuration.getJSONObject(e.getKey())).toOption())
44             .onEmpty(() -> log.warn(f("App configuration '%s' is missing DMaaP configuration ('streams_publishes' key) "
45                 + "or DMaaP configuration is not a valid json document", configuration)))
46             .peek(dMaaPConf -> log.info(f("Found following DMaaP configuration: '%s'", dMaaPConf)));
47     }
48
49     static Map<String, String> getProperties(JSONObject configuration) {
50         log.info(f("Getting properties configuration from app configuration: '%s'", configuration));
51         Map<String, String> confEntries = toList(configuration.toMap().entrySet().iterator())
52             .toMap(e -> Tuple(e.getKey(), String.valueOf(e.getValue())))
53             .filterKeys(e -> !e.startsWith("streams_publishes"));
54         log.info(f("Found following app properties: '%s'", confEntries));
55         return confEntries;
56     }
57 }