2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2022 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 lombok.AllArgsConstructor;
30 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
31 import org.onap.policy.models.pdp.concepts.PdpStatus;
32 import org.onap.policy.models.pdp.concepts.PdpUpdate;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
36 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
37 import org.onap.policy.pdpx.main.XacmlState;
38 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
39 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class XacmlPdpUpdatePublisher {
46 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpUpdatePublisher.class);
48 private final TopicSinkClient client;
49 private final XacmlState state;
50 private final XacmlPdpApplicationManager appManager;
53 * Handle the PDP Update message.
55 * @param message Incoming message
57 public synchronized void handlePdpUpdate(PdpUpdate message) {
60 Map<ToscaConceptIdentifier, ToscaPolicy> deployedPolicies = policyToMap(appManager.getToscaPolicies().keySet());
63 Map<ToscaConceptIdentifier, ToscaPolicy> toBeDeployedPolicies = policyToMap(message.getPoliciesToBeDeployed());
64 List<ToscaConceptIdentifier> toBeUndeployedIds =
65 Optional.ofNullable(message.getPoliciesToBeUndeployed()).orElse(Collections.emptyList());
67 var stats = XacmlPdpStatisticsManager.getCurrent();
70 for (ToscaConceptIdentifier policyId: toBeUndeployedIds) {
71 ToscaPolicy policy = deployedPolicies.get(policyId);
73 LOGGER.warn("attempt to undeploy policy that has not been previously deployed: {}", policyId);
74 stats.updateUndeployFailureCount();
75 } else if (toBeDeployedPolicies.containsKey(policyId)) {
76 LOGGER.warn("not undeploying policy, as it also appears in the deployment list: {}", policyId);
77 stats.updateUndeployFailureCount();
79 appManager.removeUndeployedPolicy(policy);
80 stats.updateUndeploySuccessCount();
84 var errorMessage = new StringBuilder();
86 // if deployed policies do not contain the incoming policy load it
87 for (ToscaPolicy policy : toBeDeployedPolicies.values()) {
88 if (!deployedPolicies.containsKey(policy.getIdentifier())) {
90 appManager.loadDeployedPolicy(policy);
91 stats.updateDeploySuccessCount();
92 } catch (XacmlApplicationException e) {
93 // Failed to load policy, return error(s) to PAP
94 LOGGER.error("Failed to load policy: {}", policy, e);
95 errorMessage.append("Failed to load policy: " + policy + ": "
96 + e.getMessage() + XacmlPolicyUtils.LINE_SEPARATOR);
97 stats.updateDeployFailureCount();
102 // update the policy count statistic
103 stats.setTotalPolicyCount(appManager.getPolicyCount());
105 PdpStatus status = state.updateInternalState(message, errorMessage.toString());
106 LOGGER.debug("Returning current deployed policies: {} ", status.getPolicies());
108 sendPdpUpdate(status);
111 private Map<ToscaConceptIdentifier, ToscaPolicy> policyToMap(Collection<ToscaPolicy> policies) {
112 if (policies == null) {
113 return Collections.emptyMap();
116 return policies.stream().collect(Collectors.toMap(ToscaPolicy::getIdentifier, policy -> policy));
119 private void sendPdpUpdate(PdpStatus status) {
120 // Send PdpStatus Change to PAP
121 if (!client.send(status)) {
122 LOGGER.error("failed to send to topic sink {}", client.getTopic());