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;
28 import java.util.Properties;
31 import javax.validation.constraints.NotNull;
33 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
36 * Parses the cloud configuration.
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>
41 public class CloudConfigParser {
43 private static final String DMAAP_SECURITY_TRUST_STORE_PATH = "dmaap.security.trustStorePath";
44 private static final String DMAAP_SECURITY_TRUST_STORE_PASS_PATH = "dmaap.security.trustStorePasswordPath";
45 private static final String DMAAP_SECURITY_KEY_STORE_PATH = "dmaap.security.keyStorePath";
46 private static final String DMAAP_SECURITY_KEY_STORE_PASS_PATH = "dmaap.security.keyStorePasswordPath";
47 private static final String DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH = "dmaap.security.enableDmaapCertAuth";
48 private static final String CONFIG = "config";
50 private static final String KNOWN_HOSTS_FILE_PATH_ENV_PROPERTY = "KNOWN_HOSTS_FILE_PATH";
51 private static final String CBS_PROPERTY_SFTP_SECURITY_STRICT_HOST_KEY_CHECKING =
52 "sftp.security.strictHostKeyChecking";
54 private final Properties systemEnvironment;
56 private final JsonObject jsonObject;
58 public CloudConfigParser(JsonObject jsonObject, Properties systemEnvironment) {
59 this.jsonObject = jsonObject.getAsJsonObject(CONFIG);
60 this.systemEnvironment = systemEnvironment;
64 * Get the publisher configurations.
66 * @return a map with change identifier as key and the connected publisher configuration as value.
67 * @throws DatafileTaskException if a member of the configuration is missing.
69 public @NotNull Map<String, PublisherConfiguration> getDmaapPublisherConfigurations() throws DatafileTaskException {
70 JsonObject producerCfgs = jsonObject.get("streams_publishes").getAsJsonObject();
71 Iterator<String> changeIdentifierList = producerCfgs.keySet().iterator();
72 Map<String, PublisherConfiguration> result = new HashMap<>();
74 while (changeIdentifierList.hasNext()) {
75 String changeIdentifier = changeIdentifierList.next();
76 JsonObject producerCfg = getAsJson(producerCfgs, changeIdentifier);
77 JsonObject feedConfig = get(producerCfg, "dmaap_info").getAsJsonObject();
79 PublisherConfiguration cfg = ImmutablePublisherConfiguration.builder() //
80 .publishUrl(getAsString(feedConfig, "publish_url")) //
81 .passWord(getAsString(feedConfig, "password")) //
82 .userName(getAsString(feedConfig, "username")) //
83 .trustStorePath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PATH)) //
84 .trustStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PASS_PATH)) //
85 .keyStorePath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PATH)) //
86 .keyStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PASS_PATH)) //
87 .enableDmaapCertAuth(get(jsonObject, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
88 .changeIdentifier(changeIdentifier) //
89 .logUrl(getAsString(feedConfig, "log_url")) //
92 result.put(cfg.changeIdentifier(), cfg);
98 * Get the consumer configuration.
100 * @return the consumer configuration.
101 * @throws DatafileTaskException if a member of the configuration is missing.
103 public @NotNull ConsumerConfiguration getDmaapConsumerConfig() throws DatafileTaskException {
104 JsonObject consumerCfg = jsonObject.get("streams_subscribes").getAsJsonObject();
105 Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
106 if (topics.size() != 1) {
107 throw new DatafileTaskException("Invalid configuration, number of topic must be one, config: " + topics);
109 JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
110 JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
111 String topicUrl = getAsString(dmaapInfo, "topic_url");
113 return ImmutableConsumerConfiguration.builder().topicUrl(topicUrl)
114 .trustStorePath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PATH))
115 .trustStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_TRUST_STORE_PASS_PATH))
116 .keyStorePath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PATH))
117 .keyStorePasswordPath(getAsString(jsonObject, DMAAP_SECURITY_KEY_STORE_PASS_PATH))
118 .enableDmaapCertAuth(get(jsonObject, DMAAP_SECURITY_ENABLE_DMAAP_CERT_AUTH).getAsBoolean()) //
123 * Get the sFTP configuration.
125 * @return the sFTP configuration.
126 * @throws DatafileTaskException if a member of the configuration is missing.
128 public @NotNull SftpConfig getSftpConfig() throws DatafileTaskException {
129 String filePath = determineKnownHostsFilePath();
130 return new ImmutableSftpConfig.Builder() //
131 .strictHostKeyChecking(getAsBoolean(jsonObject, CBS_PROPERTY_SFTP_SECURITY_STRICT_HOST_KEY_CHECKING))
132 .knownHostsFilePath(filePath).build();
136 * Get the security configuration for communication with the xNF.
138 * @return the xNF communication security configuration.
139 * @throws DatafileTaskException if a member of the configuration is missing.
141 public @NotNull FtpesConfig getFtpesConfig() throws DatafileTaskException {
142 return new ImmutableFtpesConfig.Builder() //
143 .keyCert(getAsString(jsonObject, "dmaap.ftpesConfig.keyCert"))
144 .keyPasswordPath(getAsString(jsonObject, "dmaap.ftpesConfig.keyPasswordPath"))
145 .trustedCa(getAsString(jsonObject, "dmaap.ftpesConfig.trustedCa"))
146 .trustedCaPasswordPath(getAsString(jsonObject, "dmaap.ftpesConfig.trustedCaPasswordPath")) //
150 private String determineKnownHostsFilePath() {
151 String filePath = "";
152 if (systemEnvironment != null) {
154 systemEnvironment.getProperty(KNOWN_HOSTS_FILE_PATH_ENV_PROPERTY, "/home/datafile/.ssh/known_hosts");
159 private static @NotNull JsonElement get(JsonObject obj, String memberName) throws DatafileTaskException {
160 JsonElement elem = obj.get(memberName);
162 throw new DatafileTaskException("Could not find member: " + memberName + " in: " + obj);
167 private static @NotNull String getAsString(JsonObject obj, String memberName) throws DatafileTaskException {
168 return get(obj, memberName).getAsString();
171 private static @NotNull Boolean getAsBoolean(JsonObject obj, String memberName) throws DatafileTaskException {
172 return get(obj, memberName).getAsBoolean();
175 private static @NotNull JsonObject getAsJson(JsonObject obj, String memberName) throws DatafileTaskException {
176 return get(obj, memberName).getAsJsonObject();