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;
30 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
31 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.boot.context.properties.ConfigurationProperties;
35 import org.springframework.boot.context.properties.EnableConfigurationProperties;
36 import org.springframework.context.annotation.Configuration;
38 import com.google.gson.GsonBuilder;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonParser;
42 import com.google.gson.JsonSyntaxException;
43 import com.google.gson.TypeAdapterFactory;
46 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
47 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
50 @EnableConfigurationProperties
51 @ConfigurationProperties("app")
52 public abstract class DatafileAppConfig implements Config {
54 private static final String CONFIG = "configs";
55 private static final String DMAAP = "dmaap";
56 private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration";
57 private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration";
58 private static final String FTP = "ftp";
59 private static final String FTPES_CONFIGURATION = "ftpesConfiguration";
60 private static final String SECURITY = "security";
62 private static final Logger logger = LoggerFactory.getLogger(DatafileAppConfig.class);
64 DmaapConsumerConfiguration dmaapConsumerConfiguration;
66 DmaapPublisherConfiguration dmaapPublisherConfiguration;
68 FtpesConfig ftpesConfig;
71 private String filepath;
75 public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
76 return dmaapConsumerConfiguration;
80 public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
81 return dmaapPublisherConfiguration;
85 public FtpesConfig getFtpesConfiguration() {
90 public void initFileStreamReader() {
92 GsonBuilder gsonBuilder = new GsonBuilder();
93 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
94 JsonParser parser = new JsonParser();
95 JsonObject jsonObject;
96 try (InputStream inputStream = getInputStream(filepath)) {
97 JsonElement rootElement = getJsonElement(parser, inputStream);
98 if (rootElement.isJsonObject()) {
99 jsonObject = rootElement.getAsJsonObject();
100 ftpesConfig = deserializeType(gsonBuilder,
101 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(FTP).getAsJsonObject(FTPES_CONFIGURATION),
103 dmaapConsumerConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
104 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
105 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
106 DmaapConsumerConfiguration.class);
108 dmaapPublisherConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
109 jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
110 rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
111 DmaapPublisherConfiguration.class);
113 } catch (IOException e) {
114 logger.error("Problem with file loading, file: {}", filepath, e);
115 } catch (JsonSyntaxException e) {
116 logger.error("Problem with Json deserialization", e);
120 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
121 return parser.parse(new InputStreamReader(inputStream));
124 private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
125 @NotNull Class<T> type) {
126 return gsonBuilder.create().fromJson(jsonObject, type);
129 InputStream getInputStream(@NotNull String filepath) throws IOException {
130 return new BufferedInputStream(new FileInputStream(filepath));
133 String getFilepath() {
134 return this.filepath;
137 public void setFilepath(String filepath) {
138 this.filepath = filepath;
141 private JsonObject concatenateJsonObjects(JsonObject target, JsonObject source) {
143 .forEach(entry -> target.add(entry.getKey(), entry.getValue()));