9f97fb24b58276de006a34789e2f75c71cdb2b8c
[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 (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
55                 pdpStatusContext.getPdpSubgroup())) {
56             switch (pdpStateChangeMsg.getState()) {
57                 case PASSIVE:
58                     pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
59                     break;
60                 case ACTIVE:
61                     pdpResponseDetails = handleActiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler);
62                     break;
63                 default:
64                     break;
65             }
66             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
67             pdpStatus.setResponse(pdpResponseDetails);
68             pdpStatus.setDescription("Pdp status response message for PdpStateChange");
69             pdpStatusPublisher.send(pdpStatus);
70         }
71     }
72
73     /**
74      * Method to handle when the new state from pap is active.
75      *
76      * @param pdpStateChangeMsg
77      * @param pdpStatusContext
78      * @param pdpMessageHandler
79      * @return pdpResponseDetails
80      */
81     private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg,
82             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
83         PdpResponseDetails pdpResponseDetails = null;
84         if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
85             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
86                     PdpResponseStatus.SUCCESS, "Pdp already in active state");
87         } else {
88             final List<ToscaPolicy> policies = Registry.get(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST);
89             if (policies.isEmpty()) {
90                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
91                         PdpResponseStatus.SUCCESS, "No policies found. Apex engine not running.");
92             } else {
93                 try {
94                     // assumed that the apex policies list contains only one entry.
95                     final ApexEngineHandler apexEngineHandler =
96                             new ApexEngineHandler((String) policies.get(0).getProperties().get("content"));
97                     Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
98                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
99                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
100                     pdpStatusContext.setState(PdpState.ACTIVE);
101                 } catch (final ApexStarterException e) {
102                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
103                             PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
104                 }
105             }
106         }
107         return pdpResponseDetails;
108     }
109
110     /**
111      * Method to handle when the new state from pap is passive.
112      *
113      * @param pdpStateChangeMsg
114      * @param pdpStatusContext
115      * @param pdpMessageHandler
116      * @return pdpResponseDetails
117      */
118     private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg,
119             final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) {
120         PdpResponseDetails pdpResponseDetails = null;
121         if (pdpStatusContext.getState().equals(PdpState.PASSIVE)) {
122             pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
123                     PdpResponseStatus.SUCCESS, "Pdp already in passive state");
124         } else {
125             final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
126             try {
127                 apexEngineHandler.shutdown();
128                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
129                         PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive.");
130                 pdpStatusContext.setState(PdpState.PASSIVE);
131             } catch (final Exception e) {
132                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
133                         PdpResponseStatus.FAIL,
134                         "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage());
135             }
136         }
137         return pdpResponseDetails;
138     }
139 }