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