59bb259d1c3969c8ec77eeb436df963283432c62
[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.context.annotation.Configuration;
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     private static final String SECURITY = "security";
60     private static final Logger logger = LoggerFactory.getLogger(DatafileAppConfig.class);
61
62     DmaapConsumerConfiguration dmaapConsumerConfiguration;
63
64     DmaapPublisherConfiguration dmaapPublisherConfiguration;
65
66     FtpesConfig ftpesConfig;
67
68     @NotEmpty
69     private String filepath;
70
71
72     @Override
73     public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
74         return dmaapConsumerConfiguration;
75     }
76
77     @Override
78     public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
79         return dmaapPublisherConfiguration;
80     }
81
82     @Override
83     public FtpesConfig getFtpesConfiguration() {
84         return ftpesConfig;
85     }
86
87     @Override
88     public void initFileStreamReader() {
89
90         GsonBuilder gsonBuilder = new GsonBuilder();
91         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
92         JsonParser parser = new JsonParser();
93         JsonObject jsonObject;
94         try (InputStream inputStream = getInputStream(filepath)) {
95             JsonElement rootElement = getJsonElement(parser, inputStream);
96             if (rootElement.isJsonObject()) {
97                 jsonObject = rootElement.getAsJsonObject();
98                 ftpesConfig = deserializeType(gsonBuilder,
99                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
100                         FtpesConfig.class);
101                 dmaapConsumerConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
102                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
103                         rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
104                         DmaapConsumerConfiguration.class);
105
106                 dmaapPublisherConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
107                         jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
108                         rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
109                         DmaapPublisherConfiguration.class);
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     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
119         return parser.parse(new InputStreamReader(inputStream));
120     }
121
122     private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
123             @NotNull Class<T> type) {
124         return gsonBuilder.create().fromJson(jsonObject, type);
125     }
126
127     InputStream getInputStream(@NotNull String filepath) throws IOException {
128         return new BufferedInputStream(new FileInputStream(filepath));
129     }
130
131     String getFilepath() {
132         return this.filepath;
133     }
134
135     public void setFilepath(String filepath) {
136         this.filepath = filepath;
137     }
138
139     private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
140         source.entrySet()
141                 .forEach(entry -> target.add(entry.getKey(), entry.getValue()));
142         return target;
143     }
144 }