Revert "Synchronize updates to xacml-pdp state"
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / comm / XacmlPdpUpdatePublisher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2020 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.ArrayList;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
28 import org.onap.policy.models.pdp.concepts.PdpStatus;
29 import org.onap.policy.models.pdp.concepts.PdpUpdate;
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
31 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
32 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
33 import org.onap.policy.pdpx.main.XacmlState;
34 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
35 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class XacmlPdpUpdatePublisher {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpUpdatePublisher.class);
42
43     private final TopicSinkClient client;
44     private final XacmlState state;
45     private final XacmlPdpApplicationManager appManager;
46
47     /**
48      * Constructs the object.
49      * @param client messages are published to this client
50      * @param state tracks the state of this PDP
51      * @param appManager application manager
52      */
53     public XacmlPdpUpdatePublisher(TopicSinkClient client, XacmlState state, XacmlPdpApplicationManager appManager) {
54         this.client = client;
55         this.state = state;
56         this.appManager = appManager;
57     }
58
59     /**
60      * Handle the PDP Update message.
61      *
62      * @param message Incoming message
63      */
64     public void handlePdpUpdate(PdpUpdate message) {
65
66         Set<ToscaPolicy> incomingPolicies =
67                 new HashSet<>(message.getPolicies() == null ? Collections.emptyList() : message.getPolicies());
68         Set<ToscaPolicy> deployedPolicies =
69                 new HashSet<>(appManager.getToscaPolicies().keySet());
70
71         // Undeploy a policy
72         // if incoming policies do not contain the deployed policy then remove it from PDP
73         for (ToscaPolicy policy : deployedPolicies) {
74             if (!incomingPolicies.contains(policy)) {
75                 appManager.removeUndeployedPolicy(policy);
76             }
77         }
78
79         StringBuilder errorMessage = new StringBuilder();
80         // Deploy a policy
81         // if deployed policies do not contain the incoming policy load it
82         for (ToscaPolicy policy : incomingPolicies) {
83             if (!deployedPolicies.contains(policy)) {
84                 try {
85                     appManager.loadDeployedPolicy(policy);
86                 } catch (XacmlApplicationException e) {
87                     // Failed to load policy, return error(s) to PAP
88                     LOGGER.error("Failed to load policy: {}", policy, e);
89                     errorMessage.append("Failed to load policy: " + policy + ": "
90                             + e.getMessage() + XacmlPolicyUtils.LINE_SEPARATOR);
91                 }
92             }
93         }
94         // Return current deployed policies
95         message.setPolicies(new ArrayList<>(appManager.getToscaPolicies().keySet()));
96         LOGGER.debug("Returning current deployed policies: {} ", message.getPolicies());
97
98         // update the policy count statistic
99         XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
100         if (stats != null) {
101             stats.setTotalPolicyCount(appManager.getPolicyCount());
102         }
103
104         sendPdpUpdate(state.updateInternalState(message, errorMessage.toString()));
105     }
106
107     private void sendPdpUpdate(PdpStatus status) {
108         // Send PdpStatus Change to PAP
109         if (!client.send(status)) {
110             LOGGER.error("failed to send to topic sink {}", client.getTopic());
111         }
112     }
113
114 }