c0ec690d840c8ede02f82806027caf26c33a8135
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.starter.handler;
22
23 import java.util.List;
24
25 import org.onap.policy.apex.starter.ApexStarterConstants;
26 import org.onap.policy.apex.starter.comm.PdpStatusPublisher;
27 import org.onap.policy.apex.starter.engine.ApexEngineHandler;
28 import org.onap.policy.apex.starter.exception.ApexStarterException;
29 import org.onap.policy.common.utils.services.Registry;
30 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
31 import org.onap.policy.models.pdp.concepts.PdpStateChange;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
34 import org.onap.policy.models.pdp.enums.PdpState;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36
37 /**
38  * This class supports the handling of pdp state change messages.
39  *
40  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
41  */
42 public class PdpStateChangeMessageHandler {
43
44     /**
45      * Method which handles a pdp state change event from PAP.
46      *
47      * @param pdpStateChangeMsg pdp state change message
48      */
49     public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
50         final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
51         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
52         final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
53         PdpResponseDetails pdpResponseDetails = null;
54         if (isMessageRelevant(pdpStateChangeMsg, pdpStatusContext)) {
55             switch (pdpStateChangeMsg.getState()) {
56                 case TERMINATED:
57                     pdpResponseDetails = handleTerminatedState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
58                     break;
59                 case PASSIVE:
60                     pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
61                     break;
62                 case ACTIVE:
63                     pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
64                     break;
65                 default:
66                     break;
67             }
68             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
69             pdpStatus.setResponse(pdpResponseDetails);
70             pdpStatusPublisher.send(pdpStatus);
71         }
72     }
73
74     /**
75      * Check if the pdp state change message is meant for this pdp.
76      *
77      * @param pdpStateChangeMsg
78      * @param pdpStatusContext
79      * @return boolean value if relevant or not
80      */
81     private boolean isMessageRelevant(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext) {
82         return pdpStatusContext.getName().equals(pdpStateChangeMsg.getName()) || (null == pdpStateChangeMsg.getName()
83                 && pdpStateChangeMsg.getPdpGroup().equals(pdpStatusContext.getPdpGroup())
84                 && pdpStateChangeMsg.getPdpSubgroup().equals(pdpStatusContext.getPdpSubgroup()));
85     }
86
87     /**
88      * Method to handle when the new state from pap is active.
89      *
90      * @param pdpStateChangeMsg
91      * @param pdpStatusContext
92      * @param pdpMessageHandler
93      * @return pdpResponseDetails
94      */
95     private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
96             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
97         PdpResponseDetails pdpResponseDetails = null;
98         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
99             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
100                     PdpResponseStatus.SUCCESS, "Pdp already in active state");
101         } else {
102             final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
103             if (policies.isEmpty()) {
104                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
105                         PdpResponseStatus.SUCCESS, "No policies found. Apex engine not running.");
106             } else {
107                 try {
108                     // assumed that the apex policies list contains only one entry.
109                     final ApexEngineHandler apexEngineHandler =
110                             new ApexEngineHandler((String) policies.get(0).getProperties().get("content"));
111                     Registry.register(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
112                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
113                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
114                     pdpStatusContext.setState(PdpState.ACTIVE);
115                 } catch (final ApexStarterException e) {
116                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
117                             PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
118                 }
119             }
120         }
121         return pdpResponseDetails;
122     }
123
124     /**
125      * Method to handle when the new state from pap is passive.
126      *
127      * @param pdpStateChangeMsg
128      * @param pdpStatusContext
129      * @param pdpMessageHandler
130      * @return pdpResponseDetails
131      */
132     private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
133             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
134         PdpResponseDetails pdpResponseDetails = null;
135         if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
136             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
137                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
138         } else {
139             final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
140             try {
141                 apexEngineHandler.shutdown();
142                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
143                         PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
144                 pdpStatusContext.setState(PdpState.PASSIVE);
145             } catch (final Exception e) {
146                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
147                         PdpResponseStatus.FAIL,
148                         "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
149             }
150         }
151         return pdpResponseDetails;
152     }
153
154     /**
155      * Method to handle when the new state from pap is terminated.
156      *
157      * @param pdpStateChangeMsg
158      * @param pdpStatusPublisher
159      * @param pdpMessageHandler
160      * @return pdpResponseDetails
161      */
162     private PdpResponseDetails handleTerminatedState(final PdpStateChange pdpStateChangeMsg,
163             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
164
165         PdpResponseDetails pdpResponseDetails;
166         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
167             final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
168             try {
169                 apexEngineHandler.shutdown();
170                 pdpStatusContext.setState(PdpState.PASSIVE);
171                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
172                         PdpResponseStatus.SUCCESS, "Apex Engine stopped. State changed to passive.");
173             } catch (final Exception e) {
174                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
175                         PdpResponseStatus.FAIL,
176                         "Stopping apex engine failed. State cannot be changed." + e.getMessage());
177             }
178         } else {
179             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
180                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
181         }
182         return pdpResponseDetails;
183
184     }
185 }