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;
25 import java.io.BufferedInputStream;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.util.ServiceLoader;
31 import javax.validation.constraints.NotEmpty;
32 import javax.validation.constraints.NotNull;
33 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.boot.context.properties.ConfigurationProperties;
38 import org.springframework.boot.context.properties.EnableConfigurationProperties;
39 import org.springframework.stereotype.Component;
42 * Holds all configuration for the DFC.
44 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
45 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
49 @EnableConfigurationProperties
50 @ConfigurationProperties("app")
51 public class AppConfig {
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";
59 private static final String SECURITY = "security";
60 private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
62 private DmaapConsumerConfiguration dmaapConsumerConfiguration;
63 private DmaapPublisherConfiguration dmaapPublisherConfiguration;
64 private FtpesConfig ftpesConfiguration;
67 private String filepath;
69 public synchronized DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
70 return dmaapConsumerConfiguration;
73 public synchronized DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
74 return dmaapPublisherConfiguration;
77 public synchronized FtpesConfig getFtpesConfiguration() {
78 return ftpesConfiguration;
82 * Reads the configuration from file.
84 public void loadConfigurationFromFile() {
85 GsonBuilder gsonBuilder = new GsonBuilder();
86 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
87 JsonParser parser = new JsonParser();
88 JsonObject jsonObject;
89 try (InputStream inputStream = createInputStream(filepath)) {
90 JsonElement rootElement = getJsonElement(parser, inputStream);
91 if (rootElement.isJsonObject()) {
92 jsonObject = rootElement.getAsJsonObject();
93 FtpesConfig ftpesConfig = deserializeType(gsonBuilder,
94 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
96 DmaapConsumerConfiguration consumerConfiguration = deserializeType(gsonBuilder,
97 concatenateJsonObjects(
98 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
99 .getAsJsonObject(DMAAP_CONSUMER),
100 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
101 DmaapConsumerConfiguration.class);
103 DmaapPublisherConfiguration publisherConfiguration = deserializeType(gsonBuilder,
104 concatenateJsonObjects(
105 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
106 .getAsJsonObject(DMAAP_PRODUCER),
107 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
108 DmaapPublisherConfiguration.class);
110 setConfiguration(consumerConfiguration, publisherConfiguration, ftpesConfig);
112 } catch (JsonSyntaxException | IOException e) {
113 logger.error("Problem with loading configuration, file: {}", filepath, e);
117 synchronized void setConfiguration(DmaapConsumerConfiguration consumerConfiguration,
118 DmaapPublisherConfiguration publisherConfiguration, FtpesConfig ftpesConfig) {
119 this.dmaapConsumerConfiguration = consumerConfiguration;
120 this.dmaapPublisherConfiguration = publisherConfiguration;
121 this.ftpesConfiguration = ftpesConfig;
124 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
125 return parser.parse(new InputStreamReader(inputStream));
128 private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
129 @NotNull Class<T> type) {
130 return gsonBuilder.create().fromJson(jsonObject, type);
133 InputStream createInputStream(@NotNull String filepath) throws IOException {
134 return new BufferedInputStream(new FileInputStream(filepath));
137 synchronized String getFilepath() {
138 return this.filepath;
141 public synchronized void setFilepath(String filepath) {
142 this.filepath = filepath;
145 private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
146 source.entrySet().forEach(entry -> target.add(entry.getKey(), entry.getValue()));