AT&T 1712 and 1802 release code
[so.git] / cloudify-client / src / main / java / org / openecomp / mso / 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.openecomp.mso.cloudify.base.client;
22
23 import java.util.Properties;
24
25 import org.openecomp.mso.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                 throw authException;
85         }
86
87         /**
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.
91          * @param request
92          * @return a JSON POJO object specific to the request type
93          */
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;
97         }
98
99         public void property(String property, String value) {
100                 properties.put(property, value);
101         }
102
103         /**
104          * Set a Token Provider.  This class should be able to produce an
105          * authentication token on-demand.
106          * @param tokenProvider
107          */
108         public void setTokenProvider(CloudifyTokenProvider tokenProvider) {
109                 this.tokenProvider = tokenProvider;
110         }
111         
112         /**
113          * Manually set the authentication token to use for this client.
114          * @param token
115          */
116         public void setToken(String token) {
117                 setTokenProvider(new CloudifySimpleTokenProvider(token));
118         }
119         
120         /**
121          * Perform a simple GET request with no request message body
122          * @param path
123          * @param returnType
124          * @return An object of Class <R>
125          */
126         public <R> CloudifyRequest<R> get(String path, Class<R> returnType) {
127                 return new CloudifyRequest<R>(this, HttpMethod.GET, path, null, returnType);
128         }
129         
130 }