0242bef709026e02b9ca85f3a3bde390b68eb35e
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.configuration;
20
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
30
31 /**
32  * Parses the cloud configuration.
33  *
34  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
35  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
36  */
37 public class CloudConfigParser {
38     private static final String DMAAP_SECURITY_TRUST_STORE_PATH = "dmaap.security.trustStorePath";
39     private static final String DMAAP_SECURITY_TRUST_STORE_PASS_PATH = "dmaap.security.trustStorePasswordPath";
40     private static final String DMAAP_SECURITY_KEY_STORE_PATH = "dmaap.security.keyStorePath";
41     private static final String DMAAP_SECURITY_KEY_STORE_PASS_PATH = "dmaap.security.keyStorePasswordPath";
42     private static final String DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH = "dmaap.security.enableDmaapCertAuth";
43
44     private final JsonObject serviceConfigurationRoot;
45     private final JsonObject dmaapConfigurationRoot;
46
47     public CloudConfigParser(JsonObject serviceConfigurationRoot, JsonObject dmaapConfigurationRoot) {
48         this.serviceConfigurationRoot = serviceConfigurationRoot;
49         this.dmaapConfigurationRoot = dmaapConfigurationRoot;
50     }
51
52     /**
53      * Get the publisher configurations.
54      *
55      * @return a map with change identifier as key and the connected publisher configuration as value.
56      *
57      * @throws DatafileTaskException if a member of the configuration is missing.
58      */
59     public Map<String, PublisherConfiguration> getDmaapPublisherConfigurations() throws DatafileTaskException {
60         Iterator<JsonElement> producerCfgs =
61             toArray(serviceConfigurationRoot.get("dmaap.dmaapProducerConfiguration")).iterator();
62
63         Map<String, PublisherConfiguration> result = new HashMap<>();
64
65         while (producerCfgs.hasNext()) {
66             JsonObject producerCfg = producerCfgs.next().getAsJsonObject();
67             String feedName = getAsString(producerCfg, "feedName");
68             JsonObject feedConfig = getFeedConfig(feedName);
69
70             PublisherConfiguration cfg = ImmutablePublisherConfiguration.builder() //
71                 .publishUrl(getAsString(feedConfig, "publish_url")) //
72                 .passWord(getAsString(feedConfig, "password")) //
73                 .userName(getAsString(feedConfig, "username")) //
74                 .trustStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PATH)) //
75                 .trustStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PASS_PATH)) //
76                 .keyStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PATH)) //
77                 .keyStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PASS_PATH)) //
78                 .enableDmaapCertAuth(
79                     get(serviceConfigurationRoot, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
80                 .changeIdentifier(getAsString(producerCfg, "changeIdentifier")) //
81                 .logUrl(getAsString(feedConfig, "log_url")) //
82                 .build();
83
84             result.put(cfg.changeIdentifier(), cfg);
85         }
86         return result;
87     }
88
89     /**
90      * Get the consumer configuration.
91      *
92      * @return the consumer configuration.
93      * @throws DatafileTaskException if a member of the configuration is missing.
94      */
95     public ConsumerConfiguration getDmaapConsumerConfig() throws DatafileTaskException {
96         JsonObject consumerCfg = serviceConfigurationRoot.get("streams_subscribes").getAsJsonObject();
97         Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
98         if (topics.size() != 1) {
99             throw new DatafileTaskException("Invalid configuration, number oftopic must be one, config: " + topics);
100         }
101         JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
102         JsonObject dmaapInfo = get(topic, "dmmap_info").getAsJsonObject();
103         String topicUrl = getAsString(dmaapInfo, "topic_url");
104
105         return ImmutableConsumerConfiguration.builder().topicUrl(topicUrl)
106             .trustStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PATH))
107             .trustStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PASS_PATH))
108             .keyStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PATH))
109             .keyStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PASS_PATH))
110             .enableDmaapCertAuth(get(serviceConfigurationRoot, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
111             .build();
112     }
113
114     /**
115      * Get the security configuration for communication with the xNF.
116      *
117      * @return the xNF communication security configuration.
118      * @throws DatafileTaskException if a member of the configuration is missing.
119      */
120     public FtpesConfig getFtpesConfig() throws DatafileTaskException {
121         return new ImmutableFtpesConfig.Builder() //
122             .keyCert(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.keyCert"))
123             .keyPassword(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.keyPassword"))
124             .trustedCa(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.trustedCa"))
125             .trustedCaPassword(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.trustedCaPassword")) //
126             .build();
127     }
128
129     private static JsonElement get(JsonObject obj, String memberName) throws DatafileTaskException {
130         JsonElement elem = obj.get(memberName);
131         if (elem == null) {
132             throw new DatafileTaskException("Could not find member: " + memberName + " in: " + obj);
133         }
134         return elem;
135     }
136
137     private static String getAsString(JsonObject obj, String memberName) throws DatafileTaskException {
138         return get(obj, memberName).getAsString();
139     }
140
141     private JsonObject getFeedConfig(String feedName) throws DatafileTaskException {
142         JsonElement elem = dmaapConfigurationRoot.get(feedName);
143         if (elem == null) {
144             elem = get(serviceConfigurationRoot, feedName); // Fallback, try to find it under serviceConfigurationRoot
145         }
146         return elem.getAsJsonObject();
147     }
148
149     private static JsonArray toArray(JsonElement obj) {
150         if (obj.isJsonArray()) {
151             return obj.getAsJsonArray();
152         }
153         JsonArray arr = new JsonArray();
154         arr.add(obj);
155         return arr;
156     }
157 }