2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021,2023 Nordix Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdp.xacml.application.common;
24 import jakarta.ws.rs.core.Response;
25 import java.net.HttpURLConnection;
26 import org.onap.policy.common.endpoints.http.client.HttpClient;
27 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Methods to access policy-api via REST service calls.
35 public class PolicyApiCaller {
36 private static Logger logger = LoggerFactory.getLogger(PolicyApiCaller.class);
38 private static final String POLICY_TYPE_URI = "/policy/api/v1/policytypes/";
39 private static final String POLICY_TYPE_VERSION_URI = "/versions/";
41 private final HttpClient httpClient;
44 * Constructs the object.
46 * @param httpClient API REST client
48 public PolicyApiCaller(HttpClient httpClient) {
49 this.httpClient = httpClient;
53 * Gets a policy type from policy-api.
55 * @param type policy type of interest
56 * @return the desired policy type
57 * @throws PolicyApiException if an error occurs
59 public ToscaServiceTemplate getPolicyType(ToscaConceptIdentifier type) throws PolicyApiException {
62 Response resp = httpClient
63 .get(POLICY_TYPE_URI + type.getName() + POLICY_TYPE_VERSION_URI + type.getVersion());
65 switch (resp.getStatus()) {
66 case HttpURLConnection.HTTP_OK:
67 return resp.readEntity(ToscaServiceTemplate.class);
68 case HttpURLConnection.HTTP_NOT_FOUND:
69 logger.warn("policy-api not found {}", resp);
70 throw new NotFoundException(type.toString());
72 logger.warn("policy-api request error {}", resp);
73 throw new PolicyApiException(type.toString());
76 } catch (RuntimeException e) {
77 logger.warn("policy-api connection error, client info: {} ", httpClient);
78 throw new PolicyApiException(type.toString(), e);