Use default serialization provider
[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  * ================================================================================
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.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;
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(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());
61
62         try {
63             httpClient = makeClient(busParams);
64         } catch (HttpClientConfigException e) {
65             throw new PolicyApiException("connection to host: " + busParams.getHostname(), e);
66         }
67     }
68
69     /**
70      * Gets a policy type from policy-api.
71      *
72      * @param type policy type of interest
73      * @return the desired policy type
74      * @throws PolicyApiException if an error occurs
75      */
76     public ToscaServiceTemplate getPolicyType(ToscaPolicyTypeIdentifier type) throws PolicyApiException {
77
78         try {
79             Response resp = httpClient
80                             .get(POLICY_TYPE_URI + type.getName() + POLICY_TYPE_VERSION_URI + type.getVersion());
81
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());
88                 default:
89                     logger.warn("policy-api request error {}", resp);
90                     throw new PolicyApiException(type.toString());
91             }
92
93         } catch (RuntimeException e) {
94             logger.warn("policy-api connection error");
95             throw new PolicyApiException(type.toString(), e);
96         }
97     }
98
99     // these methods may be overridden by junit tests
100
101     protected HttpClient makeClient(BusTopicParams busParams) throws HttpClientConfigException {
102         return HttpClientFactoryInstance.getClientFactory().build(busParams);
103     }
104 }