2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
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.apex.services.onappf.handler;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.List;
26 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
27 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
28 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
29 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
30 import org.onap.policy.common.endpoints.event.comm.TopicSink;
31 import org.onap.policy.common.utils.services.Registry;
32 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
33 import org.onap.policy.models.pdp.concepts.PdpStatus;
34 import org.onap.policy.models.pdp.concepts.PdpUpdate;
35 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
36 import org.onap.policy.models.pdp.enums.PdpState;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * This class supports the handling of pdp update messages.
44 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
46 public class PdpUpdateMessageHandler {
48 private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
51 * Method which handles a pdp update event from PAP.
53 * @param pdpUpdateMsg pdp update message
55 public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
56 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
57 final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
58 PdpResponseDetails pdpResponseDetails = null;
59 if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
60 pdpStatusContext.getPdpSubgroup())) {
61 if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
62 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
63 PdpResponseStatus.SUCCESS, "Pdp already updated");
65 pdpResponseDetails = handlePdpUpdate(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext);
67 final PdpStatusPublisher pdpStatusPublisherTemp =
68 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
69 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
70 pdpStatus.setResponse(pdpResponseDetails);
71 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
72 pdpStatusPublisherTemp.send(pdpStatus);
77 * Method to do pdp update.
79 * @param pdpUpdateMsg the pdp update message
80 * @param pdpMessageHandler the message handler
81 * @param pdpStatusContext the pdp status in memory
82 * @return pdpResponseDetails the pdp response
84 private PdpResponseDetails handlePdpUpdate(final PdpUpdate pdpUpdateMsg, final PdpMessageHandler pdpMessageHandler,
85 final PdpStatus pdpStatusContext) {
86 PdpResponseDetails pdpResponseDetails = null;
87 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
88 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
89 && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
90 updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
92 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
93 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
95 .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
96 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
97 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
98 pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
100 ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
101 // in hearbeat while in active state, only the policies which are running should be there.
102 // if some policy fails, that shouldn't go in the heartbeat.
103 // If no policies are running, then the policy list in the heartbeat can be empty
104 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
105 pdpStatusContext.setPolicies(apexEngineHandler.getRunningPolicies());
107 pdpStatusContext.setPolicies(Collections.emptyList());
110 if (null == pdpResponseDetails) {
111 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
112 PdpResponseStatus.SUCCESS, "Pdp update successful.");
114 return pdpResponseDetails;
118 * Method to start or stop apex engine based on the list of policies received from pap. When current state is
119 * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies,
120 * stop the current running policies and the deploy the new ones.
122 * @param pdpUpdateMsg the pdp update message from pap
123 * @param pdpMessageHandler pdp message handler
124 * @param pdpStatusContext the pdp status object in memory
125 * @return pdpResponseDetails the pdp response
127 private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
128 final PdpMessageHandler pdpMessageHandler) {
129 PdpResponseDetails pdpResponseDetails = null;
130 ApexEngineHandler apexEngineHandler = null;
132 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
133 } catch (final IllegalArgumentException e) {
134 LOGGER.debug("ApenEngineHandler not in registry.", e);
136 if (pdpUpdateMsg.getPolicies().isEmpty()) {
137 pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
139 pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
141 return pdpResponseDetails;
144 private PdpResponseDetails stopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
145 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
146 PdpResponseDetails pdpResponseDetails = null;
147 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
149 apexEngineHandler.shutdown();
150 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
151 PdpResponseStatus.SUCCESS, "Pdp update successful. No policies are running.");
152 } catch (final ApexStarterException e) {
153 LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
154 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
155 PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
158 return pdpResponseDetails;
161 private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
162 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
163 PdpResponseDetails pdpResponseDetails = null;
166 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
167 apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPolicies());
169 apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPolicies());
170 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
172 if (apexEngineHandler.isApexEngineRunning()) {
173 List<ToscaConceptIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
174 if (new HashSet<>(runningPolicies)
175 .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())))) {
176 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
177 PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
179 StringBuilder message =
180 new StringBuilder("Apex engine started. But, only the following polices are running - ");
181 for (ToscaConceptIdentifier policy : runningPolicies) {
182 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
184 message.append(". Other policies failed execution. Please see the logs for more details.");
185 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
186 PdpResponseStatus.SUCCESS, message.toString());
189 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
190 PdpResponseStatus.FAIL, "Apex engine failed to start.");
192 } catch (final ApexStarterException e) {
193 LOGGER.error("Apex engine service running failed. ", e);
194 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
195 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
197 final ApexPolicyStatisticsManager apexPolicyStatisticsManager =
198 ApexPolicyStatisticsManager.getInstanceFromRegistry();
199 if (apexPolicyStatisticsManager != null) {
200 apexPolicyStatisticsManager
201 .updatePolicyDeployCounter(pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
203 return pdpResponseDetails;
207 * Method checks if the Pdp update message is already handled by checking the values in the context.
209 * @param pdpUpdateMsg pdp update message received from pap
210 * @param pdpStatusContext values saved in context memory
211 * @return boolean flag which tells if the information is same or not
213 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
214 return null != pdpStatusContext.getPdpGroup()
215 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
216 && null != pdpStatusContext.getPdpSubgroup()
217 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
218 && null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
219 .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
223 * Method to update the time interval used by the timer task.
225 * @param interval time interval received in the pdp update message from pap
227 public void updateInterval(final long interval) {
228 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
229 pdpStatusPublisher.terminate();
230 final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
231 Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
232 new PdpStatusPublisher(topicSinks, interval));