Fix sonars in policy-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 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;
34
35 /**
36  * Notifier for completion of policy updates.
37  */
38 public class PolicyNotifier {
39     private static final Logger logger = LoggerFactory.getLogger(PolicyNotifier.class);
40
41     /**
42      * Notification publisher.
43      */
44     private final Publisher<PolicyNotification> publisher;
45
46     private final PolicyModelsProviderFactoryWrapper daoFactory;
47
48
49     /**
50      * Constructs the object. Loads all deployed policies into the internal cache.
51      *
52      * @param publisher notification publisher
53      * @param daoFactory factory used to load policy deployment data from the DB
54      */
55     public PolicyNotifier(Publisher<PolicyNotification> publisher, PolicyModelsProviderFactoryWrapper daoFactory) {
56
57         this.publisher = publisher;
58         this.daoFactory = daoFactory;
59     }
60
61     /**
62      * Processes a response from a PDP.
63      *
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
68      *        the response
69      */
70     public synchronized void processResponse(String pdp, String pdpGroup, Set<ToscaConceptIdentifier> expectedPolicies,
71                     Set<ToscaConceptIdentifier> actualPolicies) {
72
73         try (PolicyModelsProvider dao = daoFactory.create()) {
74             DeploymentStatus status = makeDeploymentTracker(dao);
75             status.loadByGroup(pdpGroup);
76             status.completeDeploy(pdp, expectedPolicies, actualPolicies);
77
78             var notification = new PolicyNotification();
79             status.flush(notification);
80
81             publish(notification);
82
83         } catch (PfModelException e) {
84             logger.warn("cannot update deployment status", e);
85         }
86     }
87
88     /**
89      * Publishes a notification, if it is not empty.
90      *
91      * @param notification notification to be published
92      */
93     public synchronized void publish(PolicyNotification notification) {
94         if (!notification.isEmpty()) {
95             publisher.enqueue(new QueueToken<>(notification));
96         }
97     }
98
99
100     // the following methods may be overridden by junit tests
101
102     protected DeploymentStatus makeDeploymentTracker(PolicyModelsProvider dao) {
103         return new DeploymentStatus(dao);
104     }
105 }