Merge "Fix sonars in policy models"
[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, 2021 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 var pdpMessageHandler = new PdpMessageHandler();
49         final var 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 var pdpStatusPublisher =
54                             Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
55             if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) {
56                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
57                         PdpResponseStatus.SUCCESS, "Pdp already updated");
58             } else {
59                 if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0
60                         && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) {
61                     updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs());
62                 }
63                 pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup());
64                 pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup());
65                 pdpStatusContext
66                         .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
67                 if (pdpStatusContext.getState().equals(PdpState.ACTIVE) && !pdpUpdateMsg.getPolicies().isEmpty()) {
68                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
69                             PdpResponseStatus.SUCCESS, "Pdp engine started and policies are running.");
70                 }
71                 Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies());
72                 if (null == pdpResponseDetails) {
73                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
74                             PdpResponseStatus.SUCCESS, "Pdp update successful.");
75                 }
76             }
77             final var pdpStatusPublisherTemp =
78                     Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
79             final var pdpStatus = pdpMessageHandler.createPdpStatusFromContext();
80             pdpStatus.setResponse(pdpResponseDetails);
81             pdpStatus.setDescription("Pdp status response message for PdpUpdate");
82             pdpStatusPublisherTemp.send(pdpStatus);
83         }
84     }
85
86
87     /**
88      * Method checks if the Pdp update message is already handled by checking the values in the context.
89      *
90      * @param pdpUpdateMsg pdp update message received from pap
91      * @param pdpStatusContext values saved in context memory
92      * @return boolean flag which tells if the information is same or not
93      */
94     private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) {
95         if (pdpStatusContext.getPdpGroup() == null
96                 || !pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())) {
97             return false;
98         }
99
100         if (pdpStatusContext.getPdpSubgroup() == null
101                 || !pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())) {
102             return false;
103         }
104
105         return null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
106                 .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
107     }
108
109     /**
110      * Method to update the time interval used by the timer task.
111      *
112      * @param interval time interval received in the pdp update message from pap
113      */
114     public void updateInterval(final long interval) {
115         final var pdpStatusPublisher =
116                         Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
117         pdpStatusPublisher.terminate();
118         final List<TopicSink> topicSinks = Registry.get(PdpSimulatorConstants.REG_PDP_TOPIC_SINKS);
119         Registry.registerOrReplace(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER,
120                 new PdpStatusPublisher(topicSinks, interval));
121     }
122 }