60c94e2bdb59ba9748b90d8ed538766c7167c4e6
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling.sdc;
22
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;
34
35 /**
36  * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded.
37  */
38 public class SdcReceptionHandler extends AbstractReceptionHandler {
39
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;
45
46     @Override
47     protected void initializeReception(final String parameterGroupName) throws PluginInitializationException {
48         handlerParameters = (PssdConfigurationParametersGroup) ParameterService.get(parameterGroupName);
49         initializeSdcClient();
50         startSdcClient();
51     }
52
53     // Add functionality for receiving SDC distibutions and invoking AbstractReceptionHandler
54     // inputReceived()
55
56     @Override
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);
66             }
67         }
68         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
69         LOGGER.debug("SDC Client is stopped successfully");
70     }
71
72     /**
73      * Method to change the status of this reception handler instance.
74      *
75      * @param newStatus the new status
76      */
77     protected synchronized final void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) {
78         switch (newStatus) {
79             case INIT:
80             case STOPPED:
81                 sdcReceptionHandlerStatus = newStatus;
82                 break;
83             case IDLE:
84                 if (nbOfNotificationsOngoing > 1) {
85                     --nbOfNotificationsOngoing;
86                 } else {
87                     nbOfNotificationsOngoing = 0;
88                     sdcReceptionHandlerStatus = newStatus;
89                 }
90                 break;
91             case BUSY:
92                 ++nbOfNotificationsOngoing;
93                 sdcReceptionHandlerStatus = newStatus;
94                 break;
95         }
96     }
97
98     /**
99      * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}.
100      *
101      * @return the {@link IDistributionClient} instance
102      */
103     protected IDistributionClient createSdcDistributionClient() {
104         return DistributionClientFactory.createDistributionClient();
105     }
106
107     /**
108      * Method to initialize the SDC client.
109      *
110      * @throws PluginInitializationException if the initialization of SDC Client fails
111      */
112     private void initializeSdcClient() throws PluginInitializationException {
113
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);
119         }
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);
130         }
131         LOGGER.debug("SDC Client is initialized successfully");
132         this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
133     }
134
135     /**
136      * Method to start the SDC client.
137      *
138      * @param configParameter the configuration parameters
139      * @throws PluginInitializationException if the start of SDC Client fails
140      */
141     private void startSdcClient() throws PluginInitializationException {
142
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);
150         }
151         LOGGER.debug("SDC Client is started successfully");
152         this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
153     }
154 }