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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.collectors.datafile.configuration;
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;
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;
36 import java.nio.charset.StandardCharsets;
37 import java.util.ServiceLoader;
38 import javax.validation.constraints.NotEmpty;
39 import javax.validation.constraints.NotNull;
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;
51 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
54 @EnableConfigurationProperties
55 @ConfigurationProperties("app")
56 public abstract class DatafileAppConfig implements Config {
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";
65 private final Logger logger = LoggerFactory.getLogger(this.getClass());
67 AaiClientConfiguration aaiClientConfiguration;
69 DmaapConsumerConfiguration dmaapConsumerConfiguration;
71 DmaapPublisherConfiguration dmaapPublisherConfiguration;
74 private String filepath;
78 public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
79 return dmaapConsumerConfiguration;
83 public AaiClientConfiguration getAaiClientConfiguration() {
84 return aaiClientConfiguration;
88 public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
89 return dmaapPublisherConfiguration;
93 public void initFileStreamReader() {
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);
107 dmaapConsumerConfiguration = deserializeType(gsonBuilder,
108 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
109 DmaapConsumerConfiguration.class);
111 dmaapPublisherConfiguration = deserializeType(gsonBuilder,
112 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
113 DmaapPublisherConfiguration.class);
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);
122 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
123 return parser.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
126 private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
127 @NotNull Class<T> type) {
128 return gsonBuilder.create().fromJson(jsonObject, type);
131 InputStream getInputStream(@NotNull String filepath) throws IOException {
132 return new BufferedInputStream(new FileInputStream(filepath));
135 String getFilepath() {
136 return this.filepath;
139 public void setFilepath(String filepath) {
140 this.filepath = filepath;