2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.ArrayList;
25 import java.util.List;
26 import java.util.stream.Collectors;
27 import org.onap.policy.common.endpoints.event.comm.TopicSink;
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
30 import org.onap.policy.models.pdp.concepts.PdpStatus;
31 import org.onap.policy.models.pdp.concepts.PdpUpdate;
32 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
35 import org.onap.policy.models.sim.pdp.comm.PdpStatusPublisher;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39 * This class supports the handling of pdp update messages.
41 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
43 public class PdpUpdateMessageHandler {
46 * Method which handles a pdp update event from PAP.
48 * @param pdpUpdateMsg pdp update message
50 public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
51 final var pdpMessageHandler = new PdpMessageHandler();
52 final var pdpStatusContext = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
53 PdpResponseDetails pdpResponseDetails = null;
54 if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
55 pdpStatusContext.getPdpSubgroup())) {
56 final var pdpStatusPublisher =
57 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
58 if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
59 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpUpdateMsg.getRequestId(),
60 PdpResponseStatus.SUCCESS, "Pdp already updated");
62 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
63 && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
64 updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
66 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
67 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
68 @SuppressWarnings("unchecked")
69 List<ToscaPolicy> policies = Registry.getOrDefault(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST,
70 List.class, new ArrayList<>());
71 policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed());
72 policies.removeIf(policy -> pdpUpdateMsg.getPoliciesToBeUndeployed().contains(policy.getIdentifier()));
73 pdpStatusContext.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier)
74 .collect(Collectors.toList()));
75 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)
76 && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
77 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpUpdateMsg.getRequestId(),
78 PdpResponseStatus.SUCCESS, "Pdp engine started and policies are running.");
80 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST, policies);
81 if (null == pdpResponseDetails) {
82 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpUpdateMsg.getRequestId(),
83 PdpResponseStatus.SUCCESS, "Pdp update successful.");
86 final var pdpStatusPublisherTemp =
87 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
88 final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
89 pdpStatus.setResponse(pdpResponseDetails);
90 pdpStatus.setDescription("Pdp status response message for PdpUpdate");
91 pdpStatusPublisherTemp.send(pdpStatus);
97 * Method checks if the Pdp update message is already handled by checking the values in the context.
99 * @param pdpUpdateMsg pdp update message received from pap
100 * @param pdpStatusContext values saved in context memory
101 * @return boolean flag which tells if the information is same or not
103 private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
104 if (pdpStatusContext.getPdpGroup() == null
105 || !pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())) {
109 if (pdpStatusContext.getPdpSubgroup() == null
110 || !pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())) {
114 return null != pdpStatusContext.getPolicies() && (pdpStatusContext.getPolicies()
115 .containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(
116 pdpUpdateMsg.getPoliciesToBeDeployed()))) && pdpStatusContext.getPolicies()
117 .stream().noneMatch(pdpUpdateMsg.getPoliciesToBeUndeployed()::contains);
121 * Method to update the time interval used by the timer task.
123 * @param interval time interval received in the pdp update message from pap
125 public void updateInterval(final long interval) {
126 final var pdpStatusPublisher =
127 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
128 pdpStatusPublisher.terminate();
129 final List<TopicSink> topicSinks = Registry.get(PdpSimulatorConstants.REG_PDP_TOPIC_SINKS);
130 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER,
131 new PdpStatusPublisher(topicSinks, interval));