Merge "Create the operationshistory table from pdpx"
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / comm / XacmlPdpUpdatePublisher.java
1 /*-
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
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.Collections;
24 import java.util.HashSet;
25 import java.util.Set;
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;
34
35 public class XacmlPdpUpdatePublisher {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpUpdatePublisher.class);
38
39     private final TopicSinkClient client;
40     private final XacmlState state;
41     private final XacmlPdpApplicationManager appManager;
42
43     /**
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
48      */
49     public XacmlPdpUpdatePublisher(TopicSinkClient client, XacmlState state, XacmlPdpApplicationManager appManager) {
50         this.client = client;
51         this.state = state;
52         this.appManager = appManager;
53     }
54
55     /**
56      * Handle the PDP Update message.
57      *
58      * @param message Incoming message
59      */
60     public void handlePdpUpdate(PdpUpdate message) {
61
62         Set<ToscaPolicy> incomingPolicies =
63                 new HashSet<>(message.getPolicies() == null ? Collections.emptyList() : message.getPolicies());
64         Set<ToscaPolicy> deployedPolicies =
65                 new HashSet<>(appManager.getToscaPolicies().keySet());
66
67         // Undeploy a policy
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);
72             }
73         }
74
75         // Deploy a 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);
80             }
81         }
82
83         sendPdpUpdate(state.updateInternalState(message));
84     }
85
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());
90         }
91     }
92
93 }