bump poms to 2.1.4-SNAPSHOT
[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 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdp.xacml.application.common;
22
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.common.gson.GsonMessageBodyHandler;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
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.setSerializationProvider(GsonMessageBodyHandler.class.getName());
61         busParams.setUseHttps(params.isHttps());
62         busParams.setUserName(params.getUserName());
63
64         try {
65             httpClient = makeClient(busParams);
66         } catch (HttpClientConfigException e) {
67             throw new PolicyApiException("connection to host: " + busParams.getHostname(), e);
68         }
69     }
70
71     /**
72      * Gets a policy type from policy-api.
73      *
74      * @param type policy type of interest
75      * @return the desired policy type
76      * @throws PolicyApiException if an error occurs
77      */
78     public ToscaPolicyType getPolicyType(ToscaPolicyTypeIdentifier type) throws PolicyApiException {
79
80         try {
81             Response resp = httpClient
82                             .get(POLICY_TYPE_URI + type.getName() + POLICY_TYPE_VERSION_URI + type.getVersion());
83
84             switch (resp.getStatus()) {
85                 case HttpURLConnection.HTTP_OK:
86                     return resp.readEntity(ToscaPolicyType.class);
87                 case HttpURLConnection.HTTP_NOT_FOUND:
88                     logger.warn("policy-api not found {}", resp);
89                     throw new NotFoundException(type.toString());
90                 default:
91                     logger.warn("policy-api request error {}", resp);
92                     throw new PolicyApiException(type.toString());
93             }
94
95         } catch (RuntimeException e) {
96             logger.warn("policy-api connection error");
97             throw new PolicyApiException(type.toString(), e);
98         }
99     }
100
101     // these methods may be overridden by junit tests
102
103     protected HttpClient makeClient(BusTopicParams busParams) throws HttpClientConfigException {
104         return HttpClientFactoryInstance.getClientFactory().build(busParams);
105     }
106 }