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