8658150c039a79a544a33605cc807c0fa0aed9c4
[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 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
25 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
26 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
29 import org.onap.policy.models.pdp.concepts.PdpStateChange;
30 import org.onap.policy.models.pdp.concepts.PdpStatus;
31 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
32 import org.onap.policy.models.pdp.enums.PdpState;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
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     private static final Logger LOGGER = LoggerFactory.getLogger(PdpStateChangeMessageHandler.class);
45
46     /**
47      * Method which handles a pdp state change event from PAP.
48      *
49      * @param pdpStateChangeMsg pdp state change message
50      */
51     public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
52         final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
53         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
54         final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
55         PdpResponseDetails pdpResponseDetails = null;
56         if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
57                 pdpStatusContext.getPdpSubgroup())) {
58             switch (pdpStateChangeMsg.getState()) {
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             pdpStatus.setDescription("Pdp status response message for PdpStateChange");
71             pdpStatusPublisher.send(pdpStatus);
72         }
73     }
74
75     /**
76      * Method to handle when the new state from pap is active.
77      *
78      * @param pdpStateChangeMsg pdp state change message
79      * @param pdpStatusContext pdp status object in memory
80      * @param pdpMessageHandler the pdp message handler
81      * @return pdpResponseDetails pdp response
82      */
83     private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
84             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
85         PdpResponseDetails pdpResponseDetails = null;
86         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
87             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
88                     PdpResponseStatus.SUCCESS, "Pdp already in active state");
89         } else {
90             final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
91             if (policies.isEmpty()) {
92                 pdpStatusContext.setState(PdpState.ACTIVE);
93                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
94                         PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
95             } else {
96                 try {
97                     // assumed that the apex policies list contains only one entry.
98                     final ApexEngineHandler apexEngineHandler =
99                             new ApexEngineHandler(policies.get(0).getProperties().get("content"));
100                     Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
101                     if (apexEngineHandler.isApexEngineRunning()) {
102                         pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
103                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
104                         pdpStatusContext.setState(PdpState.ACTIVE);
105                     } else {
106                         pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
107                             PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active.");
108                     }
109                 } catch (final ApexStarterException e) {
110                     LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
111                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
112                             PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
113                 }
114             }
115         }
116         return pdpResponseDetails;
117     }
118
119     /**
120      * Method to handle when the new state from pap is passive.
121      *
122      * @param pdpStateChangeMsg pdp state change message
123      * @param pdpStatusContext pdp status object in memory
124      * @param pdpMessageHandler the pdp message handler
125      * @return pdpResponseDetails pdp response
126      */
127     private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
128             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
129         PdpResponseDetails pdpResponseDetails = null;
130         if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
131             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
132                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
133         } else {
134             ApexEngineHandler apexEngineHandler = null;
135             try {
136                 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
137             } catch (final IllegalArgumentException e) {
138                 LOGGER.debug("ApenEngineHandler not in registry.", e);
139             }
140             try {
141                 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
142                     apexEngineHandler.shutdown();
143                 }
144                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
145                         PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
146                 pdpStatusContext.setState(PdpState.PASSIVE);
147             } catch (final Exception e) {
148                 LOGGER.error("Stopping apex engine failed. State cannot be changed to Passive.", e);
149                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
150                         PdpResponseStatus.FAIL,
151                         "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
152             }
153         }
154         return pdpResponseDetails;
155     }
156 }