Fix sonars in policy-pap
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / comm / PdpMessageGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.comm;
24
25 import java.util.LinkedList;
26 import java.util.List;
27 import org.onap.policy.common.parameters.ParameterService;
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.models.base.PfModelException;
30 import org.onap.policy.models.pap.concepts.PolicyNotification;
31 import org.onap.policy.models.pdp.concepts.PdpStateChange;
32 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
33 import org.onap.policy.models.pdp.concepts.PdpUpdate;
34 import org.onap.policy.models.pdp.enums.PdpState;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38 import org.onap.policy.pap.main.PapConstants;
39 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
40 import org.onap.policy.pap.main.notification.DeploymentStatus;
41 import org.onap.policy.pap.main.notification.PolicyNotifier;
42 import org.onap.policy.pap.main.parameters.PapParameterGroup;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47 /**
48  * PDP message generator used to generate PDP-UPDATE and PDP-STATE-CHANGE messages.
49  */
50 public class PdpMessageGenerator {
51     private static final Logger LOGGER = LoggerFactory.getLogger(PdpMessageGenerator.class);
52
53     private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
54
55     /**
56      * Lock used when updating PDPs.
57      */
58     protected final Object updateLock;
59
60     /**
61      * Used to send UPDATE and STATE-CHANGE requests to the PDPs.
62      */
63     protected final PdpModifyRequestMap requestMap;
64
65     /**
66      * Factory for PAP DAO.
67      */
68     protected final PolicyModelsProviderFactoryWrapper modelProviderWrapper;
69
70     /**
71      * Heart beat interval, in milliseconds, to pass to PDPs, or {@code null}.
72      */
73     private final Long heartBeatMs;
74
75     /**
76      * Constructs the object.
77      *
78      * @param includeHeartBeat if the heart beat interval is to be included in any
79      *        PDP-UPDATE messages
80      */
81     public PdpMessageGenerator(boolean includeHeartBeat) {
82         modelProviderWrapper = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
83         updateLock = Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class);
84         requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
85
86         if (includeHeartBeat) {
87             PapParameterGroup params = ParameterService.get(PAP_GROUP_PARAMS_NAME);
88             heartBeatMs = params.getPdpParameters().getHeartBeatMs();
89
90         } else {
91             heartBeatMs = null;
92         }
93     }
94
95     protected PdpUpdate createPdpUpdateMessage(final String pdpGroupName, final PdpSubGroup subGroup,
96                     final String pdpInstanceId, final List<ToscaPolicy> policies,
97                     final List<ToscaPolicy> policiesToBeDeployed,
98                     final List<ToscaConceptIdentifier> policiesToBeUndeployed) {
99
100         final var update = new PdpUpdate();
101
102         update.setName(pdpInstanceId);
103         update.setPdpGroup(pdpGroupName);
104         update.setPdpSubgroup(subGroup.getPdpType());
105         update.setPolicies(policies);
106         update.setPoliciesToBeDeployed(policiesToBeDeployed);
107         update.setPoliciesToBeUndeployed(policiesToBeUndeployed);
108         update.setPdpHeartbeatIntervalMs(heartBeatMs);
109
110         LOGGER.debug("Created PdpUpdate message - {}", update);
111         return update;
112     }
113
114     /**
115      * Method to return a list of policies.
116      *
117      * @param subGroup PdpSubGroup to retrieve policies from
118      * @param databaseProvider PolicyModelsProvider used to retrieve list of policies
119      * @returns a list of ToscaPolicy
120      **/
121     public List<ToscaPolicy> getToscaPolicies(final PdpSubGroup subGroup,
122             final PolicyModelsProvider databaseProvider)
123                     throws PfModelException {
124
125         final List<ToscaPolicy> policies = new LinkedList<>();
126         for (final ToscaConceptIdentifier policyIdentifier : subGroup.getPolicies()) {
127             policies.addAll(databaseProvider.getPolicyList(policyIdentifier.getName(), policyIdentifier.getVersion()));
128         }
129
130         LOGGER.debug("Created ToscaPolicy list - {}", policies);
131         return policies;
132     }
133
134     protected PdpStateChange createPdpStateChangeMessage(final String pdpGroupName, final PdpSubGroup subGroup,
135                     final String pdpInstanceId, final PdpState pdpState) {
136
137         final var stateChange = new PdpStateChange();
138         stateChange.setName(pdpInstanceId);
139         stateChange.setPdpGroup(pdpGroupName);
140         stateChange.setPdpSubgroup(subGroup.getPdpType());
141         stateChange.setState(pdpState == null ? PdpState.ACTIVE : pdpState);
142
143         LOGGER.debug("Created PdpStateChange message - {}", stateChange);
144         return stateChange;
145     }
146
147     /**
148      * If group state=ACTIVE AND updateMsg has policiesToDeploy, then make sure deployment status is updated
149      * If group state=PASSIVE, then delete any deployment information for a PDP.
150      *
151      * @param pdpGroupName the group name
152      * @param pdpType the pdp type
153      * @param pdpInstanceId the pdp instance
154      * @param pdpState the new state as per the PDP_STATE_CHANGE change message
155      * @param databaseProvider the database provider
156      * @param policies list of policies as per the PDP_UPDATE message
157      * @throws PfModelException the exception
158      */
159     protected void updateDeploymentStatus(final String pdpGroupName, final String pdpType, final String pdpInstanceId,
160         PdpState pdpState, final PolicyModelsProvider databaseProvider, List<ToscaPolicy> policies)
161         throws PfModelException {
162         var deploymentStatus = new DeploymentStatus(databaseProvider);
163         deploymentStatus.loadByGroup(pdpGroupName);
164         if (pdpState.equals(PdpState.PASSIVE)) {
165             deploymentStatus.deleteDeployment(pdpInstanceId);
166         } else if (pdpState.equals(PdpState.ACTIVE)) {
167             for (ToscaPolicy toscaPolicy : policies) {
168                 deploymentStatus.deploy(pdpInstanceId, toscaPolicy.getIdentifier(), toscaPolicy.getTypeIdentifier(),
169                     pdpGroupName, pdpType, true);
170             }
171         }
172         var notification = new PolicyNotification();
173         deploymentStatus.flush(notification);
174         PolicyNotifier notifier = Registry.get(PapConstants.REG_POLICY_NOTIFIER);
175         notifier.publish(notification);
176     }
177 }