8eeeda7ed645c732902484e1de74abbb7f0505e3
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Intel Corp. All rights reserved.
5  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.forwarding.xacml.pdp;
24
25 import java.util.Collection;
26 import java.util.Collections;
27
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import org.onap.policy.api.PolicyParameters;
33 import org.onap.policy.api.PushPolicyParameters;
34 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
35 import org.onap.policy.common.endpoints.http.client.HttpClient;
36 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
37 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
38 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
39 import org.onap.policy.common.parameters.ParameterService;
40 import org.onap.policy.distribution.forwarding.PolicyForwarder;
41 import org.onap.policy.distribution.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.http.HttpStatus;
47
48 /**
49  * Forwards policies to the XACML PDP.
50  */
51 public class XacmlPdpPolicyForwarder implements PolicyForwarder {
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpPolicyForwarder.class);
54     private static final String BASE_PATH = "pdp/api/";
55
56     private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null;
57
58
59     @Override
60     public void forward(final Collection<ToscaEntity> policies) {
61         for (final ToscaEntity policy : policies) {
62             forward(policy);
63         }
64     }
65
66     private void forward(final ToscaEntity policy) {
67         final XacmlPdpPolicyAdapter<?> policyAdapter = getXacmlPdpPolicyAdapter(policy);
68
69         if (policyAdapter == null) {
70             LOGGER.error("Cannot forward policy {}. Unsupported policy type {}", policy,
71                     policy.getClass().getSimpleName());
72             return;
73         }
74
75         final boolean policyCreated = createPolicy(policyAdapter);
76         if (policyCreated) {
77             pushPolicy(policyAdapter);
78         }
79     }
80
81     private XacmlPdpPolicyAdapter<?> getXacmlPdpPolicyAdapter(final ToscaEntity policy) {
82         if (policy instanceof ToscaPolicy) {
83             return new XacmlPdpOptimizationPolicyAdapter((ToscaPolicy) policy);
84         }
85         return null;
86     }
87
88     private boolean createPolicy(final XacmlPdpPolicyAdapter<?> policyAdapter) {
89         final PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters();
90         final Entity<PolicyParameters> entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON);
91
92         return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getName());
93     }
94
95     private boolean pushPolicy(final XacmlPdpPolicyAdapter<?> policyAdapter) {
96         final PushPolicyParameters pushPolicyParameters =
97                 policyAdapter.getAsPushPolicyParameters(configurationParameters.getPdpGroup());
98         final Entity<PushPolicyParameters> entity = Entity.entity(pushPolicyParameters, MediaType.APPLICATION_JSON);
99
100         return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getName());
101     }
102
103     private boolean invokeHttpClient(final Entity<?> entity, final String method, final String policyName) {
104
105         try {
106             final Response response = getHttpClient().put(method, entity,
107                     Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth()));
108
109             if (response.getStatus() != HttpStatus.OK.value()) {
110                 LOGGER.error(
111                         "Invocation of method {} failed for policy {}. Response status: {}, Response status info: {}",
112                         method, policyName, response.getStatus(), response.getStatusInfo());
113                 return false;
114             }
115         } catch (final HttpClientConfigException exception) {
116             LOGGER.error("Invocation of method " + method + " failed for policy " + policyName
117                     + " due to error opening Http client", exception);
118             return false;
119         }
120         return true;
121     }
122
123     private HttpClient getHttpClient() throws HttpClientConfigException {
124         final boolean useHttps = configurationParameters.isUseHttps();
125         final String hostname = configurationParameters.getHostname();
126         final int port = configurationParameters.getPort();
127         final String userName = configurationParameters.getUserName();
128         final String password = configurationParameters.getPassword();
129         final boolean managed = configurationParameters.isManaged();
130         final BusTopicParams params =
131                 BusTopicParams.builder().clientName("SDC Dist").useHttps(useHttps).hostname(hostname).port(port)
132                         .userName(userName).password(password).basePath(BASE_PATH).managed(managed).build();
133         return getHttpClientFactory().build(params);
134     }
135
136     @Override
137     public void configure(final String parameterGroupName) {
138         configurationParameters = ParameterService.get(parameterGroupName);
139     }
140
141     // these may be overridden by junit tests
142
143     protected HttpClientFactory getHttpClientFactory() {
144         return HttpClientFactoryInstance.getClientFactory();
145     }
146 }