2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
7 * Modifications Copyright (C) 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.notification;
26 import lombok.RequiredArgsConstructor;
28 import org.onap.policy.models.pap.concepts.PolicyNotification;
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
30 import org.onap.policy.pap.main.comm.Publisher;
31 import org.onap.policy.pap.main.comm.QueueToken;
32 import org.onap.policy.pap.main.service.PolicyStatusService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
38 * Notifier for completion of policy updates.
40 @RequiredArgsConstructor
42 public class PolicyNotifier {
43 private static final Logger logger = LoggerFactory.getLogger(PolicyNotifier.class);
45 private final PolicyStatusService policyStatusService;
48 * Notification publisher.
51 private Publisher<PolicyNotification> publisher;
54 * Processes a response from a PDP.
56 * @param pdp PDP of interest
57 * @param pdpGroup name of the PdpGroup containing the PDP
58 * @param expectedPolicies policies that expected to be deployed on the PDP
59 * @param actualPolicies policies that are still active on the PDP, as specified in
62 public synchronized void processResponse(String pdp, String pdpGroup, Set<ToscaConceptIdentifier> expectedPolicies,
63 Set<ToscaConceptIdentifier> actualPolicies) {
66 DeploymentStatus status = makeDeploymentTracker();
67 status.loadByGroup(pdpGroup);
68 status.completeDeploy(pdp, expectedPolicies, actualPolicies);
70 var notification = new PolicyNotification();
71 status.flush(notification);
73 publish(notification);
75 } catch (RuntimeException e) {
76 logger.warn("cannot update deployment status", e);
81 * Publishes a notification, if it is not empty.
83 * @param notification notification to be published
85 public synchronized void publish(PolicyNotification notification) {
86 if (!notification.isEmpty()) {
87 publisher.enqueue(new QueueToken<>(notification));
92 // the following methods may be overridden by junit tests
94 protected DeploymentStatus makeDeploymentTracker() {
95 return new DeploymentStatus(policyStatusService);