Change RestServerParameters to BusTopicParams
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / PolicyApiCaller.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2021 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
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.pdp.xacml.application.common;
23
24 import java.net.HttpURLConnection;
25 import javax.ws.rs.core.Response;
26 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
27 import org.onap.policy.common.endpoints.http.client.HttpClient;
28 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
29 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Methods to access policy-api via REST service calls.
37  */
38 public class PolicyApiCaller {
39     private static Logger logger = LoggerFactory.getLogger(PolicyApiCaller.class);
40
41     private static final String POLICY_TYPE_URI = "/policy/api/v1/policytypes/";
42     private static final String POLICY_TYPE_VERSION_URI = "/versions/";
43
44     private final HttpClient httpClient;
45
46     /**
47      * Constructs the object.
48      *
49      * @param params target specification
50      * @throws PolicyApiException if an error occurs
51      */
52     public PolicyApiCaller(BusTopicParams params) throws PolicyApiException {
53         try {
54             params.setClientName("policy-api");
55             httpClient = makeClient(params);
56         } catch (HttpClientConfigException e) {
57             throw new PolicyApiException("connection to host: " + params.getHostname(), e);
58         }
59     }
60
61     /**
62      * Gets a policy type from policy-api.
63      *
64      * @param type policy type of interest
65      * @return the desired policy type
66      * @throws PolicyApiException if an error occurs
67      */
68     public ToscaServiceTemplate getPolicyType(ToscaConceptIdentifier type) throws PolicyApiException {
69
70         try {
71             Response resp = httpClient
72                             .get(POLICY_TYPE_URI + type.getName() + POLICY_TYPE_VERSION_URI + type.getVersion());
73
74             switch (resp.getStatus()) {
75                 case HttpURLConnection.HTTP_OK:
76                     return resp.readEntity(ToscaServiceTemplate.class);
77                 case HttpURLConnection.HTTP_NOT_FOUND:
78                     logger.warn("policy-api not found {}", resp);
79                     throw new NotFoundException(type.toString());
80                 default:
81                     logger.warn("policy-api request error {}", resp);
82                     throw new PolicyApiException(type.toString());
83             }
84
85         } catch (RuntimeException e) {
86             logger.warn("policy-api connection error, client info: {} ", httpClient);
87             throw new PolicyApiException(type.toString(), e);
88         }
89     }
90
91     // these methods may be overridden by junit tests
92
93     protected HttpClient makeClient(BusTopicParams busParams) throws HttpClientConfigException {
94         return HttpClientFactoryInstance.getClientFactory().build(busParams);
95     }
96 }