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.sdc.api.IDistributionClient;
30 import org.onap.sdc.api.results.IDistributionClientResult;
31 import org.onap.sdc.impl.DistributionClientFactory;
32 import org.onap.sdc.utils.DistributionActionResultEnum;
35 * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded.
37 public class SdcReceptionHandler extends AbstractReceptionHandler {
39 private static final Logger LOGGER = FlexLogger.getLogger(SdcReceptionHandler.class);
40 private SdcReceptionHandlerStatus sdcReceptionHandlerStatus = SdcReceptionHandlerStatus.STOPPED;
41 private SdcReceptionHandlerConfigurationParameterGroup handlerParameters;
42 private IDistributionClient distributionClient;
43 private volatile int nbOfNotificationsOngoing = 0;
46 protected void initializeReception(final String parameterGroupName) throws PluginInitializationException {
47 handlerParameters = (SdcReceptionHandlerConfigurationParameterGroup) ParameterService.get(parameterGroupName);
48 initializeSdcClient();
52 // Add functionality for receiving SDC distibutions and invoking AbstractReceptionHandler
56 public void destroy() throws PluginTerminationException {
57 LOGGER.debug("Going to stop the SDC Client...");
58 if (distributionClient != null) {
59 final IDistributionClientResult clientResult = distributionClient.stop();
60 if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
61 final String message =
62 "SDC client stop failed with reason:" + clientResult.getDistributionMessageResult();
63 LOGGER.error(message);
64 throw new PluginTerminationException(message);
67 changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
68 LOGGER.debug("SDC Client is stopped successfully");
72 * Method to change the status of this reception handler instance.
74 * @param newStatus the new status
76 protected synchronized final void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) {
80 sdcReceptionHandlerStatus = newStatus;
83 if (nbOfNotificationsOngoing > 1) {
84 --nbOfNotificationsOngoing;
86 nbOfNotificationsOngoing = 0;
87 sdcReceptionHandlerStatus = newStatus;
91 ++nbOfNotificationsOngoing;
92 sdcReceptionHandlerStatus = newStatus;
98 * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}.
100 * @return the {@link IDistributionClient} instance
102 protected IDistributionClient createSdcDistributionClient() {
103 return DistributionClientFactory.createDistributionClient();
107 * Method to initialize the SDC client.
109 * @throws PluginInitializationException if the initialization of SDC Client fails
111 private void initializeSdcClient() throws PluginInitializationException {
113 LOGGER.debug("Going to initialize the SDC Client...");
114 if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) {
115 final String message = "The SDC Client is already initialized";
116 LOGGER.error(message);
117 throw new PluginInitializationException(message);
119 final SdcConfiguration sdcConfig = new SdcConfiguration(handlerParameters);
120 distributionClient = createSdcDistributionClient();
121 final IDistributionClientResult clientResult =
122 distributionClient.init(sdcConfig, new SdcNotificationCallBack());
123 if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
124 changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
125 final String message =
126 "SDC client initialization failed with reason:" + clientResult.getDistributionMessageResult();
127 LOGGER.error(message);
128 throw new PluginInitializationException(message);
130 LOGGER.debug("SDC Client is initialized successfully");
131 this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
135 * Method to start the SDC client.
137 * @param configParameter the configuration parameters
138 * @throws PluginInitializationException if the start of SDC Client fails
140 private void startSdcClient() throws PluginInitializationException {
142 LOGGER.debug("Going to start the SDC Client...");
143 final IDistributionClientResult clientResult = distributionClient.start();
144 if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
145 changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
146 final String message = "SDC client start failed with reason:" + clientResult.getDistributionMessageResult();
147 LOGGER.error(message);
148 throw new PluginInitializationException(message);
150 LOGGER.debug("SDC Client is started successfully");
151 this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);