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
index b26f69a..f86b490 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * =============================================================================
@@ -17,8 +17,6 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  * ============LICENSE_END=========================================================
  */
 
@@ -26,6 +24,9 @@ package org.onap.appc.adapter.chef.chefclient.impl;
 
 import java.io.IOException;
 import java.net.URISyntaxException;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
@@ -38,28 +39,24 @@ import org.onap.appc.adapter.chef.chefclient.impl.ChefRequestBuilder.OngoingRequ
 public class ChefApiClientImpl implements ChefApiClient {
 
     private final HttpClient httpClient;
-    private final ChefApiHeaderFactory chefApiHeaderFactory;
-    private String endpoint;
-    private String userId;
-    private String pemPath;
-    private String organizations;
-
-    public ChefApiClientImpl(HttpClient httpClient, ChefApiHeaderFactory chefApiHeaderFactory,
-        String endpoint, String organizations, String userId, String pemPath) {
+    private final String endpoint;
+    private final String organization;
+    private final HttpHeaderFactory httpHeaderFactory;
+    private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefApiClientImpl.class);
+    public ChefApiClientImpl(HttpClient httpClient, String endpoint, String organization,
+            HttpHeaderFactory httpHeaderFactory) {
         this.httpClient = httpClient;
-        this.chefApiHeaderFactory = chefApiHeaderFactory;
         this.endpoint = endpoint;
-        this.organizations = organizations;
-        this.userId = userId;
-        this.pemPath = pemPath;
+        this.organization = organization;
+        this.httpHeaderFactory = httpHeaderFactory;
     }
 
     @Override
     public ChefResponse get(String path) {
         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
             .httpGet()
-            .withPath(path)
-            .withHeaders(chefApiHeaderFactory.create("GET", path, "", userId, organizations, pemPath));
+            .withPath(getPath(path))
+            .withHeaders(httpHeaderFactory.create("GET", path, ""));
         return execute(requestBuilder);
     }
 
@@ -67,8 +64,8 @@ public class ChefApiClientImpl implements ChefApiClient {
     public ChefResponse delete(String path) {
         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
             .httpDelete()
-            .withPath(path)
-            .withHeaders(chefApiHeaderFactory.create("DELETE", path, "", userId, organizations, pemPath));
+            .withPath(getPath(path))
+            .withHeaders(httpHeaderFactory.create("DELETE", path, ""));
         return execute(requestBuilder);
     }
 
@@ -76,8 +73,8 @@ public class ChefApiClientImpl implements ChefApiClient {
     public ChefResponse post(String path, String body) {
         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
             .httpPost(body)
-            .withPath(path)
-            .withHeaders(chefApiHeaderFactory.create("POST", path, body, userId, organizations, pemPath));
+            .withPath(getPath(path))
+            .withHeaders(httpHeaderFactory.create("POST", path, body));
         return execute(requestBuilder);
     }
 
@@ -85,13 +82,17 @@ public class ChefApiClientImpl implements ChefApiClient {
     public ChefResponse put(String path, String body) {
         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
             .httpPut(body)
-            .withPath(path)
-            .withHeaders(chefApiHeaderFactory.create("PUT", path, body, userId, organizations, pemPath));
+            .withPath(getPath(path))
+            .withHeaders(httpHeaderFactory.create("PUT", path, body));
+        logger.info("request: PATH: "+path+ " body: "+body);
         return execute(requestBuilder);
     }
 
     private ChefResponse execute(OngoingRequestBuilder chefRequest) {
         try {
+            if (httpClient == null) {
+                return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Could not create http client for chef");
+            }
             HttpResponse response = httpClient.execute(chefRequest.build());
             int statusCode = response.getStatusLine().getStatusCode();
             HttpEntity httpEntity = response.getEntity();
@@ -103,4 +104,9 @@ public class ChefApiClientImpl implements ChefApiClient {
             return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
         }
     }
+
+    private String getPath(String path) {
+        return "/organizations/" + organization + path;
+    }
 }
+