diff options
author | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2021-11-16 10:36:10 +0100 |
---|---|---|
committer | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2022-01-25 13:10:49 +0100 |
commit | 59fe5fc76f320c16738f65f268db908faa884e14 (patch) | |
tree | 0bef2a82e3a9ca90012d526459f205201b7ded4a /src/main/java/org/onap | |
parent | 0b0abb996459578641823b4354830fd8747b5573 (diff) |
Switch configuration provider to CBS Client - DCAE SDK
Issue-ID: DCAEGEN2-2964
Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com>
Change-Id: Iaaf170d209b77b9709cc202c131f9d3bdf1033ed
Diffstat (limited to 'src/main/java/org/onap')
5 files changed, 75 insertions, 118 deletions
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java index 9cfddf0..0da2b7e 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. - * Copyright (C) 2021 Nokia. + * Copyright (C) 2021-2022 Nokia. * Copyright (C) 2021 Samsung Electronics. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,7 +35,6 @@ import org.onap.dcaegen2.services.pmmapper.config.DynamicConfiguration; import org.onap.dcaegen2.services.pmmapper.config.EnvironmentReader; import org.onap.dcaegen2.services.pmmapper.config.FilesProcessingConfig; import org.onap.dcaegen2.services.pmmapper.datarouter.DeliveryHandler; -import org.onap.dcaegen2.services.pmmapper.exceptions.CBSServerError; import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException; import org.onap.dcaegen2.services.pmmapper.exceptions.MapperConfigException; import org.onap.dcaegen2.services.pmmapper.exceptions.ProcessEventException; @@ -53,6 +52,12 @@ import org.onap.dcaegen2.services.pmmapper.utils.IncomingEventsCache; import org.onap.dcaegen2.services.pmmapper.utils.MeasConverter; import org.onap.dcaegen2.services.pmmapper.utils.MeasSplitter; import org.onap.dcaegen2.services.pmmapper.utils.XMLValidator; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import org.onap.logging.ref.slf4j.ONAPLogAdapter; import org.slf4j.LoggerFactory; import org.slf4j.MDC; @@ -119,8 +124,8 @@ public class App { public App(Path templatesDirectory, Path schemasDirectory, int httpPort, int httpsPort, ConfigHandler configHandler, FilesProcessingConfig filesProcessingConfig) throws EnvironmentConfigException { try { - this.mapperConfig = configHandler.getMapperConfig(); - } catch (EnvironmentConfigException | CBSServerError | MapperConfigException e) { + this.mapperConfig = configHandler.getInitialConfiguration(); + } catch (MapperConfigException e) { logger.unwrap().error("Failed to acquire initial configuration, Application cannot start", e); throw new IllegalStateException("Config acquisition failed"); } @@ -160,7 +165,7 @@ public class App { this.configScheduler.schedulePeriodically(this::reconfigure, INITIAL_RECONFIGURATION_PERIOD, RECONFIGURATION_PERIOD, TimeUnit.SECONDS); this.healthCheckHandler = new HealthCheckHandler(); this.deliveryHandler = new DeliveryHandler(fluxSink::next); - this.dynamicConfiguration = new DynamicConfiguration(Arrays.asList(mapperConfig), mapperConfig); + this.dynamicConfiguration = new DynamicConfiguration(Arrays.asList(mapperConfig), mapperConfig, configHandler); this.serverResources = Arrays.asList(healthCheckHandler, deliveryHandler, dynamicConfiguration); try { this.applicationServer = server(this.mapperConfig, this.serverResources); @@ -219,7 +224,12 @@ public class App { public static void main(String[] args) throws EnvironmentConfigException { FilesProcessingConfig processingConfig = new FilesProcessingConfig(new EnvironmentReader()); - new App(templates, schemas, HTTP_PORT, HTTPS_PORT, new ConfigHandler(), processingConfig).start(); + final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); + final CbsRequest request = CbsRequests.getConfiguration(diagnosticContext); + final CbsClientConfiguration config = CbsClientConfiguration.fromEnvironment(); + CbsClient cbsClient = CbsClientFactory.createCbsClient(config).block(); + + new App(templates, schemas, HTTP_PORT, HTTPS_PORT, new ConfigHandler(cbsClient, request), processingConfig).start(); } public static boolean filterByFileType(MeasFilterHandler filterHandler,Event event, MapperConfig config) { diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java index ae185f4..6c93d2f 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2022 Nokia. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,19 +20,19 @@ */ package org.onap.dcaegen2.services.pmmapper.config; -import com.google.gson.Gson; import com.google.gson.JsonObject; -import org.onap.dcaegen2.services.pmmapper.exceptions.CBSServerError; import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException; import org.onap.dcaegen2.services.pmmapper.exceptions.MapperConfigException; -import org.onap.dcaegen2.services.pmmapper.utils.EnvironmentConfig; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; -import org.onap.dcaegen2.services.pmmapper.utils.RequestSender; import org.onap.dcaegen2.services.pmmapper.utils.RequiredFieldDeserializer; +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.logging.ref.slf4j.ONAPLogAdapter; import org.slf4j.LoggerFactory; import com.google.gson.GsonBuilder; +import reactor.core.publisher.Mono; /** * Handles the retrieval of the component spec-based PM-Mapper Configuration @@ -39,24 +40,22 @@ import com.google.gson.GsonBuilder; */ public class ConfigHandler { + private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(ConfigHandler.class)); - private RequestSender sender; - private EnvironmentConfig environmentConfig; - /** - * Creates a ConfigHandler. - */ - public ConfigHandler() { - this(new RequestSender(), new EnvironmentConfig()); - } + private final CbsClient cbsClient; + private final CbsRequest cbsRequest; + + private MapperConfig mapperConfig; /** - * @see ConfigHandler#ConfigHandler() - * @param sender A RequestSender + * Creates a ConfigHandler based on Cbs Client and Cbs Request provided by DCAE SDK + * @param CbsClient A Cbs Client + * @param CbsRequest A Cbs Request */ - public ConfigHandler(RequestSender sender, EnvironmentConfig environmentConfig) { - this.sender = sender; - this.environmentConfig = environmentConfig; + public ConfigHandler(CbsClient cbsClient, CbsRequest cbsRequest){ + this.cbsClient = cbsClient; + this.cbsRequest = cbsRequest; } /** @@ -65,32 +64,53 @@ public class ConfigHandler { * @throws EnvironmentConfigException */ public MapperConfig getMapperConfig() throws EnvironmentConfigException { - String mapperConfigJson = ""; - String cbsSocketAddress = this.environmentConfig.getCBSHostName() + ":" + this.environmentConfig.getCBSPort(); - String requestURL = "http://" + cbsSocketAddress + "/service_component/" + this.environmentConfig.getServiceName(); - try { - logger.unwrap().info("Fetching pm-mapper configuration from Configbinding Service"); - mapperConfigJson = sender.send(requestURL); - } catch (Exception exception) { - throw new CBSServerError("Error connecting to Configbinding Service: ", exception); + + Mono.just(cbsClient) + .flatMap(client -> client.get(cbsRequest)) + .subscribe( + this::handleConfigurationFromConsul, + this::handleError + ); + + if (mapperConfig == null) { + logger.unwrap().error("Mapper configuration is not initialized"); + throw new EnvironmentConfigException("Mapper configuration is not initialized"); } - return convertMapperConfigToObject(mapperConfigJson); + return mapperConfig; } - private MapperConfig convertMapperConfigToObject(String mapperConfigJson) { - MapperConfig mapperConfig; + /** + * Retrieves Initial PM-Mapper Configuration from DCAE's ConfigBinding Service. + * + * @throws MapperConfigException + */ + public MapperConfig getInitialConfiguration() { + logger.unwrap().info("Attempt to get initial configuration"); + JsonObject jsonObject = Mono.just(cbsClient) + .flatMap(client -> client.get(cbsRequest)) + .block(); + handleConfigurationFromConsul(jsonObject); + return mapperConfig; + } + + void handleConfigurationFromConsul(JsonObject jsonObject) { + logger.unwrap().info("Attempt to process configuration object"); + try { - JsonObject config = new Gson().fromJson(mapperConfigJson, JsonObject.class); mapperConfig = new GsonBuilder() - .registerTypeAdapter(MapperConfig.class, new RequiredFieldDeserializer<MapperConfig>()) - .create() - .fromJson(config, MapperConfig.class); + .registerTypeAdapter(MapperConfig.class, new RequiredFieldDeserializer<MapperConfig>()) + .create() + .fromJson(jsonObject, MapperConfig.class); } catch (Exception exception) { - String exceptionMessage = "Error parsing configuration, mapper config:\n" + mapperConfigJson; + String exceptionMessage = "Error parsing configuration, mapper config:\n" + mapperConfig; + logger.unwrap().error(exceptionMessage); throw new MapperConfigException(exceptionMessage, exception); } - logger.unwrap().info("Received pm-mapper configuration from ConfigBinding Service"); - logger.unwrap().debug("Mapper configuration:\n{}", mapperConfig); - return mapperConfig; + logger.unwrap().info("PM-mapper configuration processed successful"); + logger.unwrap().info("Mapper configuration:\n{}", mapperConfig); + } + + private void handleError(Throwable throwable) { + logger.unwrap().error("Unexpected error occurred during fetching configuration", throwable); } } diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java index eee7d27..2cff1e3 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2022 Nokia. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,11 +45,11 @@ public class DynamicConfiguration extends ServerResource { * @param configurables list of objects to reconfigure * @param originalConfig original config to compare against. */ - public DynamicConfiguration(List<Configurable> configurables, MapperConfig originalConfig) { + public DynamicConfiguration(List<Configurable> configurables, MapperConfig originalConfig, ConfigHandler configHandler) { super(RECONFIGURE_ENDPOINT); this.configurables = configurables; this.originalConfig = originalConfig; - this.configHandler = new ConfigHandler(); + this.configHandler = configHandler; } private void applyConfiguration(MapperConfig updatedConfig) throws ReconfigurationException { diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/CBSServerError.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/CBSServerError.java deleted file mode 100644 index 58262ba..0000000 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/CBSServerError.java +++ /dev/null @@ -1,26 +0,0 @@ -/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-package org.onap.dcaegen2.services.pmmapper.exceptions;
-
-public class CBSServerError extends RuntimeException {
- public CBSServerError(String message, Throwable cause) {
- super(message, cause);
- }
-}
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/EnvironmentConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/EnvironmentConfig.java deleted file mode 100644 index 8457989..0000000 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/EnvironmentConfig.java +++ /dev/null @@ -1,48 +0,0 @@ -/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-package org.onap.dcaegen2.services.pmmapper.utils;
-
-import java.util.Optional;
-
-import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException;
-
-public class EnvironmentConfig {
- public static final int DEFAULT_CBS_PORT = 10000;
- public static final String ENV_CBS_HOST_KEY = "CONFIG_BINDING_SERVICE_SERVICE_HOST";
- public static final String ENV_CBS_PORT_KEY = "CONFIG_BINDING_SERVICE_SERVICE_PORT";
- public static final String ENV_SERVICE_NAME_KEY = "HOSTNAME";
-
- public String getServiceName() throws EnvironmentConfigException {
- return Optional.ofNullable(System.getenv(ENV_SERVICE_NAME_KEY))
- .orElseThrow(() -> new EnvironmentConfigException(
- ENV_SERVICE_NAME_KEY+ " environment variable must be defined prior to pm-mapper initialization."));
- }
-
- public String getCBSHostName() throws EnvironmentConfigException {
- return Optional.ofNullable(System.getenv(ENV_CBS_HOST_KEY))
- .orElseThrow(() -> new EnvironmentConfigException(
- ENV_CBS_HOST_KEY+ " environment variable must be defined prior to pm-mapper initialization."));
- }
-
- public Integer getCBSPort() {
- return Optional.ofNullable(System.getenv(ENV_CBS_PORT_KEY))
- .map(Integer::valueOf).orElse(DEFAULT_CBS_PORT);
- }
-}
|