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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
19 package org.onap.dcaegen2.collectors.datafile.configuration;
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;
28 import javax.validation.constraints.NotEmpty;
29 import javax.validation.constraints.NotNull;
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;
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;
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>
51 @EnableConfigurationProperties
52 @ConfigurationProperties("app")
53 public abstract class DatafileAppConfig implements Config {
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";
60 private static final Logger logger = LoggerFactory.getLogger(DatafileAppConfig.class);
62 DmaapConsumerConfiguration dmaapConsumerConfiguration;
64 DmaapPublisherConfiguration dmaapPublisherConfiguration;
67 private String filepath;
71 public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
72 return dmaapConsumerConfiguration;
76 public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
77 return dmaapPublisherConfiguration;
81 public void initFileStreamReader() {
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);
95 dmaapPublisherConfiguration = deserializeType(gsonBuilder,
96 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
97 DmaapPublisherConfiguration.class);
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);
106 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
107 return parser.parse(new InputStreamReader(inputStream));
110 private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
111 @NotNull Class<T> type) {
112 return gsonBuilder.create().fromJson(jsonObject, type);
115 InputStream getInputStream(@NotNull String filepath) throws IOException {
116 return new BufferedInputStream(new FileInputStream(filepath));
119 String getFilepath() {
120 return this.filepath;
123 public void setFilepath(String filepath) {
124 this.filepath = filepath;