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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.comm;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashSet;
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;
39 public class XacmlPdpUpdatePublisher {
41 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpUpdatePublisher.class);
43 private final TopicSinkClient client;
44 private final XacmlState state;
45 private final XacmlPdpApplicationManager appManager;
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
53 public XacmlPdpUpdatePublisher(TopicSinkClient client, XacmlState state, XacmlPdpApplicationManager appManager) {
56 this.appManager = appManager;
60 * Handle the PDP Update message.
62 * @param message Incoming message
64 public void handlePdpUpdate(PdpUpdate message) {
66 Set<ToscaPolicy> incomingPolicies =
67 new HashSet<>(message.getPolicies() == null ? Collections.emptyList() : message.getPolicies());
68 Set<ToscaPolicy> deployedPolicies =
69 new HashSet<>(appManager.getToscaPolicies().keySet());
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);
79 StringBuilder errorMessage = new StringBuilder();
81 // if deployed policies do not contain the incoming policy load it
82 for (ToscaPolicy policy : incomingPolicies) {
83 if (!deployedPolicies.contains(policy)) {
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);
94 // Return current deployed policies
95 message.setPolicies(new ArrayList<>(appManager.getToscaPolicies().keySet()));
96 LOGGER.debug("Returning current deployed policies: {} ", message.getPolicies());
98 // update the policy count statistic
99 XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
101 stats.setTotalPolicyCount(appManager.getPolicyCount());
104 sendPdpUpdate(state.updateInternalState(message, errorMessage.toString()));
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());