From 837f51c82b40d9c251a082768af7a196ff7c901c Mon Sep 17 00:00:00 2001 From: Niranjana Date: Tue, 1 Jun 2021 09:47:36 +0000 Subject: Update CBS-Client to read policy configuration from a file exposed by policy-sidecar container Issue-ID: DCAEGEN2-2752 Signed-off-by: Niranjana Change-Id: Ib00d1b031021c0342b0845b63d65172333ac4158 --- .../services/cbs/client/api/CbsClientFactory.java | 6 +- .../cbs/client/impl/CbsClientConfigMap.java | 71 +++++++++++++++++++--- .../cbs/client/model/CbsClientConfiguration.java | 5 ++ 3 files changed, 71 insertions(+), 11 deletions(-) (limited to 'rest-services/cbs-client/src/main/java') 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 f1e49bb7..00dbf8a0 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 @@ -3,6 +3,7 @@ * DCAEGEN2-SERVICES-SDK * ================================================================================ * Copyright (C) 2019-2021 Nokia. All rights reserved. + * Copyright (C) 2021 Wipro Limited. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,7 +68,8 @@ public class CbsClientFactory { private static Mono createCbsClientMono(RxHttpClient httpClient, CbsClientConfiguration configuration) { - CbsClientConfigMap cbsClientConfigMap = new CbsClientConfigMap(configuration.configMapFilePath()); + CbsClientConfigMap cbsClientConfigMap = new CbsClientConfigMap(configuration.configMapFilePath(), + configuration.policySyncFilePath(), configuration.appName()); return cbsClientConfigMap.verifyConfigMapFile() ? Mono.just(cbsClientConfigMap) : getConfigFromCBS(httpClient, configuration); } @@ -76,4 +78,4 @@ public class CbsClientFactory { return new CbsLookup().lookup(configuration) .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 index 42f53616..75577318 100644 --- 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 @@ -3,6 +3,7 @@ * DCAEGEN2-SERVICES-SDK * ================================================================================ * Copyright (C) 2021 Nokia. All rights reserved. + * Copyright (C) 2021 Wipro Limited. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,11 +18,15 @@ * 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 java.util.LinkedHashMap; + 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; @@ -30,23 +35,33 @@ 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; + private String policySyncFilePath = ""; + private String appName = ""; + public CbsClientConfigMap(String configMapFilePath) { + this.configMapFilePath = configMapFilePath; + } - public CbsClientConfigMap (String configMapFilePath) { + public CbsClientConfigMap(String configMapFilePath, String policySyncFilePath, String appName) { this.configMapFilePath = configMapFilePath; + this.policySyncFilePath = policySyncFilePath; + this.appName = appName; } @Override public @NotNull Mono get(CbsRequest request) { - return Mono.just(this.loadConfigMapFile()) - .map(CbsClientEnvironmentParsing::processEnvironmentVariables) - .doOnNext(this::logConfigMapOutput); + Mono configJsonMono = + Mono.just(this.loadConfigMapFile()).map(CbsClientEnvironmentParsing::processEnvironmentVariables); + if (this.shouldReadPolicySyncFile(request)) { + + return configJsonMono.map(this::loadPolicySyncFile).doOnNext(this::logConfigMapOutput); + } + return configJsonMono.doOnNext(this::logConfigMapOutput); } public boolean verifyConfigMapFile() { @@ -54,7 +69,7 @@ public class CbsClientConfigMap implements CbsClient { LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath); this.loadConfigMapFile().isJsonObject(); return true; - } catch(Exception ex) { + } catch (Exception ex) { this.logConfigMapError(ex); return false; } @@ -69,8 +84,6 @@ public class CbsClientConfigMap implements CbsClient { 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); @@ -79,4 +92,44 @@ public class CbsClientConfigMap implements CbsClient { private void logConfigMapError(Exception ex) { LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage()); } -} \ No newline at end of file + + private boolean shouldReadPolicySyncFile(CbsRequest request) { + try { + return request.requestPath() + .getForService(appName) + .contains("service_component_all"); + } catch (Exception ex) { + LOGGER.error("Error finding requestPath", ex.getMessage()); + return false; + } + } + + private JsonObject loadPolicySyncFile(JsonObject configJsonObject) { + + try { + + if (new FileReader(policySyncFilePath).doesFileExists()) { + LOGGER.info("PolicySync file is present"); + Gson gson = new GsonBuilder().create(); + JsonObject policyJsonObject = gson.fromJson(this.loadJsonStringPolicySyncFile(), JsonObject.class); + policyJsonObject.add("config", configJsonObject); + return policyJsonObject; + } + LOGGER.info("PolicySync file does not exist"); + JsonObject policyJsonObject = new JsonObject(); + policyJsonObject.add("config", configJsonObject); + return policyJsonObject; + + } catch (Exception ex) { + LOGGER.error("PolicySync file does not contain a valid json"); + JsonObject policyJsonObject = new JsonObject(); + policyJsonObject.add("config", configJsonObject); + return policyJsonObject; + } + + } + + private String loadJsonStringPolicySyncFile() { + return new FileReader(policySyncFilePath).getContent(); + } +} 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 0a3b9657..41848855 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 @@ -3,6 +3,7 @@ * DCAEGEN2-SERVICES-SDK * ========================================================= * Copyright (C) 2019-2021 Nokia. All rights reserved. + * Copyright (C) 2021 Wipro Limited. * ========================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -130,6 +131,10 @@ public interface CbsClientConfiguration { default String configMapFilePath() { return "/app-config/application_config.yaml"; } + @Value.Default + default String policySyncFilePath() { + return "/etc/policies/policies.json"; + } /** * Creates CbsClientConfiguration from system environment variables. -- cgit