Added logger
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefclient / impl / ChefApiClientImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2019 IBM
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.chef.chefclient.impl;
26
27 import java.io.IOException;
28 import java.net.URISyntaxException;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import org.apache.http.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.HttpStatus;
35 import org.apache.http.client.HttpClient;
36 import org.apache.http.util.EntityUtils;
37 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
38 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
39 import org.onap.appc.adapter.chef.chefclient.impl.ChefRequestBuilder.OngoingRequestBuilder;
40
41 public class ChefApiClientImpl implements ChefApiClient {
42
43     private final HttpClient httpClient;
44     private final String endpoint;
45     private final String organization;
46     private final HttpHeaderFactory httpHeaderFactory;
47     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefApiClientImpl.class);
48     public ChefApiClientImpl(HttpClient httpClient, String endpoint, String organization,
49             HttpHeaderFactory httpHeaderFactory) {
50         this.httpClient = httpClient;
51         this.endpoint = endpoint;
52         this.organization = organization;
53         this.httpHeaderFactory = httpHeaderFactory;
54     }
55
56     @Override
57     public ChefResponse get(String path) {
58         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
59             .httpGet()
60             .withPath(getPath(path))
61             .withHeaders(httpHeaderFactory.create("GET", path, ""));
62         return execute(requestBuilder);
63     }
64
65     @Override
66     public ChefResponse delete(String path) {
67         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
68             .httpDelete()
69             .withPath(getPath(path))
70             .withHeaders(httpHeaderFactory.create("DELETE", path, ""));
71         return execute(requestBuilder);
72     }
73
74     @Override
75     public ChefResponse post(String path, String body) {
76         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
77             .httpPost(body)
78             .withPath(getPath(path))
79             .withHeaders(httpHeaderFactory.create("POST", path, body));
80         return execute(requestBuilder);
81     }
82
83     @Override
84     public ChefResponse put(String path, String body) {
85         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
86             .httpPut(body)
87             .withPath(getPath(path))
88             .withHeaders(httpHeaderFactory.create("PUT", path, body));
89         logger.info("request: PATH: "+path+ " body: "+body);
90         return execute(requestBuilder);
91     }
92
93     private ChefResponse execute(OngoingRequestBuilder chefRequest) {
94         try {
95             if (httpClient == null) {
96                 return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Could not create http client for chef");
97             }
98             HttpResponse response = httpClient.execute(chefRequest.build());
99             int statusCode = response.getStatusLine().getStatusCode();
100             HttpEntity httpEntity = response.getEntity();
101             String responseBody = EntityUtils.toString(httpEntity);
102             return ChefResponse.create(statusCode, responseBody);
103         } catch (IOException ex) {
104             logger.error("Error occured while reading response", ex.getMessage(), ex);
105             return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
106         } catch (URISyntaxException ex) {
107             logger.error("Malformed URL", ex.getMessage(), ex);
108             return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
109         }
110     }
111
112     private String getPath(String path) {
113         return "/organizations/" + organization + path;
114     }
115 }
116