2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.sim.pdp.handler;
24 import java.util.List;
25 import org.onap.policy.common.endpoints.event.comm.TopicSink;
26 import org.onap.policy.common.utils.services.Registry;
27 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
28 import org.onap.policy.models.pdp.concepts.PdpStatus;
29 import org.onap.policy.models.pdp.concepts.PdpUpdate;
30 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
31 import org.onap.policy.models.pdp.enums.PdpState;
32 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
33 import org.onap.policy.models.sim.pdp.comm.PdpStatusPublisher;
36 * This class supports the handling of pdp update messages.
38 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40 public class PdpUpdateMessageHandler {
43 * Method which handles a pdp update event from PAP.
45 * @param pdpUpdateMsg pdp update message
47 public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
48 final var pdpMessageHandler = new PdpMessageHandler();
49 final var pdpStatusContext = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
50 PdpResponseDetails pdpResponseDetails = null;
51 if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
52 pdpStatusContext.getPdpSubgroup())) {
53 final var pdpStatusPublisher =
54 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
55 if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
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) && !pdpUpdateMsg.getPolicies().isEmpty()) {
68 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
69 PdpResponseStatus.SUCCESS, "Pdp engine started and policies are running.");
71 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
72 if (null == pdpResponseDetails) {
73 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
74 PdpResponseStatus.SUCCESS, "Pdp update successful.");
77 final var pdpStatusPublisherTemp =
78 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
79 final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
80 pdpStatus.setResponse(pdpResponseDetails);
81 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
82 pdpStatusPublisherTemp.send(pdpStatus);
88 * Method checks if the Pdp update message is already handled by checking the values in the context.
90 * @param pdpUpdateMsg pdp update message received from pap
91 * @param pdpStatusContext values saved in context memory
92 * @return boolean flag which tells if the information is same or not
94 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
95 if (pdpStatusContext.getPdpGroup() == null
96 || !pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())) {
100 if (pdpStatusContext.getPdpSubgroup() == null
101 || !pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())) {
105 return 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 var pdpStatusPublisher =
116 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
117 pdpStatusPublisher.terminate();
118 final List<TopicSink> topicSinks = Registry.get(PdpSimulatorConstants.REG_PDP_TOPIC_SINKS);
119 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER,
120 new PdpStatusPublisher(topicSinks, interval));