summaryrefslogtreecommitdiffstats
path: root/rest-services/cbs-client/src/main
diff options
context:
space:
mode:
authorTomasz Pietruszkiewicz <tomasz.pietruszkiewicz@nokia.com>2021-04-26 10:02:00 +0200
committerTomasz Pietruszkiewicz <tomasz.pietruszkiewicz@nokia.com>2021-04-29 10:38:11 +0200
commita424173f03b236958d4a454c95a1a7597073cea6 (patch)
tree231e4a757f8ae1315c73d99de00ec1adb67810f0 /rest-services/cbs-client/src/main
parent6ff688ba54d1ec40e6f6a6e5dd02ef6b6e29dd54 (diff)
CBS-Client to support config fetch from a configMap
Change-Id: I2ad57844d142ece3275ed6d8586d8689b296bbbe Issue-ID: DCAEGEN2-2716 Signed-off-by: Tomasz Pietruszkiewicz <tomasz.pietruszkiewicz@nokia.com> Signed-off-by: Michal Banka <michal.banka@nokia.com>
Diffstat (limited to 'rest-services/cbs-client/src/main')
-rw-r--r--rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java16
-rw-r--r--rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java78
-rw-r--r--rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRest.java (renamed from rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java)28
-rw-r--r--rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java6
4 files changed, 107 insertions, 21 deletions
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java
index 7e625182..f1e49bb7 100644
--- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java
+++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* DCAEGEN2-SERVICES-SDK
* ================================================================================
- * Copyright (C) 2019-2020 Nokia. All rights reserved.
+ * Copyright (C) 2019-2021 Nokia. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,7 +22,8 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
import org.jetbrains.annotations.NotNull;
import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory;
-import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientImpl;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientConfigMap;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientRest;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsLookup;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys;
@@ -36,7 +37,6 @@ import reactor.core.publisher.Mono;
* @since 1.1.2
*/
public class CbsClientFactory {
-
/**
* <p>Creates Mono which will emit instance of {@link CbsClient} when service discovery is complete.</p>
*
@@ -67,7 +67,13 @@ public class CbsClientFactory {
private static Mono<CbsClient> createCbsClientMono(RxHttpClient httpClient,
CbsClientConfiguration configuration) {
+ CbsClientConfigMap cbsClientConfigMap = new CbsClientConfigMap(configuration.configMapFilePath());
+ return cbsClientConfigMap.verifyConfigMapFile() ? Mono.just(cbsClientConfigMap) :
+ getConfigFromCBS(httpClient, configuration);
+ }
+
+ private static Mono<CbsClient> getConfigFromCBS(RxHttpClient httpClient, CbsClientConfiguration configuration) {
return new CbsLookup().lookup(configuration)
- .map(addr -> new CbsClientImpl(httpClient, configuration.appName(), addr, configuration.protocol()));
+ .map(addr ->new CbsClientRest(httpClient, configuration.appName(), addr, configuration.protocol()));
}
-}
+} \ No newline at end of file
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java
new file mode 100644
index 00000000..875e20ed
--- /dev/null
+++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java
@@ -0,0 +1,78 @@
+/*
+ * ============LICENSE_START=======================================================
+ * DCAEGEN2-SERVICES-SDK
+ * ================================================================================
+ * Copyright (C) 2021 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonObject;
+import org.jetbrains.annotations.NotNull;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
+import org.onap.dcaegen2.services.sdk.services.common.FileReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
+import reactor.core.publisher.Mono;
+import java.util.LinkedHashMap;
+
+public class CbsClientConfigMap implements CbsClient {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigMap.class);
+ private final String configMapFilePath;
+
+ public CbsClientConfigMap (String configMapFilePath) {
+ this.configMapFilePath = configMapFilePath;
+ }
+
+ @Override
+ public @NotNull Mono<JsonObject> get(CbsRequest request) {
+ return Mono.just(this.loadConfigMapFile())
+ .doOnNext(this::logConfigMapOutput);
+ }
+
+ public boolean verifyConfigMapFile() {
+ try {
+ LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath);
+ this.loadConfigMapFile().isJsonObject();
+ return true;
+ } catch(Exception ex) {
+ this.logConfigMapError(ex);
+ return false;
+ }
+ }
+
+ private JsonObject loadConfigMapFile() {
+ Gson gson = new GsonBuilder().create();
+ return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class);
+ }
+
+ private Object loadYamlConfigMapFile() {
+ return new Yaml().load(new FileReader(configMapFilePath).getContent());
+ }
+
+ private void logConfigMapOutput(JsonObject jsonObject) {
+ LOGGER.info("Got successful output from ConfigMap file");
+ LOGGER.debug("ConfigMap output: {}", jsonObject);
+ }
+
+ private void logConfigMapError(Exception ex) {
+ LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage());
+ }
+} \ No newline at end of file
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRest.java
index a895f3a1..4559a902 100644
--- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java
+++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* DCAEGEN2-SERVICES-SDK
* ================================================================================
- * Copyright (C) 2019 Nokia. All rights reserved.
+ * Copyright (C) 2019-2021 Nokia. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,15 +35,14 @@ import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
-public class CbsClientImpl implements CbsClient {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientImpl.class);
+public class CbsClientRest implements CbsClient {
+ private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientRest.class);
private final RxHttpClient httpClient;
private final String serviceName;
private final InetSocketAddress cbsAddress;
private final String protocol;
- public CbsClientImpl(RxHttpClient httpClient, String serviceName, InetSocketAddress cbsAddress, String protocol) {
+ public CbsClientRest(RxHttpClient httpClient, String serviceName, InetSocketAddress cbsAddress, String protocol) {
this.httpClient = httpClient;
this.serviceName = serviceName;
this.cbsAddress = cbsAddress;
@@ -65,6 +64,14 @@ public class CbsClientImpl implements CbsClient {
.doOnNext(this::logCbsResponse);
}
+ private void logRequestUrl(String url) {
+ LOGGER.debug("Calling {} for configuration", url);
+ }
+
+ private void logCbsResponse(JsonObject json) {
+ LOGGER.info("Got successful response from Config Binding Service");
+ LOGGER.debug("CBS response: {}", json);
+ }
private URL constructUrl(CbsRequest request) {
try {
@@ -77,13 +84,4 @@ public class CbsClientImpl implements CbsClient {
throw new IllegalArgumentException("Invalid CBS URL", e);
}
}
-
- private void logRequestUrl(String url) {
- LOGGER.debug("Calling {} for configuration", url);
- }
-
- private void logCbsResponse(JsonObject json) {
- LOGGER.info("Got successful response from Config Binding Service");
- LOGGER.debug("CBS response: {}", json);
- }
-}
+} \ No newline at end of file
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java
index 36555c2b..0a3b9657 100644
--- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java
+++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java
@@ -2,7 +2,7 @@
* ============LICENSE_START====================================
* DCAEGEN2-SERVICES-SDK
* =========================================================
- * Copyright (C) 2019 Nokia. All rights reserved.
+ * Copyright (C) 2019-2021 Nokia. All rights reserved.
* =========================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -126,6 +126,10 @@ public interface CbsClientConfiguration {
default String cbsName() {
return "config-binding-service";
}
+ @Value.Default
+ default String configMapFilePath() {
+ return "/app-config/application_config.yaml";
+ }
/**
* Creates CbsClientConfiguration from system environment variables.