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