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 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.notification;
25 import lombok.AllArgsConstructor;
26 import org.onap.policy.models.base.PfModelException;
27 import org.onap.policy.models.pap.concepts.PolicyNotification;
28 import org.onap.policy.models.provider.PolicyModelsProvider;
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
30 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
31 import org.onap.policy.pap.main.comm.Publisher;
32 import org.onap.policy.pap.main.comm.QueueToken;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Notifier for completion of policy updates.
40 public class PolicyNotifier {
41 private static final Logger logger = LoggerFactory.getLogger(PolicyNotifier.class);
44 * Notification publisher.
46 private final Publisher<PolicyNotification> publisher;
48 private final PolicyModelsProviderFactoryWrapper daoFactory;
51 * Processes a response from a PDP.
53 * @param pdp PDP of interest
54 * @param pdpGroup name of the PdpGroup containing the PDP
55 * @param expectedPolicies policies that expected to be deployed on the PDP
56 * @param actualPolicies policies that are still active on the PDP, as specified in
59 public synchronized void processResponse(String pdp, String pdpGroup, Set<ToscaConceptIdentifier> expectedPolicies,
60 Set<ToscaConceptIdentifier> actualPolicies) {
62 try (PolicyModelsProvider dao = daoFactory.create()) {
63 DeploymentStatus status = makeDeploymentTracker(dao);
64 status.loadByGroup(pdpGroup);
65 status.completeDeploy(pdp, expectedPolicies, actualPolicies);
67 var notification = new PolicyNotification();
68 status.flush(notification);
70 publish(notification);
72 } catch (PfModelException e) {
73 logger.warn("cannot update deployment status", e);
78 * Publishes a notification, if it is not empty.
80 * @param notification notification to be published
82 public synchronized void publish(PolicyNotification notification) {
83 if (!notification.isEmpty()) {
84 publisher.enqueue(new QueueToken<>(notification));
89 // the following methods may be overridden by junit tests
91 protected DeploymentStatus makeDeploymentTracker(PolicyModelsProvider dao) {
92 return new DeploymentStatus(dao);