2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.cloudify.base.client;
23 import java.util.Properties;
25 import org.openecomp.mso.cloudify.connector.http.HttpClientConnector;
27 public class CloudifyClient {
29 protected String managerEndpoint;
30 protected String tenant = "default_tenant"; // Note - only default_tenant supported in community edition
32 protected CloudifyTokenProvider tokenProvider;
34 protected static int AUTHENTICATION_RETRIES = 1;
36 protected CloudifyClientConnector connector;
38 protected Properties properties = new Properties();
40 public CloudifyClient(String managerEndpoint) {
41 this.managerEndpoint = managerEndpoint;
42 this.connector = new HttpClientConnector();
45 public CloudifyClient(String managerEndpoint, String tenant) {
46 this.managerEndpoint = managerEndpoint;
48 this.connector = new HttpClientConnector();
51 public CloudifyClient(String managerEndpoint, CloudifyClientConnector connector) {
52 this.managerEndpoint = managerEndpoint;
53 this.connector = connector;
57 * Execute a Cloudify request by making the REST API call. Return the
58 * complete CloudifyResponse structure, which includes the complete
60 * @param request a CloudifyRequest object
61 * @return a CloudifyResponse object
63 public <T> CloudifyResponse request(CloudifyRequest<T> request) {
64 CloudifyResponseException authException = null;
66 for (int i = 0; i <= AUTHENTICATION_RETRIES; i++) {
67 request.endpoint(managerEndpoint);
68 request.header("Tenant", tenant);
69 if (tokenProvider != null)
70 request.header("Authentication-Token", tokenProvider.getToken());
73 return connector.request(request);
74 } catch (CloudifyResponseException e) {
75 if (e.getStatus() != CloudifyResponseStatus.NOT_AUTHORIZED
76 || tokenProvider == null) {
80 tokenProvider.expireToken();
88 * Execute a CloudifyRequest by sending the REST API call to the Cloudify
89 * Manager endpoint. The return type is a JSON POJO object containing the
90 * response body entity.
92 * @return a JSON POJO object specific to the request type
94 public <T> T execute(CloudifyRequest<T> request) {
95 CloudifyResponse response = request(request);
96 return (request.returnType() != null && request.returnType() != Void.class) ? response.getEntity(request.returnType()) : null;
99 public void property(String property, String value) {
100 properties.put(property, value);
104 * Set a Token Provider. This class should be able to produce an
105 * authentication token on-demand.
106 * @param tokenProvider
108 public void setTokenProvider(CloudifyTokenProvider tokenProvider) {
109 this.tokenProvider = tokenProvider;
113 * Manually set the authentication token to use for this client.
116 public void setToken(String token) {
117 setTokenProvider(new CloudifySimpleTokenProvider(token));
121 * Perform a simple GET request with no request message body
124 * @return An object of Class <R>
126 public <R> CloudifyRequest<R> get(String path, Class<R> returnType) {
127 return new CloudifyRequest<R>(this, HttpMethod.GET, path, null, returnType);