2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.services.onappf.handler;
23 import java.util.HashSet;
24 import java.util.List;
25 import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
26 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
27 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
28 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
29 import org.onap.policy.common.utils.services.Registry;
30 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
31 import org.onap.policy.models.pdp.concepts.PdpStateChange;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
34 import org.onap.policy.models.pdp.enums.PdpState;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * This class supports the handling of pdp state change messages.
43 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
45 public class PdpStateChangeMessageHandler {
47 private static final Logger LOGGER = LoggerFactory.getLogger(PdpStateChangeMessageHandler.class);
50 * Method which handles a pdp state change event from PAP.
52 * @param pdpStateChangeMsg pdp state change message
54 public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
55 final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
56 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
57 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
58 PdpResponseDetails pdpResponseDetails = null;
59 if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
60 pdpStatusContext.getPdpSubgroup())) {
61 switch (pdpStateChangeMsg.getState()) {
63 pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
66 pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
71 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
72 pdpStatus.setResponse(pdpResponseDetails);
73 pdpStatus.setDescription("Pdp status response message for PdpStateChange");
74 pdpStatusPublisher.send(pdpStatus);
79 * Method to handle when the new state from pap is active.
81 * @param pdpStateChangeMsg pdp state change message
82 * @param pdpStatusContext pdp status object in memory
83 * @param pdpMessageHandler the pdp message handler
84 * @return pdpResponseDetails pdp response
86 private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
87 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
88 PdpResponseDetails pdpResponseDetails = null;
89 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
90 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
91 PdpResponseStatus.SUCCESS, "Pdp already in active state");
93 final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
94 if (policies.isEmpty()) {
95 pdpStatusContext.setState(PdpState.ACTIVE);
96 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
97 PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
99 pdpResponseDetails = startApexEngine(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler, policies);
102 return pdpResponseDetails;
106 * Method to start apex engine.
108 * @param pdpStateChangeMsg pdp state change message
109 * @param pdpStatusContext pdp status in memory
110 * @param pdpMessageHandler the pdp message handler
111 * @param policies list of policies
112 * @return pdp response details
114 private PdpResponseDetails startApexEngine(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext,
115 final PdpMessageHandler pdpMessageHandler, final List<ToscaPolicy> policies) {
116 PdpResponseDetails pdpResponseDetails;
118 final ApexEngineHandler apexEngineHandler = new ApexEngineHandler(policies);
119 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
120 if (apexEngineHandler.isApexEngineRunning()) {
121 List<ToscaPolicyIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
122 // only the policies which are succesfully executed should be there in the heartbeat
123 pdpStatusContext.setPolicies(runningPolicies);
124 if (new HashSet<>(runningPolicies)
125 .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(policies)))) {
127 pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
128 PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
130 StringBuilder message = new StringBuilder(
131 "Apex engine started. But, only the following polices are running - ");
132 for (ToscaPolicyIdentifier policy : runningPolicies) {
133 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
135 message.append(". Other policies failed execution. Please see the logs for more details.");
136 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(
137 pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, message.toString());
139 pdpStatusContext.setState(PdpState.ACTIVE);
141 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
142 PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active.");
144 } catch (final ApexStarterException e) {
145 LOGGER.error("Pdp State Change failed.", e);
146 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
147 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
149 final ApexPolicyStatisticsManager apexPolicyStatisticsManager =
150 ApexPolicyStatisticsManager.getInstanceFromRegistry();
151 if (apexPolicyStatisticsManager != null) {
152 apexPolicyStatisticsManager
153 .updatePolicyDeployCounter(pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS);
155 return pdpResponseDetails;
159 * Method to handle when the new state from pap is passive.
161 * @param pdpStateChangeMsg pdp state change message
162 * @param pdpStatusContext pdp status object in memory
163 * @param pdpMessageHandler the pdp message handler
164 * @return pdpResponseDetails pdp response
166 private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
167 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
168 PdpResponseDetails pdpResponseDetails = null;
169 if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
170 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
171 PdpResponseStatus.SUCCESS, "Pdp already in passive state");
173 ApexEngineHandler apexEngineHandler = null;
175 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
176 } catch (final IllegalArgumentException e) {
177 LOGGER.debug("ApenEngineHandler not in registry.", e);
180 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
181 apexEngineHandler.shutdown();
183 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
184 PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
185 pdpStatusContext.setState(PdpState.PASSIVE);
186 } catch (final Exception e) {
187 LOGGER.error("Stopping apex engine failed. State cannot be changed to Passive.", e);
188 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
189 PdpResponseStatus.FAIL,
190 "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
193 return pdpResponseDetails;