2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 AT&T Intellectual Property. 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdp.xacml.application.common;
23 import java.net.HttpURLConnection;
24 import javax.ws.rs.core.Response;
25 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
26 import org.onap.policy.common.endpoints.http.client.HttpClient;
27 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
28 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
29 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Methods to access policy-api via REST service calls.
38 public class PolicyApiCaller {
39 private static Logger logger = LoggerFactory.getLogger(PolicyApiCaller.class);
41 private static final String POLICY_TYPE_URI = "/policy/api/v1/policytypes/";
42 private static final String POLICY_TYPE_VERSION_URI = "/versions/";
44 private final HttpClient httpClient;
47 * Constructs the object.
49 * @param params target specification
50 * @throws PolicyApiException if an error occurs
52 public PolicyApiCaller(RestServerParameters params) throws PolicyApiException {
53 BusTopicParams busParams = new BusTopicParams();
54 busParams.setClientName("policy-api");
55 busParams.setHostname(params.getHost());
56 busParams.setManaged(false);
57 busParams.setPassword(params.getPassword());
58 busParams.setPort(params.getPort());
59 busParams.setUseHttps(params.isHttps());
60 busParams.setUserName(params.getUserName());
63 httpClient = makeClient(busParams);
64 } catch (HttpClientConfigException e) {
65 throw new PolicyApiException("connection to host: " + busParams.getHostname(), e);
70 * Gets a policy type from policy-api.
72 * @param type policy type of interest
73 * @return the desired policy type
74 * @throws PolicyApiException if an error occurs
76 public ToscaServiceTemplate getPolicyType(ToscaPolicyTypeIdentifier type) throws PolicyApiException {
79 Response resp = httpClient
80 .get(POLICY_TYPE_URI + type.getName() + POLICY_TYPE_VERSION_URI + type.getVersion());
82 switch (resp.getStatus()) {
83 case HttpURLConnection.HTTP_OK:
84 return resp.readEntity(ToscaServiceTemplate.class);
85 case HttpURLConnection.HTTP_NOT_FOUND:
86 logger.warn("policy-api not found {}", resp);
87 throw new NotFoundException(type.toString());
89 logger.warn("policy-api request error {}", resp);
90 throw new PolicyApiException(type.toString());
93 } catch (RuntimeException e) {
94 logger.warn("policy-api connection error");
95 throw new PolicyApiException(type.toString(), e);
99 // these methods may be overridden by junit tests
101 protected HttpClient makeClient(BusTopicParams busParams) throws HttpClientConfigException {
102 return HttpClientFactoryInstance.getClientFactory().build(busParams);