599de86253fcf99c2814b233bf311ab80ae30174
[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.endpoints.event.comm.TopicSink;
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.PdpStatus;
32 import org.onap.policy.models.pdp.concepts.PdpUpdate;
33 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
34 import org.onap.policy.models.pdp.enums.PdpState;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * This class supports the handling of pdp update messages.
40  *
41  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
42  */
43 public class PdpUpdateMessageHandler {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class);
46
47     /**
48      * Method which handles a pdp update event from PAP.
49      *
50      * @param pdpUpdateMsg pdp update message
51      */
52     public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
53         final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
54         final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
55         PdpResponseDetails pdpResponseDetails = null;
56         if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
57                 pdpStatusContext.getPdpSubgroup())) {
58             final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
59             if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
60                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
61                         PdpResponseStatus.SUCCESS, "Pdp already updated");
62             } else {
63                 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
64                         && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
65                     updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
66                 }
67                 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
68                 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
69                 pdpStatusContext
70                         .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
71                 if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
72                     pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
73                 }
74                 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
75                 if (null == pdpResponseDetails) {
76                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
77                             PdpResponseStatus.SUCCESS, "Pdp update successful.");
78                 }
79             }
80             final PdpStatusPublisher pdpStatusPublisherTemp =
81                     Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
82             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
83             pdpStatus.setResponse(pdpResponseDetails);
84             pdpStatus.setDescription("Pdp status response message for PdpUpdate");
85             pdpStatusPublisherTemp.send(pdpStatus);
86         }
87     }
88
89     /**
90      * Method to start or stop apex engine based on the list of policies received from pap. When current state is
91      * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies,
92      * stop the current running policies and the deploy the new ones.
93      *
94      * @param pdpUpdateMsg the pdp update message from pap
95      * @param pdpMessageHandler pdp message handler
96      * @param pdpStatusContext the pdp status object in memory
97      * @return pdpResponseDetails the pdp response
98      */
99     private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
100             final PdpMessageHandler pdpMessageHandler) {
101         PdpResponseDetails pdpResponseDetails = null;
102         if (pdpUpdateMsg.getPolicies().isEmpty()) {
103             final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
104             if (apexEngineHandler.isApexEngineRunning()) {
105                 try {
106                     apexEngineHandler.shutdown();
107                 } catch (final ApexStarterException e) {
108                     LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
109                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
110                             PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
111                 }
112             }
113         } else {
114             try {
115                 ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER);
116                 if (apexEngineHandler.isApexEngineRunning()) {
117                     apexEngineHandler.shutdown();
118                 }
119                 apexEngineHandler = new ApexEngineHandler(
120                         (String) pdpUpdateMsg.getPolicies().get(0).getProperties().get("content"));
121                 Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
122                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
123                         PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
124             } catch (final ApexStarterException e) {
125                 LOGGER.error("Apex engine service running failed. ", e);
126                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
127                         PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
128             }
129         }
130         return pdpResponseDetails;
131     }
132
133     /**
134      * Method checks if the Pdp update message is already handled by checking the values in the context.
135      *
136      * @param pdpUpdateMsg pdp update message received from pap
137      * @param pdpStatusContext values saved in context memory
138      * @return boolean flag which tells if the information is same or not
139      */
140     private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
141
142         if (null != pdpStatusContext.getPdpGroup()
143                 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())) {
144             return true;
145         } else if (null != pdpStatusContext.getPdpSubgroup()
146                 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())) {
147             return true;
148         } else {
149             return null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
150                     .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
151         }
152     }
153
154     /**
155      * Method to update the time interval used by the timer task.
156      *
157      * @param interval time interval received in the pdp update message from pap
158      */
159     public void updateInterval(final long interval) {
160         final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
161         pdpStatusPublisher.terminate();
162         final List<TopicSink> topicSinks = Registry.get(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS);
163         Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
164                 new PdpStatusPublisher(topicSinks, interval));
165     }
166 }