2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 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 com.google.gson.GsonBuilder;
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
23 import com.google.gson.JsonSyntaxException;
24 import com.google.gson.TypeAdapterFactory;
26 import java.io.BufferedInputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.util.ServiceLoader;
33 import javax.validation.constraints.NotEmpty;
34 import javax.validation.constraints.NotNull;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.boot.context.properties.ConfigurationProperties;
41 import org.springframework.boot.context.properties.EnableConfigurationProperties;
42 import org.springframework.stereotype.Component;
45 * Holds all configuration for the DFC.
47 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
48 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
52 @EnableConfigurationProperties
53 @ConfigurationProperties("app")
54 public class AppConfig {
56 private static final String CONFIG = "configs";
57 private static final String DMAAP = "dmaap";
58 private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
59 private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
60 private static final String FTP = "ftp";
61 private static final String FTPES_CONFIGURATION = "ftpesConfiguration";
62 private static final String SECURITY = "security";
63 private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
65 private DmaapConsumerConfiguration dmaapConsumerConfiguration;
66 private DmaapPublisherConfiguration dmaapPublisherConfiguration;
67 private FtpesConfig ftpesConfiguration;
70 private String filepath;
72 public synchronized DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
73 return dmaapConsumerConfiguration;
76 public synchronized DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
77 return dmaapPublisherConfiguration;
80 public synchronized FtpesConfig getFtpesConfiguration() {
81 return ftpesConfiguration;
85 * Reads the configuration from file.
87 public void loadConfigurationFromFile() {
88 GsonBuilder gsonBuilder = new GsonBuilder();
89 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
90 JsonParser parser = new JsonParser();
91 JsonObject jsonObject;
92 try (InputStream inputStream = createInputStream(filepath)) {
93 JsonElement rootElement = getJsonElement(parser, inputStream);
94 if (rootElement.isJsonObject()) {
95 jsonObject = rootElement.getAsJsonObject();
96 FtpesConfig ftpesConfig = deserializeType(gsonBuilder,
97 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
99 DmaapConsumerConfiguration consumerConfiguration = deserializeType(gsonBuilder,
100 concatenateJsonObjects(
101 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
102 .getAsJsonObject(DMAAP_CONSUMER),
103 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
104 DmaapConsumerConfiguration.class);
106 DmaapPublisherConfiguration publisherConfiguration = deserializeType(gsonBuilder,
107 concatenateJsonObjects(
108 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
109 .getAsJsonObject(DMAAP_PRODUCER),
110 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
111 DmaapPublisherConfiguration.class);
113 setConfiguration(consumerConfiguration, publisherConfiguration, ftpesConfig);
115 } catch (JsonSyntaxException | IOException e) {
116 logger.error("Problem with loading configuration, file: {}", filepath, e);
120 synchronized void setConfiguration(DmaapConsumerConfiguration consumerConfiguration,
121 DmaapPublisherConfiguration publisherConfiguration, FtpesConfig ftpesConfig) {
122 this.dmaapConsumerConfiguration = consumerConfiguration;
123 this.dmaapPublisherConfiguration = publisherConfiguration;
124 this.ftpesConfiguration = ftpesConfig;
127 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
128 return parser.parse(new InputStreamReader(inputStream));
131 private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
132 @NotNull Class<T> type) {
133 return gsonBuilder.create().fromJson(jsonObject, type);
136 InputStream createInputStream(@NotNull String filepath) throws IOException {
137 return new BufferedInputStream(new FileInputStream(filepath));
140 synchronized String getFilepath() {
141 return this.filepath;
144 public synchronized void setFilepath(String filepath) {
145 this.filepath = filepath;
148 private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
149 source.entrySet().forEach(entry -> target.add(entry.getKey(), entry.getValue()));