169bf8edab669797f1e5837d9936c57e7fdb46d3
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.configuration;
22
23 import com.google.gson.GsonBuilder;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import com.google.gson.JsonSyntaxException;
28 import com.google.gson.TypeAdapterFactory;
29
30 import java.io.BufferedInputStream;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35
36 import java.nio.charset.StandardCharsets;
37 import java.util.ServiceLoader;
38 import javax.validation.constraints.NotEmpty;
39 import javax.validation.constraints.NotNull;
40
41 import org.onap.dcaegen2.collectors.datafile.config.AaiClientConfiguration;
42 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
43 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.boot.context.properties.ConfigurationProperties;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.context.annotation.Configuration;
49
50 /**
51  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
52  */
53 @Configuration
54 @EnableConfigurationProperties
55 @ConfigurationProperties("app")
56 public abstract class DatafileAppConfig implements Config {
57
58     private static final String CONFIG = "configs";
59     private static final String AAI = "aai";
60     private static final String DMAAP = "dmaap";
61     private static final String AAI_CONFIG = "aaiClientConfiguration";
62     private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
63     private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
64
65     private final Logger logger = LoggerFactory.getLogger(this.getClass());
66
67     AaiClientConfiguration aaiClientConfiguration;
68
69     DmaapConsumerConfiguration dmaapConsumerConfiguration;
70
71     DmaapPublisherConfiguration dmaapPublisherConfiguration;
72
73     @NotEmpty
74     private String filepath;
75
76
77     @Override
78     public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
79         return dmaapConsumerConfiguration;
80     }
81
82     @Override
83     public AaiClientConfiguration getAaiClientConfiguration() {
84         return aaiClientConfiguration;
85     }
86
87     @Override
88     public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
89         return dmaapPublisherConfiguration;
90     }
91
92     @Override
93     public void initFileStreamReader() {
94
95         GsonBuilder gsonBuilder = new GsonBuilder();
96         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
97         JsonParser parser = new JsonParser();
98         JsonObject jsonObject;
99         try (InputStream inputStream = getInputStream(filepath)) {
100             JsonElement rootElement = getJsonElement(parser, inputStream);
101             if (rootElement.isJsonObject()) {
102                 jsonObject = rootElement.getAsJsonObject();
103                 aaiClientConfiguration = deserializeType(gsonBuilder,
104                     jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(AAI).getAsJsonObject(AAI_CONFIG),
105                     AaiClientConfiguration.class);
106
107                 dmaapConsumerConfiguration = deserializeType(gsonBuilder,
108                     jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
109                     DmaapConsumerConfiguration.class);
110
111                 dmaapPublisherConfiguration = deserializeType(gsonBuilder,
112                     jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
113                     DmaapPublisherConfiguration.class);
114             }
115         } catch (IOException e) {
116             logger.warn("Problem with file loading, file: {}", filepath, e);
117         } catch (JsonSyntaxException e) {
118             logger.warn("Problem with Json deserialization", e);
119         }
120     }
121
122     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
123         return parser.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
124     }
125
126     private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
127         @NotNull Class<T> type) {
128         return gsonBuilder.create().fromJson(jsonObject, type);
129     }
130
131     InputStream getInputStream(@NotNull String filepath) throws IOException {
132         return new BufferedInputStream(new FileInputStream(filepath));
133     }
134
135     String getFilepath() {
136         return this.filepath;
137     }
138
139     public void setFilepath(String filepath) {
140         this.filepath = filepath;
141     }
142
143 }