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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.distribution.forwarding.xacml.pdp;
25 import java.util.Collection;
26 import java.util.Collections;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
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;
49 * Forwards policies to the XACML PDP.
51 public class XacmlPdpPolicyForwarder implements PolicyForwarder {
53 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpPolicyForwarder.class);
54 private static final String BASE_PATH = "pdp/api/";
56 private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null;
60 public void forward(final Collection<ToscaEntity> policies) {
61 for (final ToscaEntity policy : policies) {
66 private void forward(final ToscaEntity policy) {
67 final XacmlPdpPolicyAdapter<?> policyAdapter = getXacmlPdpPolicyAdapter(policy);
69 if (policyAdapter == null) {
70 LOGGER.error("Cannot forward policy {}. Unsupported policy type {}", policy,
71 policy.getClass().getSimpleName());
75 final boolean policyCreated = createPolicy(policyAdapter);
77 pushPolicy(policyAdapter);
81 private XacmlPdpPolicyAdapter<?> getXacmlPdpPolicyAdapter(final ToscaEntity policy) {
82 if (policy instanceof ToscaPolicy) {
83 return new XacmlPdpOptimizationPolicyAdapter((ToscaPolicy) policy);
88 private boolean createPolicy(final XacmlPdpPolicyAdapter<?> policyAdapter) {
89 final PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters();
90 final Entity<PolicyParameters> entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON);
92 return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getName());
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);
100 return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getName());
103 private boolean invokeHttpClient(final Entity<?> entity, final String method, final String policyName) {
106 final Response response = getHttpClient().put(method, entity,
107 Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth()));
109 if (response.getStatus() != HttpStatus.OK.value()) {
111 "Invocation of method {} failed for policy {}. Response status: {}, Response status info: {}",
112 method, policyName, response.getStatus(), response.getStatusInfo());
115 } catch (final HttpClientConfigException exception) {
116 LOGGER.error("Invocation of method " + method + " failed for policy " + policyName
117 + " due to error opening Http client", exception);
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);
137 public void configure(final String parameterGroupName) {
138 configurationParameters = ParameterService.get(parameterGroupName);
141 // these may be overridden by junit tests
143 protected HttpClientFactory getHttpClientFactory() {
144 return HttpClientFactoryInstance.getClientFactory();