196818b5c255321657ba9e2587296921dcf2c9e4
[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 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;
45
46 /**
47  * Forwards policies to the XACML PDP.
48  */
49 public class XacmlPdpPolicyForwarder implements PolicyForwarder {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpPolicyForwarder.class);
52     private static final String BASE_PATH = "pdp/api/";
53
54     private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null;
55
56
57     @Override
58     public void forward(final Collection<Policy> policies) {
59         for (Policy policy : policies) {
60             forward(policy);
61         }
62     }
63
64     private void forward(Policy policy) {
65         XacmlPdpPolicyAdapter<?> policyAdapter = getXacmlPdpPolicyAdapter(policy);
66
67         if (policyAdapter == null) {
68             LOGGER.error("Cannot forward policy {}. Unsupported policy type {}",
69                     policy, policy.getClass().getSimpleName());
70             return;
71         }
72
73         boolean policyCreated = createPolicy(policyAdapter);
74         if (policyCreated) {
75             pushPolicy(policyAdapter);
76         }
77     }
78
79     private XacmlPdpPolicyAdapter<?> getXacmlPdpPolicyAdapter(Policy policy) {
80         if (policy instanceof OptimizationPolicy) {
81             return new XacmlPdpOptimizationPolicyAdapter((OptimizationPolicy) policy);
82         }
83         return null;
84     }
85
86     private boolean createPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
87         PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters();
88         Entity<PolicyParameters> entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON);
89
90         return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getPolicyName());
91     }
92
93     private boolean pushPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
94         PushPolicyParameters pushPolicyParameters =
95                 policyAdapter.getAsPushPolicyParameters(configurationParameters.getPdpGroup());
96         Entity<PushPolicyParameters> entity = Entity.entity(pushPolicyParameters, MediaType.APPLICATION_JSON);
97
98         return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getPolicyName());
99     }
100
101     private boolean invokeHttpClient(final Entity<?> entity, final String method, final String policyName) {
102
103         try {
104             Response response = getHttpClient().put(method, entity,
105                     Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth()));
106
107             if (response.getStatus() != HttpStatus.OK.value()) {
108                 LOGGER.error(
109                         "Invocation of method {} failed for policy {}. Response status: {}, Response status info: {}",
110                         method, policyName, response.getStatus(), response.getStatusInfo());
111                 return false;
112             }
113         } catch (HttpClientConfigException exception) {
114             LOGGER.error("Invocation of method " + method + " failed for policy " + policyName
115                     + " due to error opening Http client", exception);
116             return false;
117         }
118         return true;
119     }
120
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);
131     }
132
133     @Override
134     public void configure(String parameterGroupName) {
135         configurationParameters = ParameterService.get(parameterGroupName);
136     }
137
138     // these may be overridden by junit tests
139
140     protected HttpClientFactory getHttpClientFactory() {
141         return HttpClientFactoryInstance.getClientFactory();
142     }
143 }