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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.onap.dcaegen2.collectors.datafile.configuration;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
24 import java.util.HashMap;
25 import java.util.Iterator;
27 import java.util.Map.Entry;
30 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
33 * Parses the cloud configuration.
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>
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 private static final String CONFIG = "config";
46 private final JsonObject jsonObject;
48 public CloudConfigParser(JsonObject jsonObject) {
49 this.jsonObject = jsonObject.getAsJsonObject(CONFIG);
54 * Get the publisher configurations.
56 * @return a map with change identifier as key and the connected publisher configuration as value.
58 * @throws DatafileTaskException if a member of the configuration is missing.
60 public Map<String, PublisherConfiguration> getDmaapPublisherConfigurations() throws DatafileTaskException {
61 JsonObject producerCfgs = jsonObject.get("streams_publishes").getAsJsonObject();
62 Iterator<String> changeIdentifierList = producerCfgs.keySet().iterator();
64 Map<String, PublisherConfiguration> result = new HashMap<>();
66 while (changeIdentifierList.hasNext()) {
68 String changeIdentifier = changeIdentifierList.next();
69 JsonObject producerCfg = getAsJson(producerCfgs, changeIdentifier);
70 JsonObject feedConfig = get(producerCfg, "dmaap_info").getAsJsonObject();
72 PublisherConfiguration cfg = ImmutablePublisherConfiguration.builder() //
73 .publishUrl(getAsString(feedConfig, "publish_url")) //
74 .passWord(getAsString(feedConfig, "password")) //
75 .userName(getAsString(feedConfig, "username")) //
76 .trustStorePath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PATH)) //
77 .trustStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PASS_PATH)) //
78 .keyStorePath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PATH)) //
79 .keyStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PASS_PATH)) //
80 .enableDmaapCertAuth(get(jsonObject, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
81 .changeIdentifier(changeIdentifier) //
82 .logUrl(getAsString(feedConfig, "log_url")) //
85 result.put(cfg.changeIdentifier(), cfg);
92 * Get the consumer configuration.
94 * @return the consumer configuration.
95 * @throws DatafileTaskException if a member of the configuration is missing.
97 public 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);
103 JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
104 JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
105 String topicUrl = getAsString(dmaapInfo, "topic_url");
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()) //
117 * Get the security configuration for communication with the xNF.
119 * @return the xNF communication security configuration.
120 * @throws DatafileTaskException if a member of the configuration is missing.
122 public 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")) //
131 private static JsonElement get(JsonObject obj, String memberName) throws DatafileTaskException {
132 JsonElement elem = obj.get(memberName);
134 throw new DatafileTaskException("Could not find member: " + memberName + " in: " + obj);
139 private static String getAsString(JsonObject obj, String memberName) throws DatafileTaskException {
140 return get(obj, memberName).getAsString();
143 private static JsonObject getAsJson(JsonObject obj, String memberName) throws DatafileTaskException {
144 return get(obj, memberName).getAsJsonObject();