6db566539702488aa8c724f4b8561da0d181b605
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.forwarding.xacml.pdp;
23
24 import java.security.KeyManagementException;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.Collection;
27 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;
31 import org.onap.policy.api.PolicyParameters;
32 import org.onap.policy.api.PushPolicyParameters;
33 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
34 import org.onap.policy.common.endpoints.http.client.HttpClient;
35 import org.onap.policy.common.parameters.ParameterService;
36 import org.onap.policy.distribution.forwarding.PolicyForwarder;
37 import org.onap.policy.distribution.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter;
38 import org.onap.policy.distribution.model.OptimizationPolicy;
39 import org.onap.policy.distribution.model.Policy;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.http.HttpStatus;
44
45 /**
46  * Forwards policies to the XACML PDP.
47  */
48 public class XacmlPdpPolicyForwarder implements PolicyForwarder {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpPolicyForwarder.class);
51     private static final String BASE_PATH = "pdp/api/";
52
53     private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null;
54
55
56     @Override
57     public void forward(final Collection<Policy> policies) {
58         for (Policy policy : policies) {
59             forward(policy);
60         }
61     }
62
63     private void forward(Policy policy) {
64         XacmlPdpPolicyAdapter<?> policyAdapter = getXacmlPdpPolicyAdapter(policy);
65
66         if (policyAdapter == null) {
67             LOGGER.error("Cannot forward policy {}. Unsupported policy type {}",
68                     policy, policy.getClass().getSimpleName());
69             return;
70         }
71
72         boolean policyCreated = createPolicy(policyAdapter);
73         if (policyCreated) {
74             pushPolicy(policyAdapter);
75         }
76     }
77
78     private XacmlPdpPolicyAdapter<?> getXacmlPdpPolicyAdapter(Policy policy) {
79         if (policy instanceof OptimizationPolicy) {
80             return new XacmlPdpOptimizationPolicyAdapter((OptimizationPolicy) policy);
81         }
82         return null;
83     }
84
85     private boolean createPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
86         PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters();
87         Entity<PolicyParameters> entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON);
88
89         return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getPolicyName());
90     }
91
92     private boolean pushPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) {
93         PushPolicyParameters pushPolicyParameters =
94                 policyAdapter.getAsPushPolicyParameters(configurationParameters.getPdpGroup());
95         Entity<PushPolicyParameters> entity = Entity.entity(pushPolicyParameters, MediaType.APPLICATION_JSON);
96
97         return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getPolicyName());
98     }
99
100     private boolean invokeHttpClient(final Entity<?> entity, final String method, final String policyName) {
101
102         try {
103             Response response = getHttpClient().put(method, entity,
104                     Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth()));
105
106             if (response.getStatus() != HttpStatus.OK.value()) {
107                 LOGGER.error(
108                         "Invocation of method {} failed for policy {}. Response status: {}, Response status info: {}",
109                         method, policyName, response.getStatus(), response.getStatusInfo());
110                 return false;
111             }
112         } catch (KeyManagementException | NoSuchAlgorithmException | ClassNotFoundException exception) {
113             LOGGER.error("Invocation of method " + method + " failed for policy " + policyName
114                     + " due to error opening Http client", exception);
115             return false;
116         }
117         return true;
118     }
119
120     private HttpClient getHttpClient() 
121             throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
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 HttpClient.factory.build(params);
131     }
132
133     @Override
134     public void configure(String parameterGroupName) {
135         configurationParameters = ParameterService.get(parameterGroupName);
136     }
137
138 }