4d3de32931aac04c8f5d2e695799a64ddae2430c
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021, 2024 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2022 Bell Canada. 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.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.utils.services.Registry;
34 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
35 import org.onap.policy.models.pdp.concepts.PdpStateChange;
36 import org.onap.policy.models.pdp.concepts.PdpStatus;
37 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
38 import org.onap.policy.models.pdp.enums.PdpState;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * This class supports the handling of pdp state change messages.
47  *
48  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
49  */
50 public class PdpStateChangeMessageHandler {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(PdpStateChangeMessageHandler.class);
53
54     /**
55      * Method which handles a pdp state change event from PAP.
56      *
57      * @param pdpStateChangeMsg pdp state change message
58      */
59     public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
60         final var pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
61         final var pdpStatusPublisher =
62                         Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
63         final var pdpMessageHandler = new PdpMessageHandler();
64         PdpResponseDetails pdpResponseDetails = null;
65         if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
66                 pdpStatusContext.getPdpSubgroup())) {
67             switch (pdpStateChangeMsg.getState()) {
68                 case PASSIVE:
69                     pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
70                     break;
71                 case ACTIVE:
72                     pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
73                     break;
74                 default:
75                     break;
76             }
77             final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
78             pdpStatus.setResponse(pdpResponseDetails);
79             pdpStatus.setDescription("Pdp status response message for PdpStateChange");
80             pdpStatusPublisher.send(pdpStatus);
81         }
82     }
83
84     /**
85      * Method to handle when the new state from pap is active.
86      *
87      * @param pdpStateChangeMsg pdp state change message
88      * @param pdpStatusContext pdp status object in memory
89      * @param pdpMessageHandler the pdp message handler
90      * @return pdpResponseDetails pdp response
91      */
92     private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
93             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
94         PdpResponseDetails pdpResponseDetails = null;
95         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
96             pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
97                     PdpResponseStatus.SUCCESS, "Pdp already in active state");
98         } else {
99             final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
100             if (policies.isEmpty()) {
101                 pdpStatusContext.setState(PdpState.ACTIVE);
102                 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
103                         PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
104             } else {
105                 pdpResponseDetails = startApexEngine(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler, policies);
106             }
107         }
108         return pdpResponseDetails;
109     }
110
111     /**
112      * Method to start apex engine.
113      *
114      * @param pdpStateChangeMsg pdp state change message
115      * @param pdpStatusContext pdp status in memory
116      * @param pdpMessageHandler the pdp message handler
117      * @param policies list of policies
118      * @return pdp response details
119      */
120     private PdpResponseDetails startApexEngine(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext,
121         final PdpMessageHandler pdpMessageHandler, final List<ToscaPolicy> policies) {
122         PdpResponseDetails pdpResponseDetails;
123         try {
124             final var apexEngineHandler = new ApexEngineHandler(policies);
125             Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
126             if (apexEngineHandler.isApexEngineRunning()) {
127                 List<ToscaConceptIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
128                 // only the policies which are succesfully executed should be there in the heartbeat
129                 pdpStatusContext.setPolicies(runningPolicies);
130                 if (new HashSet<>(runningPolicies)
131                     .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(policies)))) {
132                     pdpResponseDetails =
133                         pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
134                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
135                 } else {
136                     var message = new StringBuilder(
137                         "Apex engine started. But, only the following polices are running - ");
138                     for (ToscaConceptIdentifier policy : runningPolicies) {
139                         message.append(policy.getName()).append(":").append(policy.getVersion()).append("  ");
140                     }
141                     message.append(". Other policies failed execution. Please see the logs for more details.");
142                     pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(
143                         pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, message.toString());
144                 }
145                 pdpStatusContext.setState(PdpState.ACTIVE);
146                 updateDeploymentCounts(runningPolicies, policies);
147             } else {
148                 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
149                     PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active.");
150             }
151         } catch (final ApexStarterException e) {
152             LOGGER.error("Pdp State Change failed.", e);
153             pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
154                     PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
155         }
156         return pdpResponseDetails;
157     }
158
159     /**
160      * Method to handle when the new state from pap is passive.
161      *
162      * @param pdpStateChangeMsg pdp state change message
163      * @param pdpStatusContext pdp status object in memory
164      * @param pdpMessageHandler the pdp message handler
165      * @return pdpResponseDetails pdp response
166      */
167     private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
168             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
169         PdpResponseDetails pdpResponseDetails = null;
170         if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
171             pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
172                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
173         } else {
174             ApexEngineHandler apexEngineHandler = null;
175             try {
176                 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
177             } catch (final IllegalArgumentException e) {
178                 LOGGER.debug("ApenEngineHandler not in registry.", e);
179             }
180             try {
181                 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
182                     apexEngineHandler.shutdown();
183                 }
184                 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
185                         PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
186                 pdpStatusContext.setState(PdpState.PASSIVE);
187             } catch (final Exception e) {
188                 LOGGER.error("Stopping apex engine failed. State cannot be changed to Passive.", e);
189                 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
190                         PdpResponseStatus.FAIL,
191                         "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
192             }
193         }
194         return pdpResponseDetails;
195     }
196
197     /**
198      * Update count values for deployment on engine startup.
199      * @param runningPolicies the policies running in apex engine
200      * @param policies the list of policies to deploy as per PDP_STATE_CHANGE message from pap
201      */
202     private void updateDeploymentCounts(final List<ToscaConceptIdentifier> runningPolicies,
203         final List<ToscaPolicy> policies) {
204         final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
205         if (statisticsManager == null || policies == null || policies.isEmpty()) {
206             return;
207         }
208         var policiesToDeploy = policies.stream()
209             .map(ToscaWithTypeAndObjectProperties::getIdentifier).toList();
210
211         var policiesSuccessfullyDeployed = new ArrayList<>(policiesToDeploy);
212         policiesSuccessfullyDeployed.retainAll(runningPolicies);
213         policiesSuccessfullyDeployed.forEach(policy -> statisticsManager.updatePolicyDeployCounter(true));
214
215         var policiesFailedToDeploy =  new ArrayList<>(policiesToDeploy);
216         policiesFailedToDeploy.removeIf(runningPolicies::contains);
217         policiesFailedToDeploy.forEach(policy -> statisticsManager.updatePolicyDeployCounter(false));
218     }
219 }