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
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.Collections;
24 import java.util.HashSet;
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.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class XacmlPdpUpdatePublisher {
37 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpUpdatePublisher.class);
39 private final TopicSinkClient client;
40 private final XacmlState state;
41 private final XacmlPdpApplicationManager appManager;
44 * Constructs the object.
45 * @param client messages are published to this client
46 * @param state tracks the state of this PDP
47 * @param appManager application manager
49 public XacmlPdpUpdatePublisher(TopicSinkClient client, XacmlState state, XacmlPdpApplicationManager appManager) {
52 this.appManager = appManager;
56 * Handle the PDP Update message.
58 * @param message Incoming message
60 public void handlePdpUpdate(PdpUpdate message) {
62 Set<ToscaPolicy> incomingPolicies =
63 new HashSet<>(message.getPolicies() == null ? Collections.emptyList() : message.getPolicies());
64 Set<ToscaPolicy> deployedPolicies =
65 new HashSet<>(appManager.getToscaPolicies().keySet());
68 // if incoming policies do not contain the deployed policy then remove it from PDP
69 for (ToscaPolicy policy : deployedPolicies) {
70 if (!incomingPolicies.contains(policy)) {
71 appManager.removeUndeployedPolicy(policy);
76 // if deployed policies do not contain the incoming policy load it
77 for (ToscaPolicy policy : incomingPolicies) {
78 if (!deployedPolicies.contains(policy)) {
79 appManager.loadDeployedPolicy(policy);
83 sendPdpUpdate(state.updateInternalState(message));
86 private void sendPdpUpdate(PdpStatus status) {
87 // Send PdpStatus Change to PAP
88 if (!client.send(status)) {
89 LOGGER.error("failed to send to topic sink {}", client.getTopic());