43326e7258927a57ab6841af681be40bf36eaa4b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
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.clamp.controlloop.participant.dcae.httpclient;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.Collections;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.Response.Status;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
31 import org.onap.policy.common.endpoints.http.client.HttpClient;
32 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public abstract class AbstractHttpClient implements Closeable {
39
40     private static final String MSG_REQUEST_FAILED = "request to {} failed";
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class);
43     private final HttpClient httpclient;
44     public static final Coder CODER = new StandardCoder();
45
46     /**
47      * Constructor.
48      *
49      * @param restClientParameters the REST client parameters
50      * @throws ControlLoopRuntimeException on errors
51      */
52     protected AbstractHttpClient(BusTopicParams restClientParameters) {
53         try {
54             httpclient = HttpClientFactoryInstance.getClientFactory().build(restClientParameters);
55         } catch (final Exception e) {
56             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
57                     restClientParameters.getClientName() + " Client failed to start", e);
58         }
59     }
60
61     protected boolean executePut(String path, String jsonEntity, int statusCode) {
62         try {
63             var response = httpclient.put(path, Entity.json(jsonEntity), Collections.emptyMap());
64             return response.getStatus() == statusCode;
65         } catch (Exception e) {
66             LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
67             return false;
68         }
69     }
70
71     protected boolean executePut(String path, int statusCode) {
72         try {
73             var response = httpclient.put(path, Entity.json(""), Collections.emptyMap());
74             return response.getStatus() == statusCode;
75         } catch (Exception e) {
76             LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
77             return false;
78         }
79     }
80
81     protected Loop executePost(String path, int statusCode) {
82         try {
83             var response = httpclient.post(path, Entity.json(""), Collections.emptyMap());
84             if (response.getStatus() != statusCode) {
85                 return null;
86             }
87             return response.readEntity(Loop.class);
88         } catch (Exception e) {
89             LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
90             return null;
91         }
92     }
93
94     protected Loop executeGet(String path, int statusCode) {
95         try {
96             var response = httpclient.get(path);
97
98             if (response.getStatus() != statusCode) {
99                 return null;
100             }
101             return response.readEntity(Loop.class);
102         } catch (Exception e) {
103             LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
104             return null;
105         }
106     }
107
108     @Override
109     public void close() throws IOException {
110         httpclient.shutdown();
111     }
112 }