a86a32b8395ae4f445230f007ad49d311dfc4d03
[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 import java.util.Properties;
29 import java.util.Set;
30
31 import javax.validation.constraints.NotNull;
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
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";
49
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";
53
54     private final Properties systemEnvironment;
55
56     private final JsonObject jsonObject;
57
58     public CloudConfigParser(JsonObject jsonObject, Properties systemEnvironment) {
59         this.jsonObject = jsonObject.getAsJsonObject(CONFIG);
60         this.systemEnvironment = systemEnvironment;
61     }
62
63     /**
64      * Get the publisher configurations.
65      *
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.
68      */
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<>();
73
74         while (changeIdentifierList.hasNext()) {
75             String changeIdentifier = changeIdentifierList.next();
76             JsonObject producerCfg = getAsJson(producerCfgs, changeIdentifier);
77             JsonObject feedConfig = get(producerCfg, "dmaap_info").getAsJsonObject();
78
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")) //
90                 .build();
91
92             result.put(cfg.changeIdentifier(), cfg);
93         }
94         return result;
95     }
96
97     /**
98      * Get the consumer configuration.
99      *
100      * @return the consumer configuration.
101      * @throws DatafileTaskException if a member of the configuration is missing.
102      */
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);
108         }
109         JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
110         JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
111         String topicUrl = getAsString(dmaapInfo, "topic_url");
112
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()) //
119             .build();
120     }
121
122     /**
123      * Get the sFTP configuration.
124      *
125      * @return the sFTP configuration.
126      * @throws DatafileTaskException if a member of the configuration is missing.
127      */
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();
133     }
134
135     /**
136      * Get the security configuration for communication with the xNF.
137      *
138      * @return the xNF communication security configuration.
139      * @throws DatafileTaskException if a member of the configuration is missing.
140      */
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")) //
147             .build();
148     }
149
150     private String determineKnownHostsFilePath() {
151         String filePath = "";
152         if (systemEnvironment != null) {
153             filePath =
154                 systemEnvironment.getProperty(KNOWN_HOSTS_FILE_PATH_ENV_PROPERTY, "/home/datafile/.ssh/known_hosts");
155         }
156         return filePath;
157     }
158
159     private static @NotNull JsonElement get(JsonObject obj, String memberName) throws DatafileTaskException {
160         JsonElement elem = obj.get(memberName);
161         if (elem == null) {
162             throw new DatafileTaskException("Could not find member: " + memberName + " in: " + obj);
163         }
164         return elem;
165     }
166
167     private static @NotNull String getAsString(JsonObject obj, String memberName) throws DatafileTaskException {
168         return get(obj, memberName).getAsString();
169     }
170
171     private static @NotNull Boolean getAsBoolean(JsonObject obj, String memberName) throws DatafileTaskException {
172         return get(obj, memberName).getAsBoolean();
173     }
174
175     private static @NotNull JsonObject getAsJson(JsonObject obj, String memberName) throws DatafileTaskException {
176         return get(obj, memberName).getAsJsonObject();
177     }
178
179 }