1bcb8970bf8b210b404c2ac10431c7942751b8db
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.services.onappf.handler;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
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;
47
48 /**
49  * This class supports the handling of pdp update messages.
50  *
51  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
52  */
53 public class PdpUpdateMessageHandler {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
56
57     /**
58      * Method which handles a pdp update event from PAP.
59      *
60      * @param pdpUpdateMsg pdp update message
61      */
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");
71             } else {
72                 pdpResponseDetails = handlePdpUpdate(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext);
73             }
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);
80         }
81     }
82
83     /**
84      * Method to do pdp update.
85      *
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
90      */
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());
99         }
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,
111                 policies);
112         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
113             pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
114
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());
122             } else {
123                 pdpStatusContext.setPolicies(Collections.emptyList());
124             }
125         }
126         if (null == pdpResponseDetails) {
127             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
128                     PdpResponseStatus.SUCCESS, "Pdp update successful.");
129         }
130         return pdpResponseDetails;
131     }
132
133     /**
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.
137      *
138      * @param pdpUpdateMsg the pdp update message from pap
139      * @param pdpMessageHandler pdp message handler
140      * @return pdpResponseDetails the pdp response
141      */
142     private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
143             final PdpMessageHandler pdpMessageHandler) {
144         PdpResponseDetails pdpResponseDetails = null;
145         ApexEngineHandler apexEngineHandler = null;
146         try {
147             apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
148         } catch (final IllegalArgumentException e) {
149             LOGGER.debug("ApenEngineHandler not in registry.", e);
150         }
151         if (null != apexEngineHandler
152             && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies())
153             && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
154             pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
155         } else {
156             pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
157         }
158         return pdpResponseDetails;
159     }
160
161     private PdpResponseDetails stopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
162         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
163         PdpResponseDetails pdpResponseDetails = null;
164         if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
165             List<ToscaConceptIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
166             try {
167                 apexEngineHandler.shutdown();
168                 runningPolicies = apexEngineHandler.getRunningPolicies();
169                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
170                     PdpResponseStatus.SUCCESS, "Pdp update successful. No policies are running.");
171             } catch (final ApexStarterException e) {
172                 LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
173                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
174                         PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
175             }
176             updateDeploymentCounts(runningPolicies, pdpUpdateMsg);
177         }
178         return pdpResponseDetails;
179     }
180
181     private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
182         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
183         PdpResponseDetails pdpResponseDetails = null;
184         List<ToscaConceptIdentifier> runningPolicies = new ArrayList<>();
185         try {
186             if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
187                 apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
188                     pdpUpdateMsg.getPoliciesToBeUndeployed());
189                 runningPolicies = apexEngineHandler.getRunningPolicies();
190             } else {
191                 apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
192                 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
193             }
194             if (apexEngineHandler.isApexEngineRunning()) {
195                 pdpResponseDetails =
196                     populateResponseForEngineInitiation(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
197                 runningPolicies = apexEngineHandler.getRunningPolicies();
198             } else {
199                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
200                     PdpResponseStatus.FAIL, "Apex engine failed to start.");
201             }
202         } catch (final ApexStarterException e) {
203             LOGGER.error("Apex engine service running failed. ", e);
204             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
205                     PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
206         }
207         updateDeploymentCounts(runningPolicies, pdpUpdateMsg);
208         return pdpResponseDetails;
209     }
210
211     private PdpResponseDetails populateResponseForEngineInitiation(final PdpUpdate pdpUpdateMsg,
212         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
213         PdpResponseDetails pdpResponseDetails;
214         Set<ToscaConceptIdentifier> runningPolicies = new HashSet<>(apexEngineHandler.getRunningPolicies());
215         List<ToscaConceptIdentifier> policiesToBeDeployed =
216             pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed());
217         List<ToscaConceptIdentifier> policiesToBeUndeployed = pdpUpdateMsg.getPoliciesToBeUndeployed();
218         if (runningPolicies.containsAll(policiesToBeDeployed)
219             && !containsAny(runningPolicies, policiesToBeUndeployed)) {
220             var message = new StringBuilder("Apex engine started. ");
221             if (!policiesToBeDeployed.isEmpty()) {
222                 message.append("Deployed policies are: ");
223                 for (ToscaConceptIdentifier policy : policiesToBeDeployed) {
224                     message.append(policy.getName()).append(":").append(policy.getVersion()).append("  ");
225                 }
226             }
227             if (!policiesToBeUndeployed.isEmpty()) {
228                 message.append("Undeployed policies are: ");
229                 for (ToscaConceptIdentifier policy : policiesToBeUndeployed) {
230                     message.append(policy.getName()).append(":").append(policy.getVersion()).append("  ");
231                 }
232             }
233             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
234                 PdpResponseStatus.SUCCESS, message.toString());
235         } else {
236             var message =
237                 new StringBuilder("Apex engine started. But, only the following polices are running - ");
238             for (ToscaConceptIdentifier policy : runningPolicies) {
239                 message.append(policy.getName()).append(":").append(policy.getVersion()).append("  ");
240             }
241             message.append(". Other policies failed execution. Please see the logs for more details.");
242             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
243                 PdpResponseStatus.SUCCESS, message.toString());
244         }
245         return pdpResponseDetails;
246     }
247
248     /**
249      * Method checks if the Pdp update message is already handled by checking the values in the context.
250      *
251      * @param pdpUpdateMsg pdp update message received from pap
252      * @param pdpStatusContext values saved in context memory
253      * @return boolean flag which tells if the information is same or not
254      */
255     private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
256         return null != pdpStatusContext.getPdpGroup()
257             && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
258             && null != pdpStatusContext.getPdpSubgroup()
259             && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
260             && null != pdpStatusContext.getPolicies()
261             && pdpStatusContext.getPolicies()
262                 .containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()))
263             && !containsAny(new HashSet<>(pdpStatusContext.getPolicies()), pdpUpdateMsg.getPoliciesToBeUndeployed());
264     }
265
266     /**
267      * Method to update the time interval used by the timer task.
268      *
269      * @param interval time interval received in the pdp update message from pap
270      */
271     public void updateInterval(final long interval) {
272         final var pdpStatusPublisher =
273                         Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
274         pdpStatusPublisher.terminate();
275         final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
276         Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
277                 new PdpStatusPublisher(topicSinks, interval));
278     }
279
280     /**
281      * Checks if one list contains any element of another.
282      *
283      * @param listToCheckWith list to check contents of
284      * @param listToCheckAgainst list to check against other list for similarities
285      * @return boolean flag which tells if lists share same elements or not
286      */
287     private boolean containsAny(Set<ToscaConceptIdentifier> listToCheckWith,
288             List<ToscaConceptIdentifier> listToCheckAgainst) {
289         return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
290     }
291
292     /**
293      * Update count values for deployment actions (deploy and undeploy) when applicable.
294      * @param runningPolicies the policies running in apex engine
295      * @param pdpUpdateMsg the pdp update message from pap
296      */
297     private void updateDeploymentCounts(final List<ToscaConceptIdentifier> runningPolicies,
298         final PdpUpdate pdpUpdateMsg) {
299         final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
300         if (statisticsManager != null && runningPolicies != null) {
301             if (pdpUpdateMsg.getPoliciesToBeDeployed() != null && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
302                 var policiesToDeploy = pdpUpdateMsg.getPoliciesToBeDeployed().stream()
303                     .map(ToscaWithTypeAndObjectProperties::getIdentifier).collect(Collectors.toList());
304
305                 var policiesSuccessfullyDeployed = new ArrayList<>(policiesToDeploy);
306                 policiesSuccessfullyDeployed.retainAll(runningPolicies);
307                 policiesSuccessfullyDeployed.forEach(policy -> statisticsManager.updatePolicyDeployCounter(true));
308
309                 var policiesFailedToDeploy =  new ArrayList<>(policiesToDeploy);
310                 policiesFailedToDeploy.removeIf(runningPolicies::contains);
311                 policiesFailedToDeploy.forEach(policy -> statisticsManager.updatePolicyDeployCounter(false));
312             }
313
314             var policiesToUndeploy = pdpUpdateMsg.getPoliciesToBeUndeployed();
315             if (policiesToUndeploy != null && !policiesToUndeploy.isEmpty()) {
316                 var policiesFailedToUndeploy = new ArrayList<>(policiesToUndeploy);
317                 policiesFailedToUndeploy.retainAll(runningPolicies);
318                 policiesFailedToUndeploy.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(false));
319
320                 var policiesSuccessfullyUndeployed =  new ArrayList<>(policiesToUndeploy);
321                 policiesSuccessfullyUndeployed.removeIf(runningPolicies::contains);
322                 policiesSuccessfullyUndeployed.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(true));
323             }
324         }
325     }
326 }