40de33dd51965e2aa817710ba2a7a8af86604db9
[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 org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
22 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
23 import java.io.*;
24 import java.util.ServiceLoader;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.boot.context.properties.ConfigurationProperties;
28 import org.springframework.boot.context.properties.EnableConfigurationProperties;
29 import org.springframework.stereotype.Component;
30 import javax.validation.constraints.NotEmpty;
31 import javax.validation.constraints.NotNull;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
35 import com.google.gson.JsonParser;
36 import com.google.gson.JsonSyntaxException;
37 import com.google.gson.TypeAdapterFactory;
38
39 /**
40  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
41  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
42  */
43
44 @Component
45 @EnableConfigurationProperties
46 @ConfigurationProperties("app")
47 public class AppConfig {
48
49     private static final String CONFIG = "configs";
50     private static final String DMAAP = "dmaap";
51     private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
52     private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
53     private static final String FTP = "ftp";
54     private static final String FTPES_CONFIGURATION = "ftpesConfiguration";
55     private static final String SECURITY = "security";
56     private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
57
58     DmaapConsumerConfiguration dmaapConsumerConfiguration;
59
60     DmaapPublisherConfiguration dmaapPublisherConfiguration;
61
62     FtpesConfig ftpesConfig;
63
64     @NotEmpty
65     private String filepath;
66
67     public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
68         return dmaapConsumerConfiguration;
69     }
70
71     public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
72         return dmaapPublisherConfiguration;
73     }
74
75     public FtpesConfig getFtpesConfiguration() {
76         return ftpesConfig;
77     }
78
79     public void initFileStreamReader() {
80
81         GsonBuilder gsonBuilder = new GsonBuilder();
82         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
83         JsonParser parser = new JsonParser();
84         JsonObject jsonObject;
85         try (InputStream inputStream = getInputStream(filepath)) {
86             JsonElement rootElement = getJsonElement(parser, inputStream);
87             if (rootElement.isJsonObject()) {
88                 jsonObject = rootElement.getAsJsonObject();
89                 ftpesConfig = deserializeType(gsonBuilder,
90                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
91                         FtpesConfig.class);
92                 dmaapConsumerConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
93                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
94                         rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
95                         DmaapConsumerConfiguration.class);
96
97                 dmaapPublisherConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
98                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
99                         rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
100                         DmaapPublisherConfiguration.class);
101             }
102         } catch (IOException e) {
103             logger.error("Problem with file loading, file: {}", filepath, e);
104         } catch (JsonSyntaxException e) {
105             logger.error("Problem with Json deserialization", e);
106         }
107     }
108
109     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
110         return parser.parse(new InputStreamReader(inputStream));
111     }
112
113     private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
114                                   @NotNull Class<T> type) {
115         return gsonBuilder.create().fromJson(jsonObject, type);
116     }
117
118     InputStream getInputStream(@NotNull String filepath) throws IOException {
119         return new BufferedInputStream(new FileInputStream(filepath));
120     }
121
122     String getFilepath() {
123         return this.filepath;
124     }
125
126     public void setFilepath(String filepath) {
127         this.filepath = filepath;
128     }
129
130     private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
131         source.entrySet()
132                 .forEach(entry -> target.add(entry.getKey(), entry.getValue()));
133         return target;
134     }
135
136 }