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