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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.configuration;
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;
26 import javax.validation.constraints.NotEmpty;
27 import javax.validation.constraints.NotNull;
29 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
30 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.boot.context.properties.ConfigurationProperties;
34 import org.springframework.boot.context.properties.EnableConfigurationProperties;
35 import org.springframework.context.annotation.Configuration;
37 import com.google.gson.GsonBuilder;
38 import com.google.gson.JsonElement;
39 import com.google.gson.JsonObject;
40 import com.google.gson.JsonParser;
41 import com.google.gson.JsonSyntaxException;
42 import com.google.gson.TypeAdapterFactory;
45 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
46 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
49 @EnableConfigurationProperties
50 @ConfigurationProperties("app")
51 public abstract class DatafileAppConfig implements Config {
53 private static final String CONFIG = "configs";
54 private static final String DMAAP = "dmaap";
55 private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
56 private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
57 private static final String FTP = "ftp";
58 private static final String FTPES_CONFIGURATION = "ftpesConfiguration";
61 private static final Logger logger = LoggerFactory.getLogger(DatafileAppConfig.class);
63 DmaapConsumerConfiguration dmaapConsumerConfiguration;
65 DmaapPublisherConfiguration dmaapPublisherConfiguration;
67 FtpesConfig ftpesConfig;
70 private String filepath;
74 public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
75 return dmaapConsumerConfiguration;
79 public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
80 return dmaapPublisherConfiguration;
84 public FtpesConfig getFtpesConfiguration() {
89 public void initFileStreamReader() {
91 GsonBuilder gsonBuilder = new GsonBuilder();
92 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
93 JsonParser parser = new JsonParser();
94 JsonObject jsonObject;
95 try (InputStream inputStream = getInputStream(filepath)) {
96 JsonElement rootElement = getJsonElement(parser, inputStream);
97 if (rootElement.isJsonObject()) {
98 jsonObject = rootElement.getAsJsonObject();
99 ftpesConfig = deserializeType(gsonBuilder,
100 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
102 dmaapConsumerConfiguration = deserializeType(gsonBuilder,
103 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
104 DmaapConsumerConfiguration.class);
106 dmaapPublisherConfiguration = deserializeType(gsonBuilder,
107 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
108 DmaapPublisherConfiguration.class);
110 } catch (IOException e) {
111 logger.error("Problem with file loading, file: {}", filepath, e);
112 } catch (JsonSyntaxException e) {
113 logger.error("Problem with Json deserialization", e);
117 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
118 return parser.parse(new InputStreamReader(inputStream));
121 private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
122 @NotNull Class<T> type) {
123 return gsonBuilder.create().fromJson(jsonObject, type);
126 InputStream getInputStream(@NotNull String filepath) throws IOException {
127 return new BufferedInputStream(new FileInputStream(filepath));
130 String getFilepath() {
131 return this.filepath;
134 public void setFilepath(String filepath) {
135 this.filepath = filepath;