Update totalPoliciesCount statistic
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / comm / XacmlPdpUpdatePublisher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.comm;
22
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Set;
26 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
27 import org.onap.policy.models.pdp.concepts.PdpStatus;
28 import org.onap.policy.models.pdp.concepts.PdpUpdate;
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
30 import org.onap.policy.pdpx.main.XacmlState;
31 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
32 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class XacmlPdpUpdatePublisher {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpUpdatePublisher.class);
39
40     private final TopicSinkClient client;
41     private final XacmlState state;
42     private final XacmlPdpApplicationManager appManager;
43
44     /**
45      * Constructs the object.
46      * @param client messages are published to this client
47      * @param state tracks the state of this PDP
48      * @param appManager application manager
49      */
50     public XacmlPdpUpdatePublisher(TopicSinkClient client, XacmlState state, XacmlPdpApplicationManager appManager) {
51         this.client = client;
52         this.state = state;
53         this.appManager = appManager;
54     }
55
56     /**
57      * Handle the PDP Update message.
58      *
59      * @param message Incoming message
60      */
61     public void handlePdpUpdate(PdpUpdate message) {
62
63         Set<ToscaPolicy> incomingPolicies =
64                 new HashSet<>(message.getPolicies() == null ? Collections.emptyList() : message.getPolicies());
65         Set<ToscaPolicy> deployedPolicies =
66                 new HashSet<>(appManager.getToscaPolicies().keySet());
67
68         // Undeploy a policy
69         // if incoming policies do not contain the deployed policy then remove it from PDP
70         for (ToscaPolicy policy : deployedPolicies) {
71             if (!incomingPolicies.contains(policy)) {
72                 appManager.removeUndeployedPolicy(policy);
73             }
74         }
75
76         // Deploy a policy
77         // if deployed policies do not contain the incoming policy load it
78         for (ToscaPolicy policy : incomingPolicies) {
79             if (!deployedPolicies.contains(policy)) {
80                 appManager.loadDeployedPolicy(policy);
81             }
82         }
83
84         // update the policy count statistic
85         XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
86         if (stats != null) {
87             stats.setTotalPolicyCount(appManager.getPolicyCount());
88         }
89
90         sendPdpUpdate(state.updateInternalState(message));
91     }
92
93     private void sendPdpUpdate(PdpStatus status) {
94         // Send PdpStatus Change to PAP
95         if (!client.send(status)) {
96             LOGGER.error("failed to send to topic sink {}", client.getTopic());
97         }
98     }
99
100 }