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");
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 org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
22 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
24 import java.util.ServiceLoader;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.boot.context.properties.ConfigurationProperties;
28 import org.springframework.boot.context.properties.EnableConfigurationProperties;
29 import org.springframework.stereotype.Component;
30 import javax.validation.constraints.NotEmpty;
31 import javax.validation.constraints.NotNull;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
35 import com.google.gson.JsonParser;
36 import com.google.gson.JsonSyntaxException;
37 import com.google.gson.TypeAdapterFactory;
40 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
41 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
45 @EnableConfigurationProperties
46 @ConfigurationProperties("app")
47 public class AppConfig {
49 private static final String CONFIG = "configs";
50 private static final String DMAAP = "dmaap";
51 private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
52 private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
53 private static final String FTP = "ftp";
54 private static final String FTPES_CONFIGURATION = "ftpesConfiguration";
55 private static final String SECURITY = "security";
56 private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
58 DmaapConsumerConfiguration dmaapConsumerConfiguration;
60 DmaapPublisherConfiguration dmaapPublisherConfiguration;
62 FtpesConfig ftpesConfig;
65 private String filepath;
67 public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
68 return dmaapConsumerConfiguration;
71 public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
72 return dmaapPublisherConfiguration;
75 public FtpesConfig getFtpesConfiguration() {
79 public void initFileStreamReader() {
81 GsonBuilder gsonBuilder = new GsonBuilder();
82 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
83 JsonParser parser = new JsonParser();
84 JsonObject jsonObject;
85 try (InputStream inputStream = getInputStream(filepath)) {
86 JsonElement rootElement = getJsonElement(parser, inputStream);
87 if (rootElement.isJsonObject()) {
88 jsonObject = rootElement.getAsJsonObject();
89 ftpesConfig = deserializeType(gsonBuilder,
90 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
92 dmaapConsumerConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
93 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
94 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
95 DmaapConsumerConfiguration.class);
97 dmaapPublisherConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
98 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
99 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
100 DmaapPublisherConfiguration.class);
102 } catch (IOException e) {
103 logger.error("Problem with file loading, file: {}", filepath, e);
104 } catch (JsonSyntaxException e) {
105 logger.error("Problem with Json deserialization", e);
109 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
110 return parser.parse(new InputStreamReader(inputStream));
113 private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
114 @NotNull Class<T> type) {
115 return gsonBuilder.create().fromJson(jsonObject, type);
118 InputStream getInputStream(@NotNull String filepath) throws IOException {
119 return new BufferedInputStream(new FileInputStream(filepath));
122 String getFilepath() {
123 return this.filepath;
126 public void setFilepath(String filepath) {
127 this.filepath = filepath;
130 private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
132 .forEach(entry -> target.add(entry.getKey(), entry.getValue()));