Java 17 Upgrade
[policy/models.git] / models-sim / policy-models-sim-pdp / src / main / java / org / onap / policy / models / sim / pdp / handler / PdpStateChangeMessageHandler.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.pdp.handler;
23
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;
34
35 /**
36  * This class supports the handling of pdp state change messages.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class PdpStateChangeMessageHandler {
41
42     /**
43      * Method which handles a pdp state change event from PAP.
44      *
45      * @param pdpStateChangeMsg pdp state change message
46      */
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()) {
56                 case PASSIVE:
57                     pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
58                     break;
59                 case ACTIVE:
60                     pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
61                     break;
62                 default:
63                     break;
64             }
65             final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
66             pdpStatus.setResponse(pdpResponseDetails);
67             pdpStatus.setDescription("Pdp status response message for PdpStateChange");
68             pdpStatusPublisher.send(pdpStatus);
69         }
70     }
71
72     /**
73      * Method to handle when the new state from pap is active.
74      *
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
79      */
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");
86         } else {
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.");
92             } else {
93                 pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
94                         PdpResponseStatus.SUCCESS, "Pdp started. State changed to active.");
95                 pdpStatusContext.setState(PdpState.ACTIVE);
96             }
97         }
98         return pdpResponseDetails;
99     }
100
101     /**
102      * Method to handle when the new state from pap is passive.
103      *
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
108      */
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");
115         } else {
116             pdpResponseDetails = pdpMessageHandler.createPdpResponseDetails(pdpStateChangeMsg.getRequestId(),
117                     PdpResponseStatus.SUCCESS, "Pdp state changed from Active to Passive.");
118             pdpStatusContext.setState(PdpState.PASSIVE);
119         }
120         return pdpResponseDetails;
121     }
122 }