82c390f7712b127088b20d7379a9e051f8fbebfb
[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  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
46  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
47  */
48
49 @Component
50 @EnableConfigurationProperties
51 @ConfigurationProperties("app")
52 public class AppConfig {
53
54     private static final String CONFIG = "configs";
55     private static final String DMAAP = "dmaap";
56     private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
57     private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
58     private static final String FTP = "ftp";
59     private static final String FTPES_CONFIGURATION = "ftpesConfiguration";
60     private static final String SECURITY = "security";
61     private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
62
63     private DmaapConsumerConfiguration dmaapConsumerConfiguration;
64     private DmaapPublisherConfiguration dmaapPublisherConfiguration;
65     private FtpesConfig ftpesConfiguration;
66
67     @NotEmpty
68     private String filepath;
69
70     public synchronized DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
71         return dmaapConsumerConfiguration;
72     }
73
74     public synchronized DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
75         return dmaapPublisherConfiguration;
76     }
77
78     public synchronized FtpesConfig getFtpesConfiguration() {
79         return ftpesConfiguration;
80     }
81
82     public void loadConfigurationFromFile() {
83
84         GsonBuilder gsonBuilder = new GsonBuilder();
85         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
86         JsonParser parser = new JsonParser();
87         JsonObject jsonObject;
88         try (InputStream inputStream = createInputStream(filepath)) {
89             JsonElement rootElement = getJsonElement(parser, inputStream);
90             if (rootElement.isJsonObject()) {
91                 jsonObject = rootElement.getAsJsonObject();
92                 FtpesConfig ftpesConfig = deserializeType(gsonBuilder,
93                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
94                         FtpesConfig.class);
95                 DmaapConsumerConfiguration consumerConfiguration = deserializeType(gsonBuilder,
96                         concatenateJsonObjects(
97                                 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
98                                         .getAsJsonObject(DMAAP_CONSUMER),
99                                 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
100                         DmaapConsumerConfiguration.class);
101
102                 DmaapPublisherConfiguration publisherConfiguration = deserializeType(gsonBuilder,
103                         concatenateJsonObjects(
104                                 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
105                                         .getAsJsonObject(DMAAP_PRODUCER),
106                                 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
107                         DmaapPublisherConfiguration.class);
108
109                 setConfiguration(consumerConfiguration, publisherConfiguration, ftpesConfig);
110             }
111         } catch (IOException e) {
112             logger.error("Problem with file loading, file: {}", filepath, e);
113         } catch (JsonSyntaxException e) {
114             logger.error("Problem with Json deserialization", e);
115         }
116     }
117
118     synchronized void setConfiguration(DmaapConsumerConfiguration consumerConfiguration,
119             DmaapPublisherConfiguration publisherConfiguration, FtpesConfig ftpesConfig) {
120         this.dmaapConsumerConfiguration = consumerConfiguration;
121         this.dmaapPublisherConfiguration = publisherConfiguration;
122         this.ftpesConfiguration = ftpesConfig;
123     }
124
125     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
126         return parser.parse(new InputStreamReader(inputStream));
127     }
128
129     private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
130             @NotNull Class<T> type) {
131         return gsonBuilder.create().fromJson(jsonObject, type);
132     }
133
134     InputStream createInputStream(@NotNull String filepath) throws IOException {
135         return new BufferedInputStream(new FileInputStream(filepath));
136     }
137
138     synchronized String getFilepath() {
139         return this.filepath;
140     }
141
142     public synchronized void setFilepath(String filepath) {
143         this.filepath = filepath;
144     }
145
146     private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
147         source.entrySet().forEach(entry -> target.add(entry.getKey(), entry.getValue()));
148         return target;
149     }
150
151 }