24ebc14dea2c4aec553d569601ac2edb59be1bb2
[policy/apex-pdp.git] /
1 /*-
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
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.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * This class supports the handling of pdp update messages.
49  *
50  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
51  */
52 public class PdpUpdateMessageHandler {
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
55
56     /**
57      * Method which handles a pdp update event from PAP.
58      *
59      * @param pdpUpdateMsg pdp update message
60      */
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");
70             } else {
71                 pdpResponseDetails = handlePdpUpdate(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext);
72             }
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);
79         }
80     }
81
82     /**
83      * Method to do pdp update.
84      *
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
89      */
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());
98         }
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,
110                 policies);
111         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
112             pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
113
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());
121             } else {
122                 pdpStatusContext.setPolicies(Collections.emptyList());
123             }
124         }
125         if (null == pdpResponseDetails) {
126             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
127                     PdpResponseStatus.SUCCESS, "Pdp update successful.");
128         }
129         return pdpResponseDetails;
130     }
131
132     /**
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.
136      *
137      * @param pdpUpdateMsg the pdp update message from pap
138      * @param pdpMessageHandler pdp message handler
139      * @return pdpResponseDetails the pdp response
140      */
141     private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
142             final PdpMessageHandler pdpMessageHandler) {
143         PdpResponseDetails pdpResponseDetails = null;
144         ApexEngineHandler apexEngineHandler = null;
145         try {
146             apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
147         } catch (final IllegalArgumentException e) {
148             LOGGER.debug("ApenEngineHandler not in registry.", e);
149         }
150         if (null != apexEngineHandler
151             && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies())
152             && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
153             pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
154         } else {
155             pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
156         }
157         return pdpResponseDetails;
158     }
159
160     private PdpResponseDetails stopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
161         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
162         PdpResponseDetails pdpResponseDetails = null;
163         if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
164             try {
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.");
172             }
173             updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
174         }
175         return pdpResponseDetails;
176     }
177
178     private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
179         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
180         PdpResponseDetails pdpResponseDetails = null;
181
182         try {
183             if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
184                 apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
185                     pdpUpdateMsg.getPoliciesToBeUndeployed());
186             } else {
187                 apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
188                 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
189             }
190             if (apexEngineHandler.isApexEngineRunning()) {
191                 pdpResponseDetails =
192                     populateResponseForEngineInitiation(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
193             } else {
194                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
195                     PdpResponseStatus.FAIL, "Apex engine failed to start.");
196             }
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());
201         }
202         updateDeploymentCounts(pdpUpdateMsg, pdpResponseDetails);
203         return pdpResponseDetails;
204     }
205
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("  ");
220                 }
221             }
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("  ");
226                 }
227             }
228             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
229                 PdpResponseStatus.SUCCESS, message.toString());
230         } else {
231             var message =
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("  ");
235             }
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());
239         }
240         return pdpResponseDetails;
241     }
242
243     /**
244      * Method checks if the Pdp update message is already handled by checking the values in the context.
245      *
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
249      */
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());
259     }
260
261     /**
262      * Method to update the time interval used by the timer task.
263      *
264      * @param interval time interval received in the pdp update message from pap
265      */
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));
273     }
274
275     /**
276      * Checks if one list contains any element of another.
277      *
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
281      */
282     private boolean containsAny(Set<ToscaConceptIdentifier> listToCheckWith,
283             List<ToscaConceptIdentifier> listToCheckAgainst) {
284         return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
285     }
286
287     /**
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
291      */
292     private void updateDeploymentCounts(final PdpUpdate pdpUpdateMsg, PdpResponseDetails pdpResponseDetails) {
293         final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
294
295         if (statisticsManager != null) {
296             if (pdpUpdateMsg.getPoliciesToBeDeployed() != null && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
297                 statisticsManager.updatePolicyDeployCounter(
298                         pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
299             }
300
301             if (pdpUpdateMsg.getPoliciesToBeUndeployed() != null
302                     && !pdpUpdateMsg.getPoliciesToBeUndeployed().isEmpty()) {
303                 statisticsManager.updatePolicyUndeployCounter(
304                         pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
305             }
306         }
307     }
308 }