2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.distribution.reception.handling.sdc;
 
  23 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  24 import org.onap.policy.common.logging.flexlogger.Logger;
 
  25 import org.onap.policy.common.parameters.ParameterService;
 
  26 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
 
  27 import org.onap.policy.distribution.reception.decoding.PluginTerminationException;
 
  28 import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler;
 
  29 import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup;
 
  30 import org.onap.sdc.api.IDistributionClient;
 
  31 import org.onap.sdc.api.results.IDistributionClientResult;
 
  32 import org.onap.sdc.impl.DistributionClientFactory;
 
  33 import org.onap.sdc.utils.DistributionActionResultEnum;
 
  36  * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded.
 
  38 public class SdcReceptionHandler extends AbstractReceptionHandler {
 
  40     private static final Logger LOGGER = FlexLogger.getLogger(SdcReceptionHandler.class);
 
  41     private SdcReceptionHandlerStatus sdcReceptionHandlerStatus = SdcReceptionHandlerStatus.STOPPED;
 
  42     private PssdConfigurationParametersGroup handlerParameters;
 
  43     private IDistributionClient distributionClient;
 
  44     private volatile int nbOfNotificationsOngoing = 0;
 
  47     protected void initializeReception(final String parameterGroupName) throws PluginInitializationException {
 
  48         handlerParameters = (PssdConfigurationParametersGroup) ParameterService.get(parameterGroupName);
 
  49         initializeSdcClient();
 
  53     // Add functionality for receiving SDC distibutions and invoking AbstractReceptionHandler
 
  57     public void destroy() throws PluginTerminationException {
 
  58         LOGGER.debug("Going to stop the SDC Client...");
 
  59         if (distributionClient != null) {
 
  60             final IDistributionClientResult clientResult = distributionClient.stop();
 
  61             if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
  62                 final String message =
 
  63                         "SDC client stop failed with reason:" + clientResult.getDistributionMessageResult();
 
  64                 LOGGER.error(message);
 
  65                 throw new PluginTerminationException(message);
 
  68         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
 
  69         LOGGER.debug("SDC Client is stopped successfully");
 
  73      * Method to change the status of this reception handler instance.
 
  75      * @param newStatus the new status
 
  77     protected synchronized final void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) {
 
  81                 sdcReceptionHandlerStatus = newStatus;
 
  84                 if (nbOfNotificationsOngoing > 1) {
 
  85                     --nbOfNotificationsOngoing;
 
  87                     nbOfNotificationsOngoing = 0;
 
  88                     sdcReceptionHandlerStatus = newStatus;
 
  92                 ++nbOfNotificationsOngoing;
 
  93                 sdcReceptionHandlerStatus = newStatus;
 
  99      * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}.
 
 101      * @return the {@link IDistributionClient} instance
 
 103     protected IDistributionClient createSdcDistributionClient() {
 
 104         return DistributionClientFactory.createDistributionClient();
 
 108      * Method to initialize the SDC client.
 
 110      * @throws PluginInitializationException if the initialization of SDC Client fails
 
 112     private void initializeSdcClient() throws PluginInitializationException {
 
 114         LOGGER.debug("Going to initialize the SDC Client...");
 
 115         if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) {
 
 116             final String message = "The SDC Client is already initialized";
 
 117             LOGGER.error(message);
 
 118             throw new PluginInitializationException(message);
 
 120         final SdcConfiguration sdcConfig = new SdcConfiguration(handlerParameters);
 
 121         distributionClient = createSdcDistributionClient();
 
 122         final IDistributionClientResult clientResult =
 
 123                 distributionClient.init(sdcConfig, new SdcNotificationCallBack());
 
 124         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 125             changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
 
 126             final String message =
 
 127                     "SDC client initialization failed with reason:" + clientResult.getDistributionMessageResult();
 
 128             LOGGER.error(message);
 
 129             throw new PluginInitializationException(message);
 
 131         LOGGER.debug("SDC Client is initialized successfully");
 
 132         this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
 
 136      * Method to start the SDC client.
 
 138      * @param configParameter the configuration parameters
 
 139      * @throws PluginInitializationException if the start of SDC Client fails
 
 141     private void startSdcClient() throws PluginInitializationException {
 
 143         LOGGER.debug("Going to start the SDC Client...");
 
 144         final IDistributionClientResult clientResult = distributionClient.start();
 
 145         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 146             changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
 
 147             final String message = "SDC client start failed with reason:" + clientResult.getDistributionMessageResult();
 
 148             LOGGER.error(message);
 
 149             throw new PluginInitializationException(message);
 
 151         LOGGER.debug("SDC Client is started successfully");
 
 152         this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);