Changed identifiers to concept identifiers
[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-2020 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.common.endpoints.parameters.RestServerParameters;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Methods to access policy-api via REST service calls.
38  */
39 public class PolicyApiCaller {
40     private static Logger logger = LoggerFactory.getLogger(PolicyApiCaller.class);
41
42     private static final String POLICY_TYPE_URI = "/policy/api/v1/policytypes/";
43     private static final String POLICY_TYPE_VERSION_URI = "/versions/";
44
45     private final HttpClient httpClient;
46
47     /**
48      * Constructs the object.
49      *
50      * @param params target specification
51      * @throws PolicyApiException if an error occurs
52      */
53     public PolicyApiCaller(RestServerParameters params) throws PolicyApiException {
54         BusTopicParams busParams = new BusTopicParams();
55         busParams.setClientName("policy-api");
56         busParams.setHostname(params.getHost());
57         busParams.setManaged(false);
58         busParams.setPassword(params.getPassword());
59         busParams.setPort(params.getPort());
60         busParams.setUseHttps(params.isHttps());
61         busParams.setUserName(params.getUserName());
62
63         try {
64             httpClient = makeClient(busParams);
65         } catch (HttpClientConfigException e) {
66             throw new PolicyApiException("connection to host: " + busParams.getHostname(), e);
67         }
68     }
69
70     /**
71      * Gets a policy type from policy-api.
72      *
73      * @param type policy type of interest
74      * @return the desired policy type
75      * @throws PolicyApiException if an error occurs
76      */
77     public ToscaServiceTemplate getPolicyType(ToscaConceptIdentifier type) throws PolicyApiException {
78
79         try {
80             Response resp = httpClient
81                             .get(POLICY_TYPE_URI + type.getName() + POLICY_TYPE_VERSION_URI + type.getVersion());
82
83             switch (resp.getStatus()) {
84                 case HttpURLConnection.HTTP_OK:
85                     return resp.readEntity(ToscaServiceTemplate.class);
86                 case HttpURLConnection.HTTP_NOT_FOUND:
87                     logger.warn("policy-api not found {}", resp);
88                     throw new NotFoundException(type.toString());
89                 default:
90                     logger.warn("policy-api request error {}", resp);
91                     throw new PolicyApiException(type.toString());
92             }
93
94         } catch (RuntimeException e) {
95             logger.warn("policy-api connection error");
96             throw new PolicyApiException(type.toString(), e);
97         }
98     }
99
100     // these methods may be overridden by junit tests
101
102     protected HttpClient makeClient(BusTopicParams busParams) throws HttpClientConfigException {
103         return HttpClientFactoryInstance.getClientFactory().build(busParams);
104     }
105 }