3a186aed60817b6a88f5b9259d32dd186c12b8b4
[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.services.onappf.handler;
22
23 import java.util.List;
24
25 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
26 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
27 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
30 import org.onap.policy.models.pdp.concepts.PdpStateChange;
31 import org.onap.policy.models.pdp.concepts.PdpStatus;
32 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35
36 /**
37  * This class supports the handling of pdp state change messages.
38  *
39  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40  */
41 public class PdpStateChangeMessageHandler {
42
43     /**
44      * Method which handles a pdp state change event from PAP.
45      *
46      * @param pdpStateChangeMsg pdp state change message
47      */
48     public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
49         final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
50         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
51         final PdpMessageHandler 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 PdpStatus 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
76      * @param pdpStatusContext
77      * @param pdpMessageHandler
78      * @return pdpResponseDetails
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.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
85                     PdpResponseStatus.SUCCESS, "Pdp already in active state");
86         } else {
87             final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
88             if (policies.isEmpty()) {
89                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
90                         PdpResponseStatus.SUCCESS, "No policies found. Apex engine not running.");
91             } else {
92                 try {
93                     // assumed that the apex policies list contains only one entry.
94                     final ApexEngineHandler apexEngineHandler =
95                             new ApexEngineHandler((String) policies.get(0).getProperties().get("content"));
96                     Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
97                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
98                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
99                     pdpStatusContext.setState(PdpState.ACTIVE);
100                 } catch (final ApexStarterException e) {
101                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
102                             PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
103                 }
104             }
105         }
106         return pdpResponseDetails;
107     }
108
109     /**
110      * Method to handle when the new state from pap is passive.
111      *
112      * @param pdpStateChangeMsg
113      * @param pdpStatusContext
114      * @param pdpMessageHandler
115      * @return pdpResponseDetails
116      */
117     private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
118             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
119         PdpResponseDetails pdpResponseDetails = null;
120         if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
121             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
122                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
123         } else {
124             final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
125             try {
126                 apexEngineHandler.shutdown();
127                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
128                         PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
129                 pdpStatusContext.setState(PdpState.PASSIVE);
130             } catch (final Exception e) {
131                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
132                         PdpResponseStatus.FAIL,
133                         "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
134             }
135         }
136         return pdpResponseDetails;
137     }
138 }