fc550ab07b68f997a08c1420ebbaa6531682e466
[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.JsonElement;
22 import com.google.gson.JsonObject;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
29 import javax.validation.constraints.NotNull;
30
31 import java.util.Set;
32
33 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
34
35 /**
36  * Parses the cloud configuration.
37  *
38  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
39  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
40  */
41 public class CloudConfigParser {
42     private static final String DMAAP_SECURITY_TRUST_STORE_PATH = "dmaap.security.trustStorePath";
43     private static final String DMAAP_SECURITY_TRUST_STORE_PASS_PATH = "dmaap.security.trustStorePasswordPath";
44     private static final String DMAAP_SECURITY_KEY_STORE_PATH = "dmaap.security.keyStorePath";
45     private static final String DMAAP_SECURITY_KEY_STORE_PASS_PATH = "dmaap.security.keyStorePasswordPath";
46     private static final String DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH = "dmaap.security.enableDmaapCertAuth";
47     private static final String CONFIG = "config";
48
49     private final JsonObject jsonObject;
50
51     public CloudConfigParser(JsonObject jsonObject) {
52         this.jsonObject = jsonObject.getAsJsonObject(CONFIG);
53
54     }
55
56     /**
57      * Get the publisher configurations.
58      *
59      * @return a map with change identifier as key and the connected publisher configuration as value.
60      *
61      * @throws DatafileTaskException if a member of the configuration is missing.
62      */
63     public @NotNull Map<String, PublisherConfiguration> getDmaapPublisherConfigurations() throws DatafileTaskException {
64         JsonObject producerCfgs = jsonObject.get("streams_publishes").getAsJsonObject();
65         Iterator<String> changeIdentifierList = producerCfgs.keySet().iterator();
66         Map<String, PublisherConfiguration> result = new HashMap<>();
67
68         while (changeIdentifierList.hasNext()) {
69             String changeIdentifier = changeIdentifierList.next();
70             JsonObject producerCfg = getAsJson(producerCfgs, changeIdentifier);
71             JsonObject feedConfig = get(producerCfg, "dmaap_info").getAsJsonObject();
72
73             PublisherConfiguration cfg = ImmutablePublisherConfiguration.builder() //
74                 .publishUrl(getAsString(feedConfig, "publish_url")) //
75                 .passWord(getAsString(feedConfig, "password")) //
76                 .userName(getAsString(feedConfig, "username")) //
77                 .trustStorePath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PATH)) //
78                 .trustStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PASS_PATH)) //
79                 .keyStorePath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PATH)) //
80                 .keyStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PASS_PATH)) //
81                 .enableDmaapCertAuth(get(jsonObject, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
82                 .changeIdentifier(changeIdentifier) //
83                 .logUrl(getAsString(feedConfig, "log_url")) //
84                 .build();
85
86             result.put(cfg.changeIdentifier(), cfg);
87         }
88         return result;
89     }
90
91     /**
92      * Get the consumer configuration.
93      *
94      * @return the consumer configuration.
95      * @throws DatafileTaskException if a member of the configuration is missing.
96      */
97     public @NotNull ConsumerConfiguration getDmaapConsumerConfig() throws DatafileTaskException {
98         JsonObject consumerCfg = jsonObject.get("streams_subscribes").getAsJsonObject();
99         Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
100         if (topics.size() != 1) {
101             throw new DatafileTaskException("Invalid configuration, number of topic must be one, config: " + topics);
102         }
103         JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
104         JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
105         String topicUrl = getAsString(dmaapInfo, "topic_url");
106
107         return ImmutableConsumerConfiguration.builder().topicUrl(topicUrl)
108             .trustStorePath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PATH))
109             .trustStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PASS_PATH))
110             .keyStorePath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PATH))
111             .keyStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PASS_PATH))
112             .enableDmaapCertAuth(get(jsonObject, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
113             .build();
114     }
115
116     /**
117      * Get the security configuration for communication with the xNF.
118      *
119      * @return the xNF communication security configuration.
120      * @throws DatafileTaskException if a member of the configuration is missing.
121      */
122     public @NotNull FtpesConfig getFtpesConfig() throws DatafileTaskException {
123         return new ImmutableFtpesConfig.Builder() //
124             .keyCert(getAsString(jsonObject, "dmaap.ftpesConfig.keyCert"))
125             .keyPassword(getAsString(jsonObject, "dmaap.ftpesConfig.keyPassword"))
126             .trustedCa(getAsString(jsonObject, "dmaap.ftpesConfig.trustedCa"))
127             .trustedCaPassword(getAsString(jsonObject, "dmaap.ftpesConfig.trustedCaPassword")) //
128             .build();
129     }
130
131     private static @NotNull JsonElement get(JsonObject obj, String memberName) throws DatafileTaskException {
132         JsonElement elem = obj.get(memberName);
133         if (elem == null) {
134             throw new DatafileTaskException("Could not find member: " + memberName + " in: " + obj);
135         }
136         return elem;
137     }
138
139     private static @NotNull String getAsString(JsonObject obj, String memberName) throws DatafileTaskException {
140         return get(obj, memberName).getAsString();
141     }
142
143     private static @NotNull JsonObject getAsJson(JsonObject obj, String memberName) throws DatafileTaskException {
144         return get(obj, memberName).getAsJsonObject();
145     }
146
147 }