2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 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.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * This class supports the handling of pdp update messages.
50 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
52 public class PdpUpdateMessageHandler {
54 private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
57 * Method which handles a pdp update event from PAP.
59 * @param pdpUpdateMsg pdp update message
61 public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
62 final var pdpMessageHandler = new PdpMessageHandler();
63 final var pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
64 PdpResponseDetails pdpResponseDetails = null;
65 if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
66 pdpStatusContext.getPdpSubgroup())) {
67 if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
68 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
69 PdpResponseStatus.SUCCESS, "Pdp already updated");
71 pdpResponseDetails = handlePdpUpdate(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext);
73 final var pdpStatusPublisherTemp =
74 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
75 final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
76 pdpStatus.setResponse(pdpResponseDetails);
77 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
78 pdpStatusPublisherTemp.send(pdpStatus);
83 * Method to do pdp update.
85 * @param pdpUpdateMsg the pdp update message
86 * @param pdpMessageHandler the message handler
87 * @param pdpStatusContext the pdp status in memory
88 * @return pdpResponseDetails the pdp response
90 private PdpResponseDetails handlePdpUpdate(final PdpUpdate pdpUpdateMsg, final PdpMessageHandler pdpMessageHandler,
91 final PdpStatus pdpStatusContext) {
92 PdpResponseDetails pdpResponseDetails = null;
93 final var pdpStatusPublisher =
94 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
95 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
96 && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
97 updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
99 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
100 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
101 @SuppressWarnings("unchecked")
102 List<ToscaPolicy> policies = Registry.getOrDefault(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
103 List.class, new ArrayList<>());
104 policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed());
105 Set<ToscaConceptIdentifier> policiesInDeployment = policies.stream().map(ToscaPolicy::getIdentifier)
106 .collect(Collectors.toSet());
107 policiesInDeployment.removeAll(pdpUpdateMsg.getPoliciesToBeUndeployed());
108 pdpStatusContext.setPolicies(new ArrayList<>(policiesInDeployment));
109 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
111 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
112 pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
114 var apexEngineHandler =
115 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
116 // in hearbeat while in active state, only the policies which are running should be there.
117 // if some policy fails, that shouldn't go in the heartbeat.
118 // If no policies are running, then the policy list in the heartbeat can be empty
119 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
120 pdpStatusContext.setPolicies(apexEngineHandler.getRunningPolicies());
122 pdpStatusContext.setPolicies(Collections.emptyList());
125 if (null == pdpResponseDetails) {
126 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
127 PdpResponseStatus.SUCCESS, "Pdp update successful.");
129 return pdpResponseDetails;
133 * Method to start or stop apex engine based on the list of policies received from pap. When current state is
134 * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies,
135 * stop the current running policies and the deploy the new ones.
137 * @param pdpUpdateMsg the pdp update message from pap
138 * @param pdpMessageHandler pdp message handler
139 * @return pdpResponseDetails the pdp response
141 private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
142 final PdpMessageHandler pdpMessageHandler) {
143 PdpResponseDetails pdpResponseDetails = null;
144 ApexEngineHandler apexEngineHandler = null;
146 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
147 } catch (final IllegalArgumentException e) {
148 LOGGER.debug("ApenEngineHandler not in registry.", e);
150 if (null != apexEngineHandler
151 && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies())
152 && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
153 pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
155 pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
157 return pdpResponseDetails;
160 private PdpResponseDetails stopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
161 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
162 PdpResponseDetails pdpResponseDetails = null;
163 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
165 apexEngineHandler.shutdown();
166 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
167 PdpResponseStatus.SUCCESS, "Pdp update successful. No policies are running.");
168 } catch (final ApexStarterException e) {
169 LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
170 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
171 PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
173 updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
175 return pdpResponseDetails;
178 private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
179 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
180 PdpResponseDetails pdpResponseDetails = null;
183 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
184 apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
185 pdpUpdateMsg.getPoliciesToBeUndeployed());
187 apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
188 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
190 if (apexEngineHandler.isApexEngineRunning()) {
192 populateResponseForEngineInitiation(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
194 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
195 PdpResponseStatus.FAIL, "Apex engine failed to start.");
197 } catch (final ApexStarterException e) {
198 LOGGER.error("Apex engine service running failed. ", e);
199 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
200 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
202 updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
203 return pdpResponseDetails;
206 private PdpResponseDetails populateResponseForEngineInitiation(final PdpUpdate pdpUpdateMsg,
207 final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
208 PdpResponseDetails pdpResponseDetails;
209 Set<ToscaConceptIdentifier> runningPolicies = new HashSet<>(apexEngineHandler.getRunningPolicies());
210 List<ToscaConceptIdentifier> policiesToBeDeployed =
211 pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed());
212 List<ToscaConceptIdentifier> policiesToBeUndeployed = pdpUpdateMsg.getPoliciesToBeUndeployed();
213 if (runningPolicies.containsAll(policiesToBeDeployed)
214 && !containsAny(runningPolicies, policiesToBeUndeployed)) {
215 var message = new StringBuilder("Apex engine started. ");
216 if (!policiesToBeDeployed.isEmpty()) {
217 message.append("Deployed policies are: ");
218 for (ToscaConceptIdentifier policy : policiesToBeDeployed) {
219 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
222 if (!policiesToBeUndeployed.isEmpty()) {
223 message.append("Undeployed policies are: ");
224 for (ToscaConceptIdentifier policy : policiesToBeUndeployed) {
225 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
228 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
229 PdpResponseStatus.SUCCESS, message.toString());
232 new StringBuilder("Apex engine started. But, only the following polices are running - ");
233 for (ToscaConceptIdentifier policy : runningPolicies) {
234 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
236 message.append(". Other policies failed execution. Please see the logs for more details.");
237 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
238 PdpResponseStatus.SUCCESS, message.toString());
240 return pdpResponseDetails;
244 * Method checks if the Pdp update message is already handled by checking the values in the context.
246 * @param pdpUpdateMsg pdp update message received from pap
247 * @param pdpStatusContext values saved in context memory
248 * @return boolean flag which tells if the information is same or not
250 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
251 return null != pdpStatusContext.getPdpGroup()
252 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
253 && null != pdpStatusContext.getPdpSubgroup()
254 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
255 && null != pdpStatusContext.getPolicies()
256 && pdpStatusContext.getPolicies()
257 .containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()))
258 && !containsAny(new HashSet<>(pdpStatusContext.getPolicies()), pdpUpdateMsg.getPoliciesToBeUndeployed());
262 * Method to update the time interval used by the timer task.
264 * @param interval time interval received in the pdp update message from pap
266 public void updateInterval(final long interval) {
267 final var pdpStatusPublisher =
268 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
269 pdpStatusPublisher.terminate();
270 final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
271 Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
272 new PdpStatusPublisher(topicSinks, interval));
276 * Checks if one list contains any element of another.
278 * @param listToCheckWith list to check contents of
279 * @param listToCheckAgainst list to check against other list for similarities
280 * @return boolean flag which tells if lists share same elements or not
282 private boolean containsAny(Set<ToscaConceptIdentifier> listToCheckWith,
283 List<ToscaConceptIdentifier> listToCheckAgainst) {
284 return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
288 * Update count values for deployment actions (deploy and undeploy) when applicable.
289 * @param pdpUpdateMsg the pdp update message from pap
290 * @param pdpResponseDetails the pdp response
292 private void updateDeploymentCounts(final PdpUpdate pdpUpdateMsg, PdpResponseDetails pdpResponseDetails) {
293 final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
295 if (statisticsManager != null) {
296 if (pdpUpdateMsg.getPoliciesToBeDeployed() != null && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
297 statisticsManager.updatePolicyDeployCounter(
298 pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
301 if (pdpUpdateMsg.getPoliciesToBeUndeployed() != null
302 && !pdpUpdateMsg.getPoliciesToBeUndeployed().isEmpty()) {
303 statisticsManager.updatePolicyUndeployCounter(
304 pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);