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