9c53fb1968b3f3a4c73922a7acddb2c20df8f8b9
[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 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * This class supports the handling of pdp state change messages.
40  *
41  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
42  */
43 public class PdpStateChangeMessageHandler {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(PdpStateChangeMessageHandler.class);
46
47     /**
48      * Method which handles a pdp state change event from PAP.
49      *
50      * @param pdpStateChangeMsg pdp state change message
51      */
52     public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
53         final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
54         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
55         final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
56         PdpResponseDetails pdpResponseDetails = null;
57         if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
58                 pdpStatusContext.getPdpSubgroup())) {
59             switch (pdpStateChangeMsg.getState()) {
60                 case PASSIVE:
61                     pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
62                     break;
63                 case ACTIVE:
64                     pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
65                     break;
66                 default:
67                     break;
68             }
69             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
70             pdpStatus.setResponse(pdpResponseDetails);
71             pdpStatus.setDescription("Pdp status response message for PdpStateChange");
72             pdpStatusPublisher.send(pdpStatus);
73         }
74     }
75
76     /**
77      * Method to handle when the new state from pap is active.
78      *
79      * @param pdpStateChangeMsg pdp state change message
80      * @param pdpStatusContext pdp status object in memory
81      * @param pdpMessageHandler the pdp message handler
82      * @return pdpResponseDetails pdp response
83      */
84     private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
85             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
86         PdpResponseDetails pdpResponseDetails = null;
87         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
88             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
89                     PdpResponseStatus.SUCCESS, "Pdp already in active state");
90         } else {
91             final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
92             if (policies.isEmpty()) {
93                 pdpStatusContext.setState(PdpState.ACTIVE);
94                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
95                         PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
96             } else {
97                 try {
98                     // assumed that the apex policies list contains only one entry.
99                     final ApexEngineHandler apexEngineHandler =
100                             new ApexEngineHandler((String) policies.get(0).getProperties().get("content"));
101                     Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
102                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
103                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
104                     pdpStatusContext.setState(PdpState.ACTIVE);
105                 } catch (final ApexStarterException e) {
106                     LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
107                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
108                             PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
109                 }
110             }
111         }
112         return pdpResponseDetails;
113     }
114
115     /**
116      * Method to handle when the new state from pap is passive.
117      *
118      * @param pdpStateChangeMsg pdp state change message
119      * @param pdpStatusContext pdp status object in memory
120      * @param pdpMessageHandler the pdp message handler
121      * @return pdpResponseDetails pdp response
122      */
123     private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
124             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
125         PdpResponseDetails pdpResponseDetails = null;
126         if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
127             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
128                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
129         } else {
130             ApexEngineHandler apexEngineHandler = null;
131             try {
132                 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
133             } catch (final IllegalArgumentException e) {
134                 LOGGER.debug("ApenEngineHandler not in registry.", e);
135             }
136             try {
137                 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
138                     apexEngineHandler.shutdown();
139                 }
140                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
141                         PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
142                 pdpStatusContext.setState(PdpState.PASSIVE);
143             } catch (final Exception e) {
144                 LOGGER.error("Stopping apex engine failed. State cannot be changed to Passive.", e);
145                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
146                         PdpResponseStatus.FAIL,
147                         "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
148             }
149         }
150         return pdpResponseDetails;
151     }
152 }