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