From ecc059f29f8da065356571ef00c6cad595e298b3 Mon Sep 17 00:00:00 2001 From: ramverma Date: Fri, 31 Aug 2018 17:16:01 +0100 Subject: Adding code for managing life cycle of SDC Client * Adding init/start/stop methods in SDCReceptionHandler for managing lifecycle of SDC Client. * Adding a handler status enum to hold all the possible status values. * Adding test cases to cover code changes fully. Change-Id: Ib6f370485ff330538bfada6030c592629ed3fd1c Issue-ID: POLICY-956 Signed-off-by: ramverma --- .../reception/handling/sdc/PssdConfiguration.java | 126 -------------------- .../reception/handling/sdc/SdcConfiguration.java | 119 +++++++++++++++++++ .../handling/sdc/SdcNotificationCallBack.java | 45 ++++++++ .../handling/sdc/SdcReceptionHandler.java | 128 +++++++++++++++++++-- .../handling/sdc/SdcReceptionHandlerStatus.java | 49 ++++++++ 5 files changed, 332 insertions(+), 135 deletions(-) delete mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfiguration.java create mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcConfiguration.java create mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java create mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerStatus.java (limited to 'plugins/reception-plugins/src/main/java/org/onap') diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfiguration.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfiguration.java deleted file mode 100644 index c5c877e1..00000000 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PssdConfiguration.java +++ /dev/null @@ -1,126 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Intel. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.distribution.reception.handling.sdc; - -import java.util.List; - -import org.onap.sdc.api.consumer.IConfiguration; -import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup; - -/** - * Properties for the handling Sdc - * - */ -public class PssdConfiguration implements IConfiguration { - - // Configuration file structure - - // Configuration file properties - private PssdConfigurationParametersGroup configParameters = null; - - /** - * Original constructor - * - * @param configParameters properties needed to be configured for the model loader - */ - public PssdConfiguration(final PssdConfigurationParametersGroup configParameters) { - this.configParameters = configParameters; - - } - - - @Override - public String getAsdcAddress() { - return configParameters.getAsdcAddress(); - } - - @Override - public List getMsgBusAddress() { - return configParameters.getMsgBusAddress(); - } - - @Override - public String getUser() { - return configParameters.getUser(); - } - - @Override - public String getPassword() { - return configParameters.getPassword(); - } - - @Override - public int getPollingInterval() { - return configParameters.getPollingInterval(); - } - - @Override - public int getPollingTimeout() { - return configParameters.getPollingTimeout(); - } - - @Override - public List getRelevantArtifactTypes() { - return configParameters.getArtifactTypes(); - } - - @Override - public String getConsumerGroup() { - return configParameters.getConsumerGroup(); - } - - @Override - public String getEnvironmentName() { - return configParameters.getEnvironmentName(); - } - - @Override - public String getConsumerID() { - return configParameters.getConsumerID(); - } - - @Override - public String getKeyStorePassword() { - return configParameters.getKeyStorePassword(); - } - - @Override - public String getKeyStorePath() { - return configParameters.getKeyStorePath(); - } - - @Override - public boolean activateServerTLSAuth() { - return configParameters.activateServerTLSAuth(); - } - - @Override - public boolean isFilterInEmptyResources() { - return configParameters.isFilterInEmptyResources(); - } - - @Override - public Boolean isUseHttpsWithDmaap() { - return configParameters.isUseHttpsWithDmaap(); - } - - -} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcConfiguration.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcConfiguration.java new file mode 100644 index 00000000..945670b9 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcConfiguration.java @@ -0,0 +1,119 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.handling.sdc; + +import java.util.List; + +import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup; +import org.onap.sdc.api.consumer.IConfiguration; + +/** + * This class represents the configurations needed for SDC Client. + * + */ +public class SdcConfiguration implements IConfiguration { + + private PssdConfigurationParametersGroup configParameters = null; + + /** + * Constructor for instantiating {@link SdcConfiguration}. + * + * @param configParameters the SDC Client configuration parameters + */ + public SdcConfiguration(final PssdConfigurationParametersGroup configParameters) { + this.configParameters = configParameters; + } + + @Override + public String getAsdcAddress() { + return configParameters.getAsdcAddress(); + } + + @Override + public List getMsgBusAddress() { + return configParameters.getMsgBusAddress(); + } + + @Override + public String getUser() { + return configParameters.getUser(); + } + + @Override + public String getPassword() { + return configParameters.getPassword(); + } + + @Override + public int getPollingInterval() { + return configParameters.getPollingInterval(); + } + + @Override + public int getPollingTimeout() { + return configParameters.getPollingTimeout(); + } + + @Override + public List getRelevantArtifactTypes() { + return configParameters.getArtifactTypes(); + } + + @Override + public String getConsumerGroup() { + return configParameters.getConsumerGroup(); + } + + @Override + public String getEnvironmentName() { + return configParameters.getEnvironmentName(); + } + + @Override + public String getConsumerID() { + return configParameters.getConsumerID(); + } + + @Override + public String getKeyStorePassword() { + return configParameters.getKeyStorePassword(); + } + + @Override + public String getKeyStorePath() { + return configParameters.getKeyStorePath(); + } + + @Override + public boolean activateServerTLSAuth() { + return configParameters.activateServerTLSAuth(); + } + + @Override + public boolean isFilterInEmptyResources() { + return configParameters.isFilterInEmptyResources(); + } + + @Override + public Boolean isUseHttpsWithDmaap() { + return configParameters.isUseHttpsWithDmaap(); + } +} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java new file mode 100644 index 00000000..0ea57799 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.handling.sdc; + +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.sdc.api.consumer.INotificationCallback; +import org.onap.sdc.api.notification.INotificationData; + +/** + * Class to provide an implementation of INotificationCallback interface for receiving the incoming distribution + * notifications from SDC. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class SdcNotificationCallBack implements INotificationCallback { + + private static final Logger LOGGER = FlexLogger.getLogger(SdcNotificationCallBack.class); + + @Override + public void activateCallback(final INotificationData notificationData) { + + LOGGER.debug("Got the message from SDC:" + notificationData.getDistributionID()); + // Code for handling notification will come here + } + +} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java index 494f47f6..60c94e2b 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java @@ -5,40 +5,150 @@ * 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.policy.distribution.reception.handling.sdc; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.reception.decoding.PluginInitializationException; +import org.onap.policy.distribution.reception.decoding.PluginTerminationException; import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; +import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup; +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.results.IDistributionClientResult; +import org.onap.sdc.impl.DistributionClientFactory; +import org.onap.sdc.utils.DistributionActionResultEnum; /** - * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may - * be decoded. + * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded. */ public class SdcReceptionHandler extends AbstractReceptionHandler { + private static final Logger LOGGER = FlexLogger.getLogger(SdcReceptionHandler.class); + private SdcReceptionHandlerStatus sdcReceptionHandlerStatus = SdcReceptionHandlerStatus.STOPPED; + private PssdConfigurationParametersGroup handlerParameters; + private IDistributionClient distributionClient; + private volatile int nbOfNotificationsOngoing = 0; + @Override - protected void initializeReception(String parameterGroupName) { - // Set up subscription to SDC + protected void initializeReception(final String parameterGroupName) throws PluginInitializationException { + handlerParameters = (PssdConfigurationParametersGroup) ParameterService.get(parameterGroupName); + initializeSdcClient(); + startSdcClient(); } // Add functionality for receiving SDC distibutions and invoking AbstractReceptionHandler // inputReceived() @Override - public void destroy() { - // Tear down subscription etc + public void destroy() throws PluginTerminationException { + LOGGER.debug("Going to stop the SDC Client..."); + if (distributionClient != null) { + final IDistributionClientResult clientResult = distributionClient.stop(); + if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) { + final String message = + "SDC client stop failed with reason:" + clientResult.getDistributionMessageResult(); + LOGGER.error(message); + throw new PluginTerminationException(message); + } + } + changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED); + LOGGER.debug("SDC Client is stopped successfully"); + } + + /** + * Method to change the status of this reception handler instance. + * + * @param newStatus the new status + */ + protected synchronized final void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) { + switch (newStatus) { + case INIT: + case STOPPED: + sdcReceptionHandlerStatus = newStatus; + break; + case IDLE: + if (nbOfNotificationsOngoing > 1) { + --nbOfNotificationsOngoing; + } else { + nbOfNotificationsOngoing = 0; + sdcReceptionHandlerStatus = newStatus; + } + break; + case BUSY: + ++nbOfNotificationsOngoing; + sdcReceptionHandlerStatus = newStatus; + break; + } + } + + /** + * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}. + * + * @return the {@link IDistributionClient} instance + */ + protected IDistributionClient createSdcDistributionClient() { + return DistributionClientFactory.createDistributionClient(); } + /** + * Method to initialize the SDC client. + * + * @throws PluginInitializationException if the initialization of SDC Client fails + */ + private void initializeSdcClient() throws PluginInitializationException { + + LOGGER.debug("Going to initialize the SDC Client..."); + if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) { + final String message = "The SDC Client is already initialized"; + LOGGER.error(message); + throw new PluginInitializationException(message); + } + final SdcConfiguration sdcConfig = new SdcConfiguration(handlerParameters); + distributionClient = createSdcDistributionClient(); + final IDistributionClientResult clientResult = + distributionClient.init(sdcConfig, new SdcNotificationCallBack()); + if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) { + changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED); + final String message = + "SDC client initialization failed with reason:" + clientResult.getDistributionMessageResult(); + LOGGER.error(message); + throw new PluginInitializationException(message); + } + LOGGER.debug("SDC Client is initialized successfully"); + this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT); + } + + /** + * Method to start the SDC client. + * + * @param configParameter the configuration parameters + * @throws PluginInitializationException if the start of SDC Client fails + */ + private void startSdcClient() throws PluginInitializationException { + + LOGGER.debug("Going to start the SDC Client..."); + final IDistributionClientResult clientResult = distributionClient.start(); + if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) { + changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED); + final String message = "SDC client start failed with reason:" + clientResult.getDistributionMessageResult(); + LOGGER.error(message); + throw new PluginInitializationException(message); + } + LOGGER.debug("SDC Client is started successfully"); + this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE); + } } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerStatus.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerStatus.java new file mode 100644 index 00000000..d0e04c78 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandlerStatus.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.handling.sdc; + +/** + * Class to hold the possible values for status of {@link SdcReceptionHandler}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public enum SdcReceptionHandlerStatus { + + /** + * The SdcReceptionHandler is not alive. + */ + STOPPED, + + /** + * The SdcReceptionHandler is initialized but not started yet. + */ + INIT, + + /** + * The SdcReceptionHandler is initialized, started & ready to handle incoming notifications. + */ + IDLE, + + /** + * The SdcReceptionHandler is currently busy in handling notifications. + */ + BUSY +} -- cgit 1.2.3-korg