3ac6b2c6785719ef252f011447d0880594957eb0
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 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
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30
31 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
32
33
34 /**
35  * Parses the cloud configuration.
36  *
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
38  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
39  */
40 public class CloudConfigParser {
41     private static final String DMAAP_SECURITY_TRUST_STORE_PATH = "dmaap.security.trustStorePath";
42     private static final String DMAAP_SECURITY_TRUST_STORE_PASS_PATH = "dmaap.security.trustStorePasswordPath";
43     private static final String DMAAP_SECURITY_KEY_STORE_PATH = "dmaap.security.keyStorePath";
44     private static final String DMAAP_SECURITY_KEY_STORE_PASS_PATH = "dmaap.security.keyStorePasswordPath";
45     private static final String DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH = "dmaap.security.enableDmaapCertAuth";
46
47     private final JsonObject serviceConfigurationRoot;
48     private final JsonObject dmaapConfigurationRoot;
49
50     public CloudConfigParser(JsonObject serviceConfigurationRoot, JsonObject dmaapConfigurationRoot) {
51         this.serviceConfigurationRoot = serviceConfigurationRoot;
52         this.dmaapConfigurationRoot = dmaapConfigurationRoot;
53     }
54
55     public Map<String, PublisherConfiguration> getDmaapPublisherConfig() throws DatafileTaskException {
56         Iterator<JsonElement> producerCfgs =
57                 toArray(serviceConfigurationRoot.get("dmaap.dmaapProducerConfiguration")).iterator();
58
59         Map<String, PublisherConfiguration> result = new HashMap<>();
60
61         while (producerCfgs.hasNext()) {
62             JsonObject producerCfg = producerCfgs.next().getAsJsonObject();
63             String feedName = getAsString(producerCfg, "feedName");
64             JsonObject feedConfig = getFeedConfig(feedName);
65
66             PublisherConfiguration cfg = ImmutablePublisherConfiguration.builder() //
67                     .publishUrl(getAsString(feedConfig, "publish_url")) //
68                     .passWord(getAsString(feedConfig, "password")) //
69                     .userName(getAsString(feedConfig, "username")) //
70                     .trustStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PATH)) //
71                     .trustStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PASS_PATH)) //
72                     .keyStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PATH)) //
73                     .keyStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PASS_PATH)) //
74                     .enableDmaapCertAuth(
75                             get(serviceConfigurationRoot, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
76                     .changeIdentifier(getAsString(producerCfg, "changeIdentifier")) //
77                     .logUrl(getAsString(feedConfig, "log_url")) //
78                     .build();
79
80             result.put(cfg.changeIdentifier(), cfg);
81         }
82         return result;
83     }
84
85     public ConsumerConfiguration getDmaapConsumerConfig() throws DatafileTaskException {
86         JsonObject consumerCfg = serviceConfigurationRoot.get("streams_subscribes").getAsJsonObject();
87         Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
88         if (topics.size() != 1) {
89             throw new DatafileTaskException("Invalid configuration, number oftopic must be one, config: " + topics);
90         }
91         JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
92         JsonObject dmaapInfo = get(topic, "dmmap_info").getAsJsonObject();
93         String topicUrl = getAsString(dmaapInfo, "topic_url");
94
95         return ImmutableConsumerConfiguration.builder().topicUrl(topicUrl)
96                 .trustStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PATH))
97                 .trustStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_TRUST_STORE_PASS_PATH))
98                 .keyStorePath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PATH))
99                 .keyStorePasswordPath(getAsString(serviceConfigurationRoot, DMAAP_SECURITY_KEY_STORE_PASS_PATH))
100                 .enableDmaapCertAuth(
101                         get(serviceConfigurationRoot, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
102                 .build();
103     }
104
105     public FtpesConfig getFtpesConfig() throws DatafileTaskException {
106         return new ImmutableFtpesConfig.Builder() //
107                 .keyCert(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.keyCert"))
108                 .keyPassword(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.keyPassword"))
109                 .trustedCa(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.trustedCa"))
110                 .trustedCaPassword(getAsString(serviceConfigurationRoot, "dmaap.ftpesConfig.trustedCaPassword")) //
111                 .build();
112     }
113
114     private static JsonElement get(JsonObject obj, String memberName) throws DatafileTaskException {
115         JsonElement elem = obj.get(memberName);
116         if (elem == null) {
117             throw new DatafileTaskException("Could not find member: " + memberName + " in: " + obj);
118         }
119         return elem;
120     }
121
122     private static String getAsString(JsonObject obj, String memberName) throws DatafileTaskException {
123         return get(obj, memberName).getAsString();
124     }
125
126     private JsonObject getFeedConfig(String feedName) throws DatafileTaskException {
127         JsonElement elem = dmaapConfigurationRoot.get(feedName);
128         if (elem == null) {
129             elem = get(serviceConfigurationRoot, feedName); // Fallback, try to find it under
130                                                             // serviceConfigurationRoot
131         }
132         return elem.getAsJsonObject();
133     }
134
135     private static JsonArray toArray(JsonElement obj) {
136         if (obj.isJsonArray()) {
137             return obj.getAsJsonArray();
138         }
139         JsonArray arr = new JsonArray();
140         arr.add(obj);
141         return arr;
142     }
143 }