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 (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
55 pdpStatusContext.getPdpSubgroup())) {
56 switch (pdpStateChangeMsg.getState()) {
58 pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
61 pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
66 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
67 pdpStatus.setResponse(pdpResponseDetails);
68 pdpStatus.setDescription("Pdp status response message for PdpStateChange");
69 pdpStatusPublisher.send(pdpStatus);
74 * Method to handle when the new state from pap is active.
76 * @param pdpStateChangeMsg
77 * @param pdpStatusContext
78 * @param pdpMessageHandler
79 * @return pdpResponseDetails
81 private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
82 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
83 PdpResponseDetails pdpResponseDetails = null;
84 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
85 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
86 PdpResponseStatus.SUCCESS, "Pdp already in active state");
88 final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
89 if (policies.isEmpty()) {
90 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
91 PdpResponseStatus.SUCCESS, "No policies found. Apex engine not running.");
94 // assumed that the apex policies list contains only one entry.
95 final ApexEngineHandler apexEngineHandler =
96 new ApexEngineHandler((String) policies.get(0).getProperties().get("content"));
97 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
98 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
99 PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
100 pdpStatusContext.setState(PdpState.ACTIVE);
101 } catch (final ApexStarterException e) {
102 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
103 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
107 return pdpResponseDetails;
111 * Method to handle when the new state from pap is passive.
113 * @param pdpStateChangeMsg
114 * @param pdpStatusContext
115 * @param pdpMessageHandler
116 * @return pdpResponseDetails
118 private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
119 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
120 PdpResponseDetails pdpResponseDetails = null;
121 if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
122 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
123 PdpResponseStatus.SUCCESS, "Pdp already in passive state");
125 final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
127 apexEngineHandler.shutdown();
128 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
129 PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
130 pdpStatusContext.setState(PdpState.PASSIVE);
131 } catch (final Exception e) {
132 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
133 PdpResponseStatus.FAIL,
134 "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
137 return pdpResponseDetails;