30d8f3aef5128f7b841798520109f2ebec1e6c29
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp;
22
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
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.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.common.parameters.ParameterService;
37 import org.onap.policy.distribution.forwarding.PolicyForwarder;
38 import org.onap.policy.distribution.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter;
39 import org.onap.policy.distribution.model.OptimizationPolicy;
40 import org.onap.policy.distribution.model.Policy;
41 import org.springframework.http.HttpStatus;
42
43 /**
44  * Forwards policies to the XACML PDP.
45  */
46 public class XacmlPdpPolicyForwarder implements PolicyForwarder {
47
48     private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class);
49     private static final String BASE_PATH = "pdp/api/";
50
51     private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null;
52
53
54     @Override
55     public void forward(final Collection<Policy> policies) {
56         for (Policy policy : policies) {
57             forward(policy);
58         }
59     }
60
61     private void forward(Policy policy) {
62         XacmlPdpPolicyAdapter<?> policyAdapter = getXacmlPdpPolicyAdapter(policy);
63
64         if (policyAdapter == null) {
65             LOGGER.error("Cannot forward policy " + policy + ". Unsupported policy type "
66                     + policy.getClass().getSimpleName());
67             return;
68         }
69
70         boolean policyCreated = createPolicy(policyAdapter);
71         if (policyCreated) {
72             pushPolicy(policyAdapter);
73         }
74     }
75
76     private XacmlPdpPolicyAdapter<?> getXacmlPdpPolicyAdapter(Policy policy) {
77         if (policy instanceof OptimizationPolicy) {
78             return new XacmlPdpOptimizationPolicyAdapter((OptimizationPolicy) policy);
79         }
80         return null;
81     }
82
83     private boolean createPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
84         PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters();
85         Entity<PolicyParameters> entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON);
86
87         return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getPolicyName());
88     }
89
90     private boolean pushPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
91         PushPolicyParameters pushPolicyParameters =
92                 policyAdapter.getAsPushPolicyParameters(configurationParameters.getPdpGroup());
93         Entity<PushPolicyParameters> entity = Entity.entity(pushPolicyParameters, MediaType.APPLICATION_JSON);
94
95         return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getPolicyName());
96     }
97
98     private boolean invokeHttpClient(final Entity<?> entity, final String method, final String policyName) {
99
100         try {
101             Response response = getHttpClient().put(method, entity,
102                     Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth()));
103
104             if (response.getStatus() != HttpStatus.OK.value()) {
105                 LOGGER.error(
106                         "Invocation of method " + method + " failed for policy " + policyName + ". Response status: "
107                                 + response.getStatus() + ", Response status info: " + response.getStatusInfo());
108                 return false;
109             }
110         } catch (KeyManagementException | NoSuchAlgorithmException exception) {
111             LOGGER.error("Invocation of method " + method + " failed for policy " + policyName
112                     + " due to error opening Http client", exception);
113             return false;
114         }
115         return true;
116     }
117
118     private HttpClient getHttpClient() throws KeyManagementException, NoSuchAlgorithmException {
119         boolean useHttps = configurationParameters.isUseHttps();
120         String hostname = configurationParameters.getHostname();
121         int port = configurationParameters.getPort();
122         String userName = configurationParameters.getUserName();
123         String password = configurationParameters.getPassword();
124         boolean managed = configurationParameters.isManaged();
125         BusTopicParams params = BusTopicParams.builder().clientName("SDC Dist").useHttps(useHttps).hostname(hostname)
126                 .port(port).userName(userName).password(password).basePath(BASE_PATH).managed(managed).build();
127         return HttpClient.factory.build(params);
128     }
129
130     @Override
131     public void configure(String parameterGroupName) {
132         configurationParameters = ParameterService.get(parameterGroupName);
133     }
134
135 }