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.starter.handler;
23 import java.util.List;
25 import org.onap.policy.apex.starter.ApexStarterConstants;
26 import org.onap.policy.apex.starter.comm.PdpStatusPublisher;
27 import org.onap.policy.apex.starter.engine.ApexEngineHandler;
28 import org.onap.policy.apex.starter.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;
38 * This class supports the handling of pdp state change messages.
40 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
42 public class PdpStateChangeMessageHandler {
45 * Method which handles a pdp state change event from PAP.
47 * @param pdpStateChangeMsg pdp state change message
49 public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
50 final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
51 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
52 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
53 PdpResponseDetails pdpResponseDetails = null;
54 if (isMessageRelevant(pdpStateChangeMsg, pdpStatusContext)) {
55 switch (pdpStateChangeMsg.getState()) {
57 pdpResponseDetails = handleTerminatedState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
60 pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
63 pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
68 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
69 pdpStatus.setResponse(pdpResponseDetails);
70 pdpStatusPublisher.send(pdpStatus);
75 * Check if the pdp state change message is meant for this pdp.
77 * @param pdpStateChangeMsg
78 * @param pdpStatusContext
79 * @return boolean value if relevant or not
81 private boolean isMessageRelevant(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext) {
82 return pdpStatusContext.getName().equals(pdpStateChangeMsg.getName()) || (null == pdpStateChangeMsg.getName()
83 && pdpStateChangeMsg.getPdpGroup().equals(pdpStatusContext.getPdpGroup())
84 && pdpStateChangeMsg.getPdpSubgroup().equals(pdpStatusContext.getPdpSubgroup()));
88 * Method to handle when the new state from pap is active.
90 * @param pdpStateChangeMsg
91 * @param pdpStatusContext
92 * @param pdpMessageHandler
93 * @return pdpResponseDetails
95 private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
96 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
97 PdpResponseDetails pdpResponseDetails = null;
98 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
99 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
100 PdpResponseStatus.SUCCESS, "Pdp already in active state");
102 final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
103 if (policies.isEmpty()) {
104 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
105 PdpResponseStatus.SUCCESS, "No policies found. Apex engine not running.");
108 // assumed that the apex policies list contains only one entry.
109 final ApexEngineHandler apexEngineHandler =
110 new ApexEngineHandler((String) policies.get(0).getProperties().get("content"));
111 Registry.register(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
112 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
113 PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
114 pdpStatusContext.setState(PdpState.ACTIVE);
115 } catch (final ApexStarterException e) {
116 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
117 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
121 return pdpResponseDetails;
125 * Method to handle when the new state from pap is passive.
127 * @param pdpStateChangeMsg
128 * @param pdpStatusContext
129 * @param pdpMessageHandler
130 * @return pdpResponseDetails
132 private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
133 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
134 PdpResponseDetails pdpResponseDetails = null;
135 if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
136 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
137 PdpResponseStatus.SUCCESS, "Pdp already in passive state");
139 final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
141 apexEngineHandler.shutdown();
142 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
143 PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
144 pdpStatusContext.setState(PdpState.PASSIVE);
145 } catch (final Exception e) {
146 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
147 PdpResponseStatus.FAIL,
148 "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
151 return pdpResponseDetails;
155 * Method to handle when the new state from pap is terminated.
157 * @param pdpStateChangeMsg
158 * @param pdpStatusPublisher
159 * @param pdpMessageHandler
160 * @return pdpResponseDetails
162 private PdpResponseDetails handleTerminatedState(final PdpStateChange pdpStateChangeMsg,
163 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
165 PdpResponseDetails pdpResponseDetails;
166 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
167 final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
169 apexEngineHandler.shutdown();
170 pdpStatusContext.setState(PdpState.PASSIVE);
171 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
172 PdpResponseStatus.SUCCESS, "Apex Engine stopped. State changed to passive.");
173 } catch (final Exception e) {
174 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
175 PdpResponseStatus.FAIL,
176 "Stopping apex engine failed. State cannot be changed." + e.getMessage());
179 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
180 PdpResponseStatus.SUCCESS, "Pdp already in passive state");
182 return pdpResponseDetails;