2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
30 import org.onap.policy.models.pdp.concepts.PdpStatus;
31 import org.onap.policy.models.pdp.concepts.PdpUpdate;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
34 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
35 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
36 import org.onap.policy.pdpx.main.XacmlState;
37 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
38 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class XacmlPdpUpdatePublisher {
44 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpUpdatePublisher.class);
46 private final TopicSinkClient client;
47 private final XacmlState state;
48 private final XacmlPdpApplicationManager appManager;
51 * Constructs the object.
52 * @param client messages are published to this client
53 * @param state tracks the state of this PDP
54 * @param appManager application manager
56 public XacmlPdpUpdatePublisher(TopicSinkClient client, XacmlState state, XacmlPdpApplicationManager appManager) {
59 this.appManager = appManager;
63 * Handle the PDP Update message.
65 * @param message Incoming message
67 public void handlePdpUpdate(PdpUpdate message) {
70 Map<ToscaConceptIdentifier, ToscaPolicy> deployedPolicies = policyToMap(appManager.getToscaPolicies().keySet());
73 Map<ToscaConceptIdentifier, ToscaPolicy> toBeDeployedPolicies = policyToMap(message.getPoliciesToBeDeployed());
74 List<ToscaConceptIdentifier> toBeUndeployedIds =
75 Optional.ofNullable(message.getPoliciesToBeUndeployed()).orElse(Collections.emptyList());
78 for (ToscaConceptIdentifier policyId: toBeUndeployedIds) {
79 ToscaPolicy policy = deployedPolicies.get(policyId);
81 LOGGER.warn("attempt to undeploy policy that has not been previously deployed: {}", policyId);
82 } else if (toBeDeployedPolicies.containsKey(policyId)) {
83 LOGGER.warn("not undeploying policy, as it also appears in the deployment list: {}", policyId);
85 appManager.removeUndeployedPolicy(policy);
89 var errorMessage = new StringBuilder();
91 // if deployed policies do not contain the incoming policy load it
92 for (ToscaPolicy policy : toBeDeployedPolicies.values()) {
93 if (!deployedPolicies.containsKey(policy.getIdentifier())) {
95 appManager.loadDeployedPolicy(policy);
96 } catch (XacmlApplicationException e) {
97 // Failed to load policy, return error(s) to PAP
98 LOGGER.error("Failed to load policy: {}", policy, e);
99 errorMessage.append("Failed to load policy: " + policy + ": "
100 + e.getMessage() + XacmlPolicyUtils.LINE_SEPARATOR);
105 // update the policy count statistic
106 var stats = XacmlPdpStatisticsManager.getCurrent();
108 stats.setTotalPolicyCount(appManager.getPolicyCount());
111 PdpStatus status = state.updateInternalState(message, errorMessage.toString());
112 LOGGER.debug("Returning current deployed policies: {} ", status.getPolicies());
114 sendPdpUpdate(status);
117 private Map<ToscaConceptIdentifier, ToscaPolicy> policyToMap(Collection<ToscaPolicy> policies) {
118 if (policies == null) {
119 return Collections.emptyMap();
122 return policies.stream().collect(Collectors.toMap(ToscaPolicy::getIdentifier, policy -> policy));
125 private void sendPdpUpdate(PdpStatus status) {
126 // Send PdpStatus Change to PAP
127 if (!client.send(status)) {
128 LOGGER.error("failed to send to topic sink {}", client.getTopic());