Fix sonars in policy models
[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 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 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()) {
55                 case PASSIVE:
56                     pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
57                     break;
58                 case ACTIVE:
59                     pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
60                     break;
61                 default:
62                     break;
63             }
64             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
65             pdpStatus.setResponse(pdpResponseDetails);
66             pdpStatus.setDescription("Pdp status response message for PdpStateChange");
67             pdpStatusPublisher.send(pdpStatus);
68         }
69     }
70
71     /**
72      * Method to handle when the new state from pap is active.
73      *
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
78      */
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");
85         } else {
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.");
91             } else {
92                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
93                         PdpResponseStatus.SUCCESS, "Pdp started. State changed to active.");
94                 pdpStatusContext.setState(PdpState.ACTIVE);
95             }
96         }
97         return pdpResponseDetails;
98     }
99
100     /**
101      * Method to handle when the new state from pap is passive.
102      *
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
107      */
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");
114         } else {
115             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
116                     PdpResponseStatus.SUCCESS, "Pdp state changed from Active to Passive.");
117             pdpStatusContext.setState(PdpState.PASSIVE);
118         }
119         return pdpResponseDetails;
120     }
121 }