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 org.onap.policy.models.base.PfModelException;
26 import org.onap.policy.models.pap.concepts.PolicyNotification;
27 import org.onap.policy.models.provider.PolicyModelsProvider;
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
29 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
30 import org.onap.policy.pap.main.comm.Publisher;
31 import org.onap.policy.pap.main.comm.QueueToken;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Notifier for completion of policy updates.
38 public class PolicyNotifier {
39 private static final Logger logger = LoggerFactory.getLogger(PolicyNotifier.class);
42 * Notification publisher.
44 private final Publisher<PolicyNotification> publisher;
46 private final PolicyModelsProviderFactoryWrapper daoFactory;
50 * Constructs the object. Loads all deployed policies into the internal cache.
52 * @param publisher notification publisher
53 * @param daoFactory factory used to load policy deployment data from the DB
55 public PolicyNotifier(Publisher<PolicyNotification> publisher, PolicyModelsProviderFactoryWrapper daoFactory) {
57 this.publisher = publisher;
58 this.daoFactory = daoFactory;
62 * Processes a response from a PDP.
64 * @param pdp PDP of interest
65 * @param pdpGroup name of the PdpGroup containing the PDP
66 * @param expectedPolicies policies that expected to be deployed on the PDP
67 * @param actualPolicies policies that are still active on the PDP, as specified in
70 public synchronized void processResponse(String pdp, String pdpGroup, Set<ToscaConceptIdentifier> expectedPolicies,
71 Set<ToscaConceptIdentifier> actualPolicies) {
73 try (PolicyModelsProvider dao = daoFactory.create()) {
74 DeploymentStatus status = makeDeploymentTracker(dao);
75 status.loadByGroup(pdpGroup);
76 status.completeDeploy(pdp, expectedPolicies, actualPolicies);
78 var notification = new PolicyNotification();
79 status.flush(notification);
81 publish(notification);
83 } catch (PfModelException e) {
84 logger.warn("cannot update deployment status", e);
89 * Publishes a notification, if it is not empty.
91 * @param notification notification to be published
93 public synchronized void publish(PolicyNotification notification) {
94 if (!notification.isEmpty()) {
95 publisher.enqueue(new QueueToken<>(notification));
100 // the following methods may be overridden by junit tests
102 protected DeploymentStatus makeDeploymentTracker(PolicyModelsProvider dao) {
103 return new DeploymentStatus(dao);