2 * ============LICENSE_START=======================================================
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-2022 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.comm;
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.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
37 import org.onap.policy.pap.main.PapConstants;
38 import org.onap.policy.pap.main.notification.DeploymentStatus;
39 import org.onap.policy.pap.main.notification.PolicyNotifier;
40 import org.onap.policy.pap.main.parameters.PapParameterGroup;
41 import org.onap.policy.pap.main.service.PolicyStatusService;
42 import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.context.event.ApplicationReadyEvent;
47 import org.springframework.context.event.EventListener;
51 * PDP message generator used to generate PDP-UPDATE and PDP-STATE-CHANGE messages.
53 public class PdpMessageGenerator {
54 private static final Logger LOGGER = LoggerFactory.getLogger(PdpMessageGenerator.class);
56 private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
58 private boolean includeHeartBeat;
60 * Lock used when updating PDPs.
62 protected Object updateLock;
65 * Used to send UPDATE and STATE-CHANGE requests to the PDPs.
67 protected PdpModifyRequestMap requestMap;
70 * Heart beat interval, in milliseconds, to pass to PDPs, or {@code null}.
72 private Long heartBeatMs;
75 private ToscaServiceTemplateService toscaService;
78 private PolicyStatusService policyStatusService;
81 private PolicyNotifier policyNotifier;
84 * Constructs the object.
86 * @param includeHeartBeat if the heart beat interval is to be included in any
89 public PdpMessageGenerator(boolean includeHeartBeat) {
90 this.includeHeartBeat = includeHeartBeat;
94 * Initialize the parameters.
96 @EventListener(ApplicationReadyEvent.class)
97 public void initialize() {
98 updateLock = Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class);
99 requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
101 if (includeHeartBeat) {
102 PapParameterGroup params = ParameterService.get(PAP_GROUP_PARAMS_NAME);
103 heartBeatMs = params.getPdpParameters().getHeartBeatMs();
110 protected PdpUpdate createPdpUpdateMessage(final String pdpGroupName, final PdpSubGroup subGroup,
111 final String pdpInstanceId,
112 final List<ToscaPolicy> policiesToBeDeployed,
113 final List<ToscaConceptIdentifier> policiesToBeUndeployed) {
115 final var update = new PdpUpdate();
117 update.setSource(PapConstants.PAP_NAME);
118 update.setName(pdpInstanceId);
119 update.setPdpGroup(pdpGroupName);
120 update.setPdpSubgroup(subGroup.getPdpType());
121 update.setPoliciesToBeDeployed(policiesToBeDeployed);
122 update.setPoliciesToBeUndeployed(policiesToBeUndeployed);
123 update.setPdpHeartbeatIntervalMs(heartBeatMs);
125 LOGGER.debug("Created PdpUpdate message - {}", update);
130 * Method to return a list of policies.
132 * @param subGroup PdpSubGroup to retrieve policies from
133 * @throws PfModelException the exception
134 * @returns a list of ToscaPolicy
136 public List<ToscaPolicy> getToscaPolicies(final PdpSubGroup subGroup) throws PfModelException {
138 final List<ToscaPolicy> policies = new LinkedList<>();
139 for (final ToscaConceptIdentifier policyIdentifier : subGroup.getPolicies()) {
140 policies.addAll(toscaService.getPolicyList(policyIdentifier.getName(), policyIdentifier.getVersion()));
143 LOGGER.debug("Created ToscaPolicy list - {}", policies);
147 protected PdpStateChange createPdpStateChangeMessage(final String pdpGroupName, final PdpSubGroup subGroup,
148 final String pdpInstanceId, final PdpState pdpState) {
150 final var stateChange = new PdpStateChange();
151 stateChange.setSource(PapConstants.PAP_NAME);
152 stateChange.setName(pdpInstanceId);
153 stateChange.setPdpGroup(pdpGroupName);
154 stateChange.setPdpSubgroup(subGroup.getPdpType());
155 stateChange.setState(pdpState == null ? PdpState.ACTIVE : pdpState);
157 LOGGER.debug("Created PdpStateChange message - {}", stateChange);
162 * If group state=ACTIVE AND updateMsg has policiesToDeploy, then make sure deployment status is updated
163 * If group state=PASSIVE, then delete any deployment information for a PDP.
165 * @param pdpGroupName the group name
166 * @param pdpType the pdp type
167 * @param pdpInstanceId the pdp instance
168 * @param pdpState the new state as per the PDP_STATE_CHANGE change message
169 * @param policies list of policies as per the PDP_UPDATE message
170 * @throws PfModelException the exception
172 protected void updateDeploymentStatus(final String pdpGroupName, final String pdpType, final String pdpInstanceId,
173 PdpState pdpState, List<ToscaPolicy> policies) {
174 var deploymentStatus = new DeploymentStatus(policyStatusService);
175 deploymentStatus.loadByGroup(pdpGroupName);
176 if (pdpState.equals(PdpState.PASSIVE)) {
177 deploymentStatus.deleteDeployment(pdpInstanceId);
178 } else if (pdpState.equals(PdpState.ACTIVE)) {
179 for (ToscaPolicy toscaPolicy : policies) {
180 deploymentStatus.deploy(pdpInstanceId, toscaPolicy.getIdentifier(), toscaPolicy.getTypeIdentifier(),
181 pdpGroupName, pdpType, true);
184 var notification = new PolicyNotification();
185 deploymentStatus.flush(notification);
186 policyNotifier.publish(notification);