2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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 PdpStatus pdpStatusContext = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
49 final PdpStatusPublisher pdpStatusPublisher = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER);
50 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
51 PdpResponseDetails pdpResponseDetails = null;
52 if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
53 pdpStatusContext.getPdpSubgroup())) {
54 switch (pdpStateChangeMsg.getState()) {
56 pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
59 pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
64 final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
65 pdpStatus.setResponse(pdpResponseDetails);
66 pdpStatus.setDescription("Pdp status response message for PdpStateChange");
67 pdpStatusPublisher.send(pdpStatus);
72 * Method to handle when the new state from pap is active.
74 * @param pdpStateChangeMsg pdp state change message
75 * @param pdpStatusContext pdp status object in memory
76 * @param pdpMessageHandler the pdp message handler
77 * @return pdpResponseDetails pdp response
79 private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
80 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
81 PdpResponseDetails pdpResponseDetails = null;
82 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
83 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
84 PdpResponseStatus.SUCCESS, "Pdp already in active state");
86 final List<ToscaPolicy> policies = Registry.get(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST);
87 if (policies.isEmpty()) {
88 pdpStatusContext.setState(PdpState.ACTIVE);
89 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
90 PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
92 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
93 PdpResponseStatus.SUCCESS, "Pdp started. State changed to active.");
94 pdpStatusContext.setState(PdpState.ACTIVE);
97 return pdpResponseDetails;
101 * Method to handle when the new state from pap is passive.
103 * @param pdpStateChangeMsg pdp state change message
104 * @param pdpStatusContext pdp status object in memory
105 * @param pdpMessageHandler the pdp message handler
106 * @return pdpResponseDetails pdp response
108 private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
109 final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
110 PdpResponseDetails pdpResponseDetails = null;
111 if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
112 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
113 PdpResponseStatus.SUCCESS, "Pdp already in passive state");
115 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
116 PdpResponseStatus.SUCCESS, "Pdp state changed from Active to Passive.");
117 pdpStatusContext.setState(PdpState.PASSIVE);
119 return pdpResponseDetails;