Use lombok in pap
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / notification / PolicyNotifier.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) 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.pap.main.notification;
23
24 import java.util.Set;
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;
35
36 /**
37  * Notifier for completion of policy updates.
38  */
39 @AllArgsConstructor
40 public class PolicyNotifier {
41     private static final Logger logger = LoggerFactory.getLogger(PolicyNotifier.class);
42
43     /**
44      * Notification publisher.
45      */
46     private final Publisher<PolicyNotification> publisher;
47
48     private final PolicyModelsProviderFactoryWrapper daoFactory;
49
50     /**
51      * Processes a response from a PDP.
52      *
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
57      *        the response
58      */
59     public synchronized void processResponse(String pdp, String pdpGroup, Set<ToscaConceptIdentifier> expectedPolicies,
60                     Set<ToscaConceptIdentifier> actualPolicies) {
61
62         try (PolicyModelsProvider dao = daoFactory.create()) {
63             DeploymentStatus status = makeDeploymentTracker(dao);
64             status.loadByGroup(pdpGroup);
65             status.completeDeploy(pdp, expectedPolicies, actualPolicies);
66
67             var notification = new PolicyNotification();
68             status.flush(notification);
69
70             publish(notification);
71
72         } catch (PfModelException e) {
73             logger.warn("cannot update deployment status", e);
74         }
75     }
76
77     /**
78      * Publishes a notification, if it is not empty.
79      *
80      * @param notification notification to be published
81      */
82     public synchronized void publish(PolicyNotification notification) {
83         if (!notification.isEmpty()) {
84             publisher.enqueue(new QueueToken<>(notification));
85         }
86     }
87
88
89     // the following methods may be overridden by junit tests
90
91     protected DeploymentStatus makeDeploymentTracker(PolicyModelsProvider dao) {
92         return new DeploymentStatus(dao);
93     }
94 }