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