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