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;
37 * This class supports the handling of pdp update messages.
39 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
41 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;
53 if (pdpStatusContext.getName().equals(pdpUpdateMsg.getName())) {
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 (pdpUpdateMsg.getPolicies().isEmpty()) {
68 final ApexEngineHandler apexEngineHandler =
69 Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
70 if (apexEngineHandler.isApexEngineRunning()) {
72 apexEngineHandler.shutdown();
73 } catch (final ApexStarterException e) {
74 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
75 PdpResponseStatus.FAIL,
76 "Pdp update failed as the policies couldn't be undeployed.");
80 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
81 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
82 PdpResponseStatus.SUCCESS, "Pdp update successful.");
84 final PdpStatusPublisher pdpStatusPublisherTemp =
85 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
86 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
87 pdpStatus.setResponse(pdpResponseDetails);
88 pdpStatusPublisherTemp.send(pdpStatus);
93 * Method checks of the Pdp update message is already handled by checking the values in the context.
95 * @param pdpUpdateMsg pdp update message received from pap
96 * @param pdpStatusContext values saved in context memory
97 * @param interval the current interval in which the pdp status publisher is sending the heartbeats
98 * @return boolean flag which tells if the information is same or not
100 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext,
101 final long interval) {
102 return null != pdpStatusContext.getPdpGroup()
103 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
104 && null != pdpStatusContext.getPdpSubgroup()
105 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
106 && null != pdpStatusContext.getPolicies()
107 && pdpStatusContext.getPolicies()
108 .containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()))
109 && null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
110 && interval == pdpUpdateMsg.getPdpHeartbeatIntervalMs();
114 * Method to update the time interval used by the timer task.
116 * @param interval time interval received in the pdp update message from pap
118 public void updateInterval(final long interval) {
119 final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
120 pdpStatusPublisher.terminate();
121 final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
122 Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
123 new PdpStatusPublisher(topicSinks, interval));