fd95b47b779355c90b60aa5399df324bd020822f
[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.HashSet;
24 import java.util.List;
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.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * This class supports the handling of pdp state change messages.
41  *
42  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
43  */
44 public class PdpStateChangeMessageHandler {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(PdpStateChangeMessageHandler.class);
47
48     /**
49      * Method which handles a pdp state change event from PAP.
50      *
51      * @param pdpStateChangeMsg pdp state change message
52      */
53     public void handlePdpStateChangeEvent(final PdpStateChange pdpStateChangeMsg) {
54         final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
55         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
56         final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
57         PdpResponseDetails pdpResponseDetails = null;
58         if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
59                 pdpStatusContext.getPdpSubgroup())) {
60             switch (pdpStateChangeMsg.getState()) {
61                 case PASSIVE:
62                     pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
63                     break;
64                 case ACTIVE:
65                     pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
66                     break;
67                 default:
68                     break;
69             }
70             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
71             pdpStatus.setResponse(pdpResponseDetails);
72             pdpStatus.setDescription("Pdp status response message for PdpStateChange");
73             pdpStatusPublisher.send(pdpStatus);
74         }
75     }
76
77     /**
78      * Method to handle when the new state from pap is active.
79      *
80      * @param pdpStateChangeMsg pdp state change message
81      * @param pdpStatusContext pdp status object in memory
82      * @param pdpMessageHandler the pdp message handler
83      * @return pdpResponseDetails pdp response
84      */
85     private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
86             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
87         PdpResponseDetails pdpResponseDetails = null;
88         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
89             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
90                     PdpResponseStatus.SUCCESS, "Pdp already in active state");
91         } else {
92             final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
93             if (policies.isEmpty()) {
94                 pdpStatusContext.setState(PdpState.ACTIVE);
95                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
96                         PdpResponseStatus.SUCCESS, "State changed to active. No policies found.");
97             } else {
98                 pdpResponseDetails = startApexEngine(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler, policies);
99             }
100         }
101         return pdpResponseDetails;
102     }
103
104     /**
105      * Method to start apex engine.
106      *
107      * @param pdpStateChangeMsg pdp state change message
108      * @param pdpStatusContext pdp status in memory
109      * @param pdpMessageHandler the pdp message handler
110      * @param policies list of policies
111      * @return pdp response details
112      */
113     private PdpResponseDetails startApexEngine(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext,
114         final PdpMessageHandler pdpMessageHandler, final List<ToscaPolicy> policies) {
115         PdpResponseDetails pdpResponseDetails;
116         try {
117             final ApexEngineHandler apexEngineHandler = new ApexEngineHandler(policies);
118             Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
119             if (apexEngineHandler.isApexEngineRunning()) {
120                 List<ToscaPolicyIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
121                 // only the policies which are succesfully executed should be there in the heartbeat
122                 pdpStatusContext.setPolicies(runningPolicies);
123                 if (new HashSet<>(runningPolicies)
124                     .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(policies)))) {
125                     pdpResponseDetails =
126                         pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
127                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
128                 } else {
129                     StringBuilder message = new StringBuilder(
130                         "Apex engine started. But, only the following polices are running - ");
131                     for (ToscaPolicyIdentifier policy : runningPolicies) {
132                         message.append(policy.getName()).append(":").append(policy.getVersion()).append("  ");
133                     }
134                     message.append(". Other policies failed execution. Please see the logs for more details.");
135                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(
136                         pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, message.toString());
137                 }
138                 pdpStatusContext.setState(PdpState.ACTIVE);
139             } else {
140                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
141                     PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active.");
142             }
143         } catch (final ApexStarterException e) {
144             LOGGER.error("Pdp State Change failed.", e);
145             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
146                     PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
147         }
148         return pdpResponseDetails;
149     }
150
151     /**
152      * Method to handle when the new state from pap is passive.
153      *
154      * @param pdpStateChangeMsg pdp state change message
155      * @param pdpStatusContext pdp status object in memory
156      * @param pdpMessageHandler the pdp message handler
157      * @return pdpResponseDetails pdp response
158      */
159     private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
160             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
161         PdpResponseDetails pdpResponseDetails = null;
162         if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
163             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
164                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
165         } else {
166             ApexEngineHandler apexEngineHandler = null;
167             try {
168                 apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
169             } catch (final IllegalArgumentException e) {
170                 LOGGER.debug("ApenEngineHandler not in registry.", e);
171             }
172             try {
173                 if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
174                     apexEngineHandler.shutdown();
175                 }
176                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
177                         PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
178                 pdpStatusContext.setState(PdpState.PASSIVE);
179             } catch (final Exception e) {
180                 LOGGER.error("Stopping apex engine failed. State cannot be changed to Passive.", e);
181                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
182                         PdpResponseStatus.FAIL,
183                         "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
184             }
185         }
186         return pdpResponseDetails;
187     }
188 }