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;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * This class supports the handling of pdp update messages.
41 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
43 public class PdpUpdateMessageHandler {
45 private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
48 * Method which handles a pdp update event from PAP.
50 * @param pdpUpdateMsg pdp update message
52 public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
53 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
54 final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
55 PdpResponseDetails pdpResponseDetails = null;
56 if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
57 pdpStatusContext.getPdpSubgroup())) {
58 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
59 if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
60 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
61 PdpResponseStatus.SUCCESS, "Pdp already updated");
63 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
64 && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
65 updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
67 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
68 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
70 .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
71 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
72 pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
74 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
75 if (null == pdpResponseDetails) {
76 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
77 PdpResponseStatus.SUCCESS, "Pdp update successful.");
80 final PdpStatusPublisher pdpStatusPublisherTemp =
81 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
82 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
83 pdpStatus.setResponse(pdpResponseDetails);
84 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
85 pdpStatusPublisherTemp.send(pdpStatus);
90 * Method to start or stop apex engine based on the list of policies received from pap. When current state is
91 * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies,
92 * stop the current running policies and the deploy the new ones.
94 * @param pdpUpdateMsg the pdp update message from pap
95 * @param pdpMessageHandler pdp message handler
96 * @param pdpStatusContext the pdp status object in memory
97 * @return pdpResponseDetails the pdp response
99 private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
100 final PdpMessageHandler pdpMessageHandler) {
101 PdpResponseDetails pdpResponseDetails = null;
102 ApexEngineHandler apexEngineHandler = null;
104 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
105 } catch (final IllegalArgumentException e) {
106 LOGGER.debug("ApenEngineHandler not in registry.", e);
108 if (pdpUpdateMsg.getPolicies().isEmpty()) {
109 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
111 apexEngineHandler.shutdown();
112 } catch (final ApexStarterException e) {
113 LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
114 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
115 PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
120 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
121 apexEngineHandler.shutdown();
123 apexEngineHandler = new ApexEngineHandler(
124 (String) pdpUpdateMsg.getPolicies().get(0).getProperties().get("content"));
125 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
126 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
127 PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
128 } catch (final ApexStarterException e) {
129 LOGGER.error("Apex engine service running failed. ", e);
130 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
131 PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
134 return pdpResponseDetails;
138 * Method checks if the Pdp update message is already handled by checking the values in the context.
140 * @param pdpUpdateMsg pdp update message received from pap
141 * @param pdpStatusContext values saved in context memory
142 * @return boolean flag which tells if the information is same or not
144 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
145 return null != pdpStatusContext.getPdpGroup()
146 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
147 && null != pdpStatusContext.getPdpSubgroup()
148 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
149 && null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
150 .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
154 * Method to update the time interval used by the timer task.
156 * @param interval time interval received in the pdp update message from pap
158 public void updateInterval(final long interval) {
159 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
160 pdpStatusPublisher.terminate();
161 final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
162 Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
163 new PdpStatusPublisher(topicSinks, interval));