0e7465d19e07b6029dfe95369540c765c2462f81
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.services.onappf.handler;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
30 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
31 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
32 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
33 import org.onap.policy.common.endpoints.event.comm.TopicSink;
34 import org.onap.policy.common.utils.services.Registry;
35 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
36 import org.onap.policy.models.pdp.concepts.PdpStatus;
37 import org.onap.policy.models.pdp.concepts.PdpUpdate;
38 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
39 import org.onap.policy.models.pdp.enums.PdpState;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * This class supports the handling of pdp update messages.
47  *
48  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
49  */
50 public class PdpUpdateMessageHandler {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
53
54     /**
55      * Method which handles a pdp update event from PAP.
56      *
57      * @param pdpUpdateMsg pdp update message
58      */
59     public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
60         final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
61         final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
62         PdpResponseDetails pdpResponseDetails = null;
63         if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
64                 pdpStatusContext.getPdpSubgroup())) {
65             if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
66                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
67                         PdpResponseStatus.SUCCESS, "Pdp already updated");
68             } else {
69                 pdpResponseDetails = handlePdpUpdate(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext);
70             }
71             final PdpStatusPublisher pdpStatusPublisherTemp =
72                     Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
73             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
74             pdpStatus.setResponse(pdpResponseDetails);
75             pdpStatus.setDescription("Pdp status response message for PdpUpdate");
76             pdpStatusPublisherTemp.send(pdpStatus);
77         }
78     }
79
80     /**
81      * Method to do pdp update.
82      *
83      * @param pdpUpdateMsg the pdp update message
84      * @param pdpMessageHandler the message handler
85      * @param pdpStatusContext the pdp status in memory
86      * @return pdpResponseDetails the pdp response
87      */
88     private PdpResponseDetails handlePdpUpdate(final PdpUpdate pdpUpdateMsg, final PdpMessageHandler pdpMessageHandler,
89         final PdpStatus pdpStatusContext) {
90         PdpResponseDetails pdpResponseDetails = null;
91         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
92         if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
93                 && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
94             updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
95         }
96         pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
97         pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
98         @SuppressWarnings("unchecked")
99         List<ToscaPolicy> policies = Registry.getOrDefault(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
100                 List.class, new ArrayList<>());
101         policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed());
102         pdpStatusContext.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier)
103                 .collect(Collectors.toList()));
104         Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
105                 policies);
106         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
107             pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
108
109             ApexEngineHandler apexEngineHandler =
110                 Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null);
111             // in hearbeat while in active state, only the policies which are running should be there.
112             // if some policy fails, that shouldn't go in the heartbeat.
113             // If no policies are running, then the policy list in the heartbeat can be empty
114             if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
115                 pdpStatusContext.setPolicies(apexEngineHandler.getRunningPolicies());
116             } else {
117                 pdpStatusContext.setPolicies(Collections.emptyList());
118             }
119         }
120         if (null == pdpResponseDetails) {
121             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
122                     PdpResponseStatus.SUCCESS, "Pdp update successful.");
123         }
124         return pdpResponseDetails;
125     }
126
127     /**
128      * Method to start or stop apex engine based on the list of policies received from pap. When current state is
129      * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies,
130      * stop the current running policies and the deploy the new ones.
131      *
132      * @param pdpUpdateMsg the pdp update message from pap
133      * @param pdpMessageHandler pdp message handler
134      * @param pdpStatusContext the pdp status object in memory
135      * @return pdpResponseDetails the pdp response
136      */
137     private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
138             final PdpMessageHandler pdpMessageHandler) {
139         PdpResponseDetails pdpResponseDetails = null;
140         ApexEngineHandler apexEngineHandler = null;
141         try {
142             apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
143         } catch (final IllegalArgumentException e) {
144             LOGGER.debug("ApenEngineHandler not in registry.", e);
145         }
146         if (null != apexEngineHandler
147             && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies())
148             && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
149             pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
150         } else {
151             pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
152         }
153         return pdpResponseDetails;
154     }
155
156     private PdpResponseDetails stopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
157         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
158         PdpResponseDetails pdpResponseDetails = null;
159         if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
160             try {
161                 apexEngineHandler.shutdown();
162                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
163                     PdpResponseStatus.SUCCESS, "Pdp update successful. No policies are running.");
164             } catch (final ApexStarterException e) {
165                 LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
166                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
167                         PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
168             }
169         }
170         return pdpResponseDetails;
171     }
172
173     private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
174         final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
175         PdpResponseDetails pdpResponseDetails = null;
176
177         try {
178             if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
179                 apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
180                         pdpUpdateMsg.getPoliciesToBeUndeployed());
181             } else {
182                 apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
183                 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
184             }
185             if (apexEngineHandler.isApexEngineRunning()) {
186                 List<ToscaConceptIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
187                 if (new HashSet<>(runningPolicies).containsAll(new HashSet<>(pdpMessageHandler
188                         .getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed())))
189                         && !containsAny(runningPolicies, pdpUpdateMsg.getPoliciesToBeUndeployed())) {
190                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
191                         PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
192                 } else {
193                     StringBuilder message =
194                         new StringBuilder("Apex engine started. But, only the following polices are running - ");
195                     for (ToscaConceptIdentifier policy : runningPolicies) {
196                         message.append(policy.getName()).append(":").append(policy.getVersion()).append("  ");
197                     }
198                     message.append(". Other policies failed execution. Please see the logs for more details.");
199                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
200                         PdpResponseStatus.SUCCESS, message.toString());
201                 }
202             } else {
203                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
204                     PdpResponseStatus.FAIL, "Apex engine failed to start.");
205             }
206         } catch (final ApexStarterException e) {
207             LOGGER.error("Apex engine service running failed. ", e);
208             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
209                     PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
210         }
211         final ApexPolicyStatisticsManager apexPolicyStatisticsManager =
212                 ApexPolicyStatisticsManager.getInstanceFromRegistry();
213         if (apexPolicyStatisticsManager != null) {
214             apexPolicyStatisticsManager
215                     .updatePolicyDeployCounter(pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
216         }
217         return pdpResponseDetails;
218     }
219
220     /**
221      * Method checks if the Pdp update message is already handled by checking the values in the context.
222      *
223      * @param pdpUpdateMsg pdp update message received from pap
224      * @param pdpStatusContext values saved in context memory
225      * @return boolean flag which tells if the information is same or not
226      */
227     private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
228         return null != pdpStatusContext.getPdpGroup()
229                 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
230                 && null != pdpStatusContext.getPdpSubgroup()
231                 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
232                 && null != pdpStatusContext.getPolicies()
233                 && pdpStatusContext.getPolicies().containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(
234                         pdpUpdateMsg.getPoliciesToBeDeployed()))
235                 && !containsAny(pdpStatusContext.getPolicies(), pdpUpdateMsg.getPoliciesToBeUndeployed());
236     }
237
238     /**
239      * Method to update the time interval used by the timer task.
240      *
241      * @param interval time interval received in the pdp update message from pap
242      */
243     public void updateInterval(final long interval) {
244         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
245         pdpStatusPublisher.terminate();
246         final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
247         Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
248                 new PdpStatusPublisher(topicSinks, interval));
249     }
250
251     /**
252      * Checks if one list contains any element of another.
253      *
254      * @param listToCheckWith list to check contents of
255      * @param listToCheckAgainst list to check against other list for similarities
256      * @return boolean flag which tells if lists share same elements or not
257      */
258     private boolean containsAny(List<ToscaConceptIdentifier> listToCheckWith,
259             List<ToscaConceptIdentifier> listToCheckAgainst) {
260         return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
261     }
262 }