f89a101284ddc427fecbf36825c5407355320304
[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"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.configuration;
18
19 import com.google.gson.GsonBuilder;
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
23 import com.google.gson.JsonSyntaxException;
24 import com.google.gson.TypeAdapterFactory;
25
26 import java.io.BufferedInputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.util.ServiceLoader;
32
33 import javax.validation.constraints.NotEmpty;
34 import javax.validation.constraints.NotNull;
35
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.boot.context.properties.ConfigurationProperties;
41 import org.springframework.boot.context.properties.EnableConfigurationProperties;
42 import org.springframework.stereotype.Component;
43
44 /**
45  * Holds all configuration for the DFC.
46  *
47  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
48  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
49  */
50
51 @Component
52 @EnableConfigurationProperties
53 @ConfigurationProperties("app")
54 public class AppConfig {
55
56     private static final String CONFIG = "configs";
57     private static final String DMAAP = "dmaap";
58     private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
59     private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
60     private static final String FTP = "ftp";
61     private static final String FTPES_CONFIGURATION = "ftpesConfiguration";
62     private static final String SECURITY = "security";
63     private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
64
65     private DmaapConsumerConfiguration dmaapConsumerConfiguration;
66     private DmaapPublisherConfiguration dmaapPublisherConfiguration;
67     private FtpesConfig ftpesConfiguration;
68
69     @NotEmpty
70     private String filepath;
71
72     public synchronized DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
73         return dmaapConsumerConfiguration;
74     }
75
76     public synchronized DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
77         return dmaapPublisherConfiguration;
78     }
79
80     public synchronized FtpesConfig getFtpesConfiguration() {
81         return ftpesConfiguration;
82     }
83
84     /**
85      * Reads the configuration from file.
86      */
87     public void loadConfigurationFromFile() {
88         GsonBuilder gsonBuilder = new GsonBuilder();
89         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
90         JsonParser parser = new JsonParser();
91         JsonObject jsonObject;
92         try (InputStream inputStream = createInputStream(filepath)) {
93             JsonElement rootElement = getJsonElement(parser, inputStream);
94             if (rootElement.isJsonObject()) {
95                 jsonObject = rootElement.getAsJsonObject();
96                 FtpesConfig ftpesConfig = deserializeType(gsonBuilder,
97                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
98                         FtpesConfig.class);
99                 DmaapConsumerConfiguration consumerConfiguration = deserializeType(gsonBuilder,
100                         concatenateJsonObjects(
101                                 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
102                                         .getAsJsonObject(DMAAP_CONSUMER),
103                                 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
104                         DmaapConsumerConfiguration.class);
105
106                 DmaapPublisherConfiguration publisherConfiguration = deserializeType(gsonBuilder,
107                         concatenateJsonObjects(
108                                 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
109                                         .getAsJsonObject(DMAAP_PRODUCER),
110                                 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
111                         DmaapPublisherConfiguration.class);
112
113                 setConfiguration(consumerConfiguration, publisherConfiguration, ftpesConfig);
114             }
115         } catch (IOException e) {
116             logger.error("Problem with file loading, file: {}", filepath, e);
117         } catch (JsonSyntaxException e) {
118             logger.error("Problem with Json deserialization", e);
119         }
120     }
121
122     synchronized void setConfiguration(DmaapConsumerConfiguration consumerConfiguration,
123             DmaapPublisherConfiguration publisherConfiguration, FtpesConfig ftpesConfig) {
124         this.dmaapConsumerConfiguration = consumerConfiguration;
125         this.dmaapPublisherConfiguration = publisherConfiguration;
126         this.ftpesConfiguration = ftpesConfig;
127     }
128
129     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
130         return parser.parse(new InputStreamReader(inputStream));
131     }
132
133     private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
134             @NotNull Class<T> type) {
135         return gsonBuilder.create().fromJson(jsonObject, type);
136     }
137
138     InputStream createInputStream(@NotNull String filepath) throws IOException {
139         return new BufferedInputStream(new FileInputStream(filepath));
140     }
141
142     synchronized String getFilepath() {
143         return this.filepath;
144     }
145
146     public synchronized void setFilepath(String filepath) {
147         this.filepath = filepath;
148     }
149
150     private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
151         source.entrySet().forEach(entry -> target.add(entry.getKey(), entry.getValue()));
152         return target;
153     }
154
155 }