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;
 
  27 import javax.ws.rs.client.Entity;
 
  28 import javax.ws.rs.core.MediaType;
 
  29 import javax.ws.rs.core.Response;
 
  30 import org.onap.policy.api.PolicyParameters;
 
  31 import org.onap.policy.api.PushPolicyParameters;
 
  32 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
 
  33 import org.onap.policy.common.endpoints.http.client.HttpClient;
 
  34 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
 
  35 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
 
  36 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
 
  37 import org.onap.policy.common.parameters.ParameterService;
 
  38 import org.onap.policy.distribution.forwarding.PolicyForwarder;
 
  39 import org.onap.policy.distribution.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter;
 
  40 import org.onap.policy.distribution.model.OptimizationPolicy;
 
  41 import org.onap.policy.distribution.model.Policy;
 
  42 import org.slf4j.Logger;
 
  43 import org.slf4j.LoggerFactory;
 
  44 import org.springframework.http.HttpStatus;
 
  47  * Forwards policies to the XACML PDP.
 
  49 public class XacmlPdpPolicyForwarder implements PolicyForwarder {
 
  51     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpPolicyForwarder.class);
 
  52     private static final String BASE_PATH = "pdp/api/";
 
  54     private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null;
 
  58     public void forward(final Collection<Policy> policies) {
 
  59         for (Policy policy : policies) {
 
  64     private void forward(Policy policy) {
 
  65         XacmlPdpPolicyAdapter<?> policyAdapter = getXacmlPdpPolicyAdapter(policy);
 
  67         if (policyAdapter == null) {
 
  68             LOGGER.error("Cannot forward policy {}. Unsupported policy type {}",
 
  69                     policy, policy.getClass().getSimpleName());
 
  73         boolean policyCreated = createPolicy(policyAdapter);
 
  75             pushPolicy(policyAdapter);
 
  79     private XacmlPdpPolicyAdapter<?> getXacmlPdpPolicyAdapter(Policy policy) {
 
  80         if (policy instanceof OptimizationPolicy) {
 
  81             return new XacmlPdpOptimizationPolicyAdapter((OptimizationPolicy) policy);
 
  86     private boolean createPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
 
  87         PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters();
 
  88         Entity<PolicyParameters> entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON);
 
  90         return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getPolicyName());
 
  93     private boolean pushPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
 
  94         PushPolicyParameters pushPolicyParameters =
 
  95                 policyAdapter.getAsPushPolicyParameters(configurationParameters.getPdpGroup());
 
  96         Entity<PushPolicyParameters> entity = Entity.entity(pushPolicyParameters, MediaType.APPLICATION_JSON);
 
  98         return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getPolicyName());
 
 101     private boolean invokeHttpClient(final Entity<?> entity, final String method, final String policyName) {
 
 104             Response response = getHttpClient().put(method, entity,
 
 105                     Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth()));
 
 107             if (response.getStatus() != HttpStatus.OK.value()) {
 
 109                         "Invocation of method {} failed for policy {}. Response status: {}, Response status info: {}",
 
 110                         method, policyName, response.getStatus(), response.getStatusInfo());
 
 113         } catch (HttpClientConfigException exception) {
 
 114             LOGGER.error("Invocation of method " + method + " failed for policy " + policyName
 
 115                     + " due to error opening Http client", exception);
 
 121     private HttpClient getHttpClient() throws HttpClientConfigException {
 
 122         boolean useHttps = configurationParameters.isUseHttps();
 
 123         String hostname = configurationParameters.getHostname();
 
 124         int port = configurationParameters.getPort();
 
 125         String userName = configurationParameters.getUserName();
 
 126         String password = configurationParameters.getPassword();
 
 127         boolean managed = configurationParameters.isManaged();
 
 128         BusTopicParams params = BusTopicParams.builder().clientName("SDC Dist").useHttps(useHttps).hostname(hostname)
 
 129                 .port(port).userName(userName).password(password).basePath(BASE_PATH).managed(managed).build();
 
 130         return getHttpClientFactory().build(params);
 
 134     public void configure(String parameterGroupName) {
 
 135         configurationParameters = ParameterService.get(parameterGroupName);
 
 138     // these may be overridden by junit tests
 
 140     protected HttpClientFactory getHttpClientFactory() {
 
 141         return HttpClientFactoryInstance.getClientFactory();