2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.services.onappf.ApexStarterConstants;
26 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
27 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
30 import org.onap.policy.models.pdp.concepts.PdpStateChange;
31 import org.onap.policy.models.pdp.concepts.PdpStatus;
32 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * This class supports the handling of pdp state change messages.
42 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
44 public class PdpStateChangeMessageHandler {
46 private static final Logger LOGGER = LoggerFactory.getLogger(PdpStateChangeMessageHandler.class);
49 * Method which handles a pdp state change event from PAP.
51 * @param pdpStateChangeMsg pdp state change message
53 public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
54 final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
55 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
56 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
57 PdpResponseDetails pdpResponseDetails = null;
58 if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
59 pdpStatusContext.getPdpSubgroup())) {
60 switch (pdpStateChangeMsg.getState()) {
62 pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
65 pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
70 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
71 pdpStatus.setResponse(pdpResponseDetails);
72 pdpStatus.setDescription("Pdp status response message for PdpStateChange");
73 pdpStatusPublisher.send(pdpStatus);
78 * Method to handle when the new state from pap is active.
80 * @param pdpStateChangeMsg pdp state change message
81 * @param pdpStatusContext pdp status object in memory
82 * @param pdpMessageHandler the pdp message handler
83 * @return pdpResponseDetails pdp response
85 private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
86 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
87 PdpResponseDetails pdpResponseDetails = null;
88 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
89 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
90 PdpResponseStatus.SUCCESS, "Pdp already in active state");
92 final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
93 if (policies.isEmpty()) {
94 pdpStatusContext.setState(PdpState.ACTIVE);
95 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
96 PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
98 pdpResponseDetails = startApexEngine(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler, policies);
101 return pdpResponseDetails;
105 * Method to start apex engine.
107 * @param pdpStateChangeMsg pdp state change message
108 * @param pdpStatusContext pdp status in memory
109 * @param pdpMessageHandler the pdp message handler
110 * @param policies list of policies
111 * @return pdp response details
113 private PdpResponseDetails startApexEngine(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext,
114 final PdpMessageHandler pdpMessageHandler, final List<ToscaPolicy> policies) {
115 PdpResponseDetails pdpResponseDetails;
117 final ApexEngineHandler apexEngineHandler = new ApexEngineHandler(policies);
118 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
119 if (apexEngineHandler.isApexEngineRunning()) {
120 List<ToscaPolicyIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
121 // only the policies which are succesfully executed should be there in the heartbeat
122 pdpStatusContext.setPolicies(runningPolicies);
123 if (new HashSet<>(runningPolicies)
124 .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(policies)))) {
126 pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
127 PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
129 StringBuilder message = new StringBuilder(
130 "Apex engine started. But, only the following polices are running - ");
131 for (ToscaPolicyIdentifier policy : runningPolicies) {
132 message.append(policy.getName()).append(":").append(policy.getVersion()).append(" ");
134 message.append(". Other policies failed execution. Please see the logs for more details.");
135 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(
136 pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, message.toString());
138 pdpStatusContext.setState(PdpState.ACTIVE);
140 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
141 PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active.");
143 } catch (final ApexStarterException e) {
144 LOGGER.error("Pdp State Change failed.", e);
145 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
146 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
148 return pdpResponseDetails;
152 * Method to handle when the new state from pap is passive.
154 * @param pdpStateChangeMsg pdp state change message
155 * @param pdpStatusContext pdp status object in memory
156 * @param pdpMessageHandler the pdp message handler
157 * @return pdpResponseDetails pdp response
159 private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
160 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
161 PdpResponseDetails pdpResponseDetails = null;
162 if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
163 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
164 PdpResponseStatus.SUCCESS, "Pdp already in passive state");
166 ApexEngineHandler apexEngineHandler = null;
168 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
169 } catch (final IllegalArgumentException e) {
170 LOGGER.debug("ApenEngineHandler not in registry.", e);
173 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
174 apexEngineHandler.shutdown();
176 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
177 PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
178 pdpStatusContext.setState(PdpState.PASSIVE);
179 } catch (final Exception e) {
180 LOGGER.error("Stopping apex engine failed. State cannot be changed to Passive.", e);
181 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
182 PdpResponseStatus.FAIL,
183 "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
186 return pdpResponseDetails;