8387557c249de2ce34bc47b40dc3a132c254040e
[so.git] / cloudify-client / src / main / java / org / onap / so / cloudify / base / client / CloudifyClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.cloudify.base.client;
22
23 import java.util.Properties;
24
25 import org.onap.so.cloudify.connector.http.HttpClientConnector;
26
27 public class CloudifyClient {
28         
29         protected String managerEndpoint;
30         protected String tenant = "default_tenant";     // Note - only default_tenant supported in community edition
31         
32         protected CloudifyTokenProvider tokenProvider;
33
34         protected static int AUTHENTICATION_RETRIES = 1;
35
36         protected CloudifyClientConnector connector;
37         
38         protected Properties properties = new Properties();
39
40         public CloudifyClient(String managerEndpoint) {
41                 this.managerEndpoint = managerEndpoint;
42                 this.connector = new HttpClientConnector();
43         }
44
45         public CloudifyClient(String managerEndpoint, String tenant) {
46                 this.managerEndpoint = managerEndpoint;
47                 this.tenant = tenant;
48                 this.connector = new HttpClientConnector();
49         }
50
51         public CloudifyClient(String managerEndpoint, CloudifyClientConnector connector) {
52                 this.managerEndpoint = managerEndpoint;
53                 this.connector = connector;
54         }
55
56         /**
57          * Execute a Cloudify request by making the REST API call.  Return the
58          * complete CloudifyResponse structure, which includes the complete
59          * HTTP response.
60          * @param request a CloudifyRequest object
61          * @return a CloudifyResponse object
62          */
63         public <T> CloudifyResponse request(CloudifyRequest<T> request) {
64                 CloudifyResponseException authException = null;
65
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());
71
72                         try {
73                                 return connector.request(request);
74                         } catch (CloudifyResponseException e) {
75                                 if (e.getStatus() != CloudifyResponseStatus.NOT_AUTHORIZED
76                                                 || tokenProvider == null) {
77                                         throw e;
78                                 }
79                                 authException = e;
80                                 tokenProvider.expireToken();
81                         }
82                 }
83
84                 if (authException != null) {
85                         throw authException;
86                 }
87                 
88                 return null;
89         }
90
91         /**
92          * Execute a CloudifyRequest by sending the REST API call to the Cloudify
93          * Manager endpoint.  The return type is a JSON POJO object containing the
94          * response body entity.
95          * @param request
96          * @return a JSON POJO object specific to the request type
97          */
98         public <T> T execute(CloudifyRequest<T> request) {
99                 CloudifyResponse response =  request(request);
100                 return (request.returnType() != null && request.returnType() != Void.class) ? response.getEntity(request.returnType()) : null;
101         }
102
103         public void property(String property, String value) {
104                 properties.put(property, value);
105         }
106
107         /**
108          * Set a Token Provider.  This class should be able to produce an
109          * authentication token on-demand.
110          * @param tokenProvider
111          */
112         public void setTokenProvider(CloudifyTokenProvider tokenProvider) {
113                 this.tokenProvider = tokenProvider;
114         }
115         
116         /**
117          * Manually set the authentication token to use for this client.
118          * @param token
119          */
120         public void setToken(String token) {
121                 setTokenProvider(new CloudifySimpleTokenProvider(token));
122         }
123         
124         /**
125          * Perform a simple GET request with no request message body
126          * @param path
127          * @param returnType
128          * @return An object of Class <R>
129          */
130         public <R> CloudifyRequest<R> get(String path, Class<R> returnType) {
131                 return new CloudifyRequest<R>(this, HttpMethod.GET, path, null, returnType);
132         }
133         
134 }