Remove actor and recipe checks from ControlLoopCompiler.java
[policy/models.git] / models-sim / policy-models-sim-pdp / src / main / java / org / onap / policy / models / sim / pdp / handler / PdpUpdateMessageHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.pdp.handler;
23
24 import java.util.List;
25 import org.onap.policy.common.endpoints.event.comm.TopicSink;
26 import org.onap.policy.common.utils.services.Registry;
27 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
28 import org.onap.policy.models.pdp.concepts.PdpStatus;
29 import org.onap.policy.models.pdp.concepts.PdpUpdate;
30 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
31 import org.onap.policy.models.pdp.enums.PdpState;
32 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
33 import org.onap.policy.models.sim.pdp.comm.PdpStatusPublisher;
34
35 /**
36  * This class supports the handling of pdp update messages.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class PdpUpdateMessageHandler {
41
42     /**
43      * Method which handles a pdp update event from PAP.
44      *
45      * @param pdpUpdateMsg pdp update message
46      */
47     public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) {
48         final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
49         final PdpStatus pdpStatusContext = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class);
50         PdpResponseDetails pdpResponseDetails = null;
51         if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(),
52                 pdpStatusContext.getPdpSubgroup())) {
53             final PdpStatusPublisher pdpStatusPublisher = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER);
54             if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
55                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
56                         PdpResponseStatus.SUCCESS, "Pdp already updated");
57             } else {
58                 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
59                         && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
60                     updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
61                 }
62                 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
63                 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
64                 pdpStatusContext
65                         .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
66                 if (pdpStatusContext.getState().equals(PdpState.ACTIVE) && !pdpUpdateMsg.getPolicies().isEmpty()) {
67                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
68                             PdpResponseStatus.SUCCESS, "Pdp engine started and policies are running.");
69                 }
70                 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
71                 if (null == pdpResponseDetails) {
72                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
73                             PdpResponseStatus.SUCCESS, "Pdp update successful.");
74                 }
75             }
76             final PdpStatusPublisher pdpStatusPublisherTemp =
77                     Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER);
78             final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
79             pdpStatus.setResponse(pdpResponseDetails);
80             pdpStatus.setDescription("Pdp status response message for PdpUpdate");
81             pdpStatusPublisherTemp.send(pdpStatus);
82         }
83     }
84
85
86     /**
87      * Method checks if the Pdp update message is already handled by checking the values in the context.
88      *
89      * @param pdpUpdateMsg pdp update message received from pap
90      * @param pdpStatusContext values saved in context memory
91      * @return boolean flag which tells if the information is same or not
92      */
93     private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
94         return null != pdpStatusContext.getPdpGroup()
95                 && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
96                 && null != pdpStatusContext.getPdpSubgroup()
97                 && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
98                 && null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
99                         .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
100     }
101
102     /**
103      * Method to update the time interval used by the timer task.
104      *
105      * @param interval time interval received in the pdp update message from pap
106      */
107     public void updateInterval(final long interval) {
108         final PdpStatusPublisher pdpStatusPublisher = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER);
109         pdpStatusPublisher.terminate();
110         final List<TopicSink> topicSinks = Registry.get(PdpSimulatorConstants.REG_PDP_TOPIC_SINKS);
111         Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER,
112                 new PdpStatusPublisher(topicSinks, interval));
113     }
114 }