2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.services.onappf.handler;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
32 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
33 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
34 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
35 import org.onap.policy.common.endpoints.event.comm.TopicSink;
36 import org.onap.policy.common.utils.services.Registry;
37 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
38 import org.onap.policy.models.pdp.concepts.PdpStatus;
39 import org.onap.policy.models.pdp.concepts.PdpUpdate;
40 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
41 import org.onap.policy.models.pdp.enums.PdpState;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * This class supports the handling of pdp update messages.
51 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
53 public class PdpUpdateMessageHandler {
55 private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
58 * Method which handles a pdp update event from PAP.
60 * @param pdpUpdateMsg pdp update message
62 public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
63 final var pdpMessageHandler = new PdpMessageHandler();
64 final var pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
65 PdpResponseDetails pdpResponseDetails = null;
66 if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
67 pdpStatusContext.getPdpSubgroup())) {
68 if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
69 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
70 PdpResponseStatus.SUCCESS, "Pdp already updated");
72 pdpResponseDetails = handlePdpUpdate(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext);
74 final var pdpStatusPublisherTemp =
75 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
76 final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
77 pdpStatus.setResponse(pdpResponseDetails);
78 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
79 pdpStatusPublisherTemp.send(pdpStatus);
84 * Method to do pdp update.
86 * @param pdpUpdateMsg the pdp update message
87 * @param pdpMessageHandler the message handler
88 * @param pdpStatusContext the pdp status in memory
89 * @return pdpResponseDetails the pdp response
91 private PdpResponseDetails handlePdpUpdate(final PdpUpdate pdpUpdateMsg, final PdpMessageHandler pdpMessageHandler,
92 final PdpStatus pdpStatusContext) {
93 PdpResponseDetails pdpResponseDetails = null;
94 final var pdpStatusPublisher =
95 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
96 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
97 && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
98 updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
100 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
101 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
102 @SuppressWarnings("unchecked")
103 List<ToscaPolicy> policies = Registry.getOrDefault(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
104 List.class, new ArrayList<>());
105 policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed());
106 Set<ToscaConceptIdentifier> policiesInDeployment = policies.stream().map(ToscaPolicy::getIdentifier)
107 .collect(Collectors.toSet());
108 policiesInDeployment.removeAll(pdpUpdateMsg.getPoliciesToBeUndeployed());
109 pdpStatusContext.setPolicies(new ArrayList<>(policiesInDeployment));
110 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
112 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
113 pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
115 var apexEngineHandler =
116 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
117 // in hearbeat while in active state, only the policies which are running should be there.
118 // if some policy fails, that shouldn't go in the heartbeat.
119 // If no policies are running, then the policy list in the heartbeat can be empty
120 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
121 pdpStatusContext.setPolicies(apexEngineHandler.getRunningPolicies());
123 pdpStatusContext.setPolicies(Collections.emptyList());
126 if (null == pdpResponseDetails) {
127 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
128 PdpResponseStatus.SUCCESS, "Pdp update successful.");
130 return pdpResponseDetails;
134 * Method to start or stop apex engine based on the list of policies received from pap. When current state is
135 * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies,
136 * stop the current running policies and the deploy the new ones.
138 * @param pdpUpdateMsg the pdp update message from pap
139 * @param pdpMessageHandler pdp message handler
140 * @return pdpResponseDetails the pdp response
142 private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
143 final PdpMessageHandler pdpMessageHandler) {
144 PdpResponseDetails pdpResponseDetails = null;
145 ApexEngineHandler apexEngineHandler = null;
147 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
148 } catch (final IllegalArgumentException e) {
149 LOGGER.debug("ApenEngineHandler not in registry.", e);
151 if (null != apexEngineHandler
152 && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies())
153 && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
154 pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
156 pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
158 return pdpResponseDetails;
161 private PdpResponseDetails stopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
162 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
163 PdpResponseDetails pdpResponseDetails = null;
164 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
165 final var runningPolicies = apexEngineHandler.getRunningPolicies();
167 apexEngineHandler.shutdown();
168 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
169 PdpResponseStatus.SUCCESS, "Pdp update successful. No policies are running.");
170 } catch (final ApexStarterException e) {
171 LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
172 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
173 PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
175 updateDeploymentCounts(runningPolicies, pdpUpdateMsg);
177 return pdpResponseDetails;
180 private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
181 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
182 PdpResponseDetails pdpResponseDetails = null;
183 List<ToscaConceptIdentifier> runningPolicies = null;
185 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
186 apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
187 pdpUpdateMsg.getPoliciesToBeUndeployed());
188 runningPolicies = apexEngineHandler.getRunningPolicies();
190 apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
191 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
193 if (apexEngineHandler.isApexEngineRunning()) {
195 populateResponseForEngineInitiation(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
196 runningPolicies = apexEngineHandler.getRunningPolicies();
198 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
199 PdpResponseStatus.FAIL, "Apex engine failed to start.");
201 } catch (final ApexStarterException e) {
202 LOGGER.error("Apex engine service running failed. ", e);
203 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
204 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
206 updateDeploymentCounts(runningPolicies, pdpUpdateMsg);
207 return pdpResponseDetails;
210 private PdpResponseDetails populateResponseForEngineInitiation(final PdpUpdate pdpUpdateMsg,
211 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
212 PdpResponseDetails pdpResponseDetails;
213 Set<ToscaConceptIdentifier> runningPolicies = new HashSet<>(apexEngineHandler.getRunningPolicies());
214 List<ToscaConceptIdentifier> policiesToBeDeployed =
215 pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed());
216 List<ToscaConceptIdentifier> policiesToBeUndeployed = pdpUpdateMsg.getPoliciesToBeUndeployed();
217 if (runningPolicies.containsAll(policiesToBeDeployed)
218 && !containsAny(runningPolicies, policiesToBeUndeployed)) {
219 var message = new StringBuilder("Apex engine started. ");
220 if (!policiesToBeDeployed.isEmpty()) {
221 message.append("Deployed policies are: ");
222 for (ToscaConceptIdentifier policy : policiesToBeDeployed) {
223 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
226 if (!policiesToBeUndeployed.isEmpty()) {
227 message.append("Undeployed policies are: ");
228 for (ToscaConceptIdentifier policy : policiesToBeUndeployed) {
229 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
232 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
233 PdpResponseStatus.SUCCESS, message.toString());
236 new StringBuilder("Apex engine started. But, only the following polices are running - ");
237 for (ToscaConceptIdentifier policy : runningPolicies) {
238 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
240 message.append(". Other policies failed execution. Please see the logs for more details.");
241 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
242 PdpResponseStatus.SUCCESS, message.toString());
244 return pdpResponseDetails;
248 * Method checks if the Pdp update message is already handled by checking the values in the context.
250 * @param pdpUpdateMsg pdp update message received from pap
251 * @param pdpStatusContext values saved in context memory
252 * @return boolean flag which tells if the information is same or not
254 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
255 return null != pdpStatusContext.getPdpGroup()
256 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
257 && null != pdpStatusContext.getPdpSubgroup()
258 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
259 && null != pdpStatusContext.getPolicies()
260 && pdpStatusContext.getPolicies()
261 .containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()))
262 && !containsAny(new HashSet<>(pdpStatusContext.getPolicies()), pdpUpdateMsg.getPoliciesToBeUndeployed());
266 * Method to update the time interval used by the timer task.
268 * @param interval time interval received in the pdp update message from pap
270 public void updateInterval(final long interval) {
271 final var pdpStatusPublisher =
272 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
273 pdpStatusPublisher.terminate();
274 final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
275 Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
276 new PdpStatusPublisher(topicSinks, interval));
280 * Checks if one list contains any element of another.
282 * @param listToCheckWith list to check contents of
283 * @param listToCheckAgainst list to check against other list for similarities
284 * @return boolean flag which tells if lists share same elements or not
286 private boolean containsAny(Set<ToscaConceptIdentifier> listToCheckWith,
287 List<ToscaConceptIdentifier> listToCheckAgainst) {
288 return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
292 * Update count values for deployment actions (deploy and undeploy) when applicable.
293 * @param runningPolicies the policies running in apex engine
294 * @param pdpUpdateMsg the pdp update message from pap
296 private void updateDeploymentCounts(final List<ToscaConceptIdentifier> runningPolicies,
297 final PdpUpdate pdpUpdateMsg) {
298 final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
299 if (statisticsManager != null) {
300 if (pdpUpdateMsg.getPoliciesToBeDeployed() != null && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
301 var policiesToDeploy = pdpUpdateMsg.getPoliciesToBeDeployed().stream()
302 .map(ToscaWithTypeAndObjectProperties::getIdentifier).collect(Collectors.toList());
304 var policiesSuccessfullyDeployed = new ArrayList<>(policiesToDeploy);
305 policiesSuccessfullyDeployed.retainAll(runningPolicies);
306 policiesSuccessfullyDeployed.forEach(policy -> statisticsManager.updatePolicyDeployCounter(true));
308 var policiesFailedToDeploy = new ArrayList<>(policiesToDeploy);
309 policiesFailedToDeploy.removeIf(runningPolicies::contains);
310 policiesFailedToDeploy.forEach(policy -> statisticsManager.updatePolicyDeployCounter(false));
313 var policiesToUndeploy = pdpUpdateMsg.getPoliciesToBeUndeployed();
314 if (policiesToUndeploy != null && !policiesToUndeploy.isEmpty()) {
315 var policiesSuccessfullyUndeployed = new ArrayList<>(policiesToUndeploy);
316 policiesSuccessfullyUndeployed.retainAll(runningPolicies);
317 policiesSuccessfullyUndeployed.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(true));
319 var policiesFailedToUndeploy = new ArrayList<>(policiesToUndeploy);
320 policiesFailedToUndeploy.removeIf(runningPolicies::contains);
321 policiesFailedToUndeploy.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(false));