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.endpoints.event.comm.TopicSink;
30 import org.onap.policy.common.utils.services.Registry;
31 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.onap.policy.models.pdp.concepts.PdpUpdate;
34 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
35 import org.onap.policy.models.pdp.enums.PdpState;
38 * This class supports the handling of pdp update messages.
40 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
42 public class PdpUpdateMessageHandler {
45 * Method which handles a pdp update event from PAP.
47 * @param pdpUpdateMsg pdp update message
49 public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
50 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
51 final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
52 PdpResponseDetails pdpResponseDetails = null;
53 if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
54 pdpStatusContext.getPdpSubgroup())) {
55 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
56 if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext, pdpStatusPublisher.getInterval())) {
57 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
58 PdpResponseStatus.SUCCESS, "Pdp already updated");
60 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
61 && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
62 updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
64 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
65 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
67 .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
68 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
70 startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext);
72 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
73 if (null == pdpResponseDetails) {
74 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
75 PdpResponseStatus.SUCCESS, "Pdp update successful.");
78 final PdpStatusPublisher pdpStatusPublisherTemp =
79 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
80 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
81 pdpStatus.setResponse(pdpResponseDetails);
82 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
83 pdpStatusPublisherTemp.send(pdpStatus);
88 * Method to start or stop apex engine based on the list of policies received from pap. When current state is
89 * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies,
90 * stop the current running policies and the deploy the new ones.
93 * @param pdpMessageHandler
94 * @param pdpStatusContext
95 * @return pdpResponseDetails
97 private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
98 final PdpMessageHandler pdpMessageHandler, final PdpStatus pdpStatusContext) {
99 PdpResponseDetails pdpResponseDetails = null;
100 if (pdpUpdateMsg.getPolicies().isEmpty()) {
101 final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
102 if (apexEngineHandler.isApexEngineRunning()) {
104 apexEngineHandler.shutdown();
105 } catch (final ApexStarterException e) {
106 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
107 PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
112 ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
113 if (apexEngineHandler.isApexEngineRunning()) {
114 apexEngineHandler.shutdown();
116 apexEngineHandler = new ApexEngineHandler(
117 (String) pdpUpdateMsg.getPolicies().get(0).getProperties().get("content"));
118 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
119 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
120 PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
121 } catch (final ApexStarterException e) {
122 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
123 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
126 return pdpResponseDetails;
130 * Method checks of the Pdp update message is already handled by checking the values in the context.
132 * @param pdpUpdateMsg pdp update message received from pap
133 * @param pdpStatusContext values saved in context memory
134 * @param interval the current interval in which the pdp status publisher is sending the heartbeats
135 * @return boolean flag which tells if the information is same or not
137 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext,
138 final long interval) {
139 return null != pdpStatusContext.getPdpGroup()
140 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
141 && null != pdpStatusContext.getPdpSubgroup()
142 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
143 && null != pdpStatusContext.getPolicies()
144 && new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())
145 .equals(pdpStatusContext.getPolicies())
146 && (null == pdpUpdateMsg.getPdpHeartbeatIntervalMs()
147 || (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs()
148 && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
149 && interval == pdpUpdateMsg.getPdpHeartbeatIntervalMs()));
153 * Method to update the time interval used by the timer task.
155 * @param interval time interval received in the pdp update message from pap
157 public void updateInterval(final long interval) {
158 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
159 pdpStatusPublisher.terminate();
160 final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
161 Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
162 new PdpStatusPublisher(topicSinks, interval));