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.utils.services.Registry;
26 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
27 import org.onap.policy.models.pdp.concepts.PdpStateChange;
28 import org.onap.policy.models.pdp.concepts.PdpStatus;
29 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
30 import org.onap.policy.models.pdp.enums.PdpState;
31 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
32 import org.onap.policy.models.sim.pdp.comm.PdpStatusPublisher;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 * This class supports the handling of pdp state change messages.
38 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40 public class PdpStateChangeMessageHandler {
43 * Method which handles a pdp state change event from PAP.
45 * @param pdpStateChangeMsg pdp state change message
47 public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
48 final var pdpStatusContext = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
49 final var pdpStatusPublisher =
50 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
51 final var pdpMessageHandler = new PdpMessageHandler();
52 PdpResponseDetails pdpResponseDetails = null;
53 if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
54 pdpStatusContext.getPdpSubgroup())) {
55 switch (pdpStateChangeMsg.getState()) {
57 pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
60 pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
65 final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
66 pdpStatus.setResponse(pdpResponseDetails);
67 pdpStatus.setDescription("Pdp status response message for PdpStateChange");
68 pdpStatusPublisher.send(pdpStatus);
73 * Method to handle when the new state from pap is active.
75 * @param pdpStateChangeMsg pdp state change message
76 * @param pdpStatusContext pdp status object in memory
77 * @param pdpMessageHandler the pdp message handler
78 * @return pdpResponseDetails pdp response
80 private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
81 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
82 PdpResponseDetails pdpResponseDetails = null;
83 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
84 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
85 PdpResponseStatus.SUCCESS, "Pdp already in active state");
87 final List<ToscaPolicy> policies = Registry.get(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST);
88 if (policies.isEmpty()) {
89 pdpStatusContext.setState(PdpState.ACTIVE);
90 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
91 PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
93 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
94 PdpResponseStatus.SUCCESS, "Pdp started. State changed to active.");
95 pdpStatusContext.setState(PdpState.ACTIVE);
98 return pdpResponseDetails;
102 * Method to handle when the new state from pap is passive.
104 * @param pdpStateChangeMsg pdp state change message
105 * @param pdpStatusContext pdp status object in memory
106 * @param pdpMessageHandler the pdp message handler
107 * @return pdpResponseDetails pdp response
109 private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
110 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
111 PdpResponseDetails pdpResponseDetails = null;
112 if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
113 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
114 PdpResponseStatus.SUCCESS, "Pdp already in passive state");
116 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
117 PdpResponseStatus.SUCCESS, "Pdp state changed from Active to Passive.");
118 pdpStatusContext.setState(PdpState.PASSIVE);
120 return pdpResponseDetails;