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