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.models.sim.pdp.handler;
23 import java.util.List;
26 import org.onap.policy.common.endpoints.event.comm.TopicSink;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
29 import org.onap.policy.models.pdp.concepts.PdpStatus;
30 import org.onap.policy.models.pdp.concepts.PdpUpdate;
31 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
32 import org.onap.policy.models.pdp.enums.PdpState;
33 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
34 import org.onap.policy.models.sim.pdp.comm.PdpStatusPublisher;
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(PdpSimulatorConstants.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(PdpSimulatorConstants.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 if (!pdpUpdateMsg.getPolicies().isEmpty()) {
73 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
74 PdpResponseStatus.SUCCESS, "Pdp engine started and policies are running.");
77 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
78 if (null == pdpResponseDetails) {
79 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
80 PdpResponseStatus.SUCCESS, "Pdp update successful.");
83 final PdpStatusPublisher pdpStatusPublisherTemp =
84 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER);
85 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
86 pdpStatus.setResponse(pdpResponseDetails);
87 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
88 pdpStatusPublisherTemp.send(pdpStatus);
94 * Method checks if the Pdp update message is already handled by checking the values in the context.
96 * @param pdpUpdateMsg pdp update message received from pap
97 * @param pdpStatusContext values saved in context memory
98 * @return boolean flag which tells if the information is same or not
100 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
101 return null != pdpStatusContext.getPdpGroup()
102 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
103 && null != pdpStatusContext.getPdpSubgroup()
104 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
105 && null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
106 .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
110 * Method to update the time interval used by the timer task.
112 * @param interval time interval received in the pdp update message from pap
114 public void updateInterval(final long interval) {
115 final PdpStatusPublisher pdpStatusPublisher = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER);
116 pdpStatusPublisher.terminate();
117 final List<TopicSink> topicSinks = Registry.get(PdpSimulatorConstants.REG_PDP_TOPIC_SINKS);
118 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER,
119 new PdpStatusPublisher(topicSinks, interval));