AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / client / src / main / java / org / onap / aaf / cadi / http / HMangr.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.cadi.http;
23
24 import java.net.ConnectException;
25 import java.net.HttpURLConnection;
26 import java.net.SocketException;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29
30 import javax.net.ssl.SSLHandshakeException;
31
32 import org.onap.aaf.cadi.Access;
33 import org.onap.aaf.cadi.CadiException;
34 import org.onap.aaf.cadi.Locator;
35 import org.onap.aaf.cadi.LocatorException;
36 import org.onap.aaf.cadi.SecuritySetter;
37 import org.onap.aaf.cadi.Access.Level;
38 import org.onap.aaf.cadi.Locator.Item;
39 import org.onap.aaf.cadi.client.Rcli;
40 import org.onap.aaf.cadi.client.Retryable;
41 import org.onap.aaf.misc.env.APIException;
42
43 public class HMangr {
44         private String apiVersion;
45         private int readTimeout, connectionTimeout;
46         public final Locator<URI> loc;
47         private Access access;
48         
49         public HMangr(Access access, Locator<URI> loc) throws LocatorException {
50                 readTimeout = 10000;
51                 connectionTimeout=3000;
52                 if(loc ==  null) {
53                         throw new LocatorException("Null Locator passed");
54                 }
55                 this.loc = loc;
56                 this.access = access;
57         }
58
59         /**
60          * Reuse the same service.  This is helpful for multiple calls that change service side cached data so that 
61          * there is not a speed issue.
62          * 
63          * If the service goes down, another service will be substituted, if available.
64          * 
65          * @param access
66          * @param loc
67          * @param ss
68          * @param item
69          * @param retryable
70          * @return
71          * @throws URISyntaxException 
72          * @throws Exception
73          */
74         public<RET> RET same(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws APIException, CadiException, LocatorException {
75                 RET ret = null;
76                 boolean retry = true;
77                 Rcli<HttpURLConnection> client = retryable.lastClient();
78                 try {
79                         do {
80                                 Item item;
81                                 // if no previous state, get the best
82                                 if(retryable.item()==null) {
83                                         item = loc.best();
84                                         if(item==null) {
85                                                 throw new LocatorException("No Services Found for " + loc);
86                                         }
87                                         retryable.item(item);
88                                         retryable.lastClient = null;
89                                 }
90                                 if(client==null) {
91                                         item = retryable.item();
92                                         URI uri=loc.get(item);
93                                         if(uri==null) {
94                                                 loc.invalidate(retryable.item());
95                                                 if(loc.hasItems()) {
96                                                         retryable.item(loc.next(retryable.item()));
97                                                         continue;
98                                                 } else {
99                                                         throw new LocatorException("No clients available for " + loc.toString());
100                                                 }
101                                         }
102                                         client = new HRcli(this, uri,item,ss)
103                                                 .connectionTimeout(connectionTimeout)
104                                                 .readTimeout(readTimeout)
105                                                 .apiVersion(apiVersion);
106                                 } else {
107                                         client.setSecuritySetter(ss);
108                                 }
109                                 
110                                 retry = false;
111                                 try {
112                                         ret = retryable.code(client);
113                                 } catch (APIException | CadiException e) {
114                                         item = retryable.item();
115                                         loc.invalidate(item);
116                                         retryable.item(loc.next(item));
117                                         try {
118                                                 Throwable ec = e.getCause();
119                                                 if(ec instanceof java.net.ConnectException) {
120                                                         if(client!=null && loc.hasItems()) { 
121                                                                 access.log(Level.WARN,"Connection refused, trying next available service");
122                                                                 retry = true;
123                                                         } else {
124                                                                 throw new CadiException("Connection refused, no more services to try");
125                                                         }
126                                                 } else if(ec instanceof java.net.SocketException) {
127                                                         if(client!=null && loc.hasItems()) { 
128                                                                 access.log(Level.WARN,"Socket prematurely closed, trying next available service");
129                                                                 retry = true;
130                                                         } else {
131                                                                 throw new CadiException("Socket prematurely closed, no more services to try");
132                                                         }
133                                                 } else if(ec instanceof SSLHandshakeException) {
134                                                         retryable.item(null);
135                                                         throw e;
136                                                 } else if(ec instanceof SocketException) {
137                                                         if("java.net.SocketException: Connection reset".equals(ec.getMessage())) {
138                                                                 access.log(Level.ERROR, ec.getMessage(), " can mean Certificate Expiration or TLS Protocol issues");
139                                                         }
140                                                         retryable.item(null);
141                                                         throw e;
142                                                 } else {
143                                                         retryable.item(null);
144                                                         throw e;
145                                                 }
146                                         } finally {
147                                                 client = null;
148                                         }
149                                 } catch (ConnectException e) {
150                                         item = retryable.item();
151                                         loc.invalidate(item);
152                                         retryable.item(loc.next(item));
153                                 }
154                         } while(retry);
155                 } finally {
156                         retryable.lastClient = client;
157                 }
158                 return ret;
159         }
160         
161         
162         public<RET> RET best(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
163                 if(loc==null) {
164                         throw new LocatorException("No Locator Configured");
165                 }
166                 retryable.item(loc.best());
167                 return same(ss,retryable);
168         }
169         public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
170                 return oneOf(ss,retryable,true,null);
171         }
172
173         public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify) throws LocatorException, CadiException, APIException {
174                 return oneOf(ss,retryable,notify,null);
175         }
176         
177         public<RET> RET oneOf(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify,String host) throws LocatorException, CadiException, APIException {
178                 RET ret = null;
179                 // make sure we have all current references:
180                 loc.refresh();
181                 for(Item li=loc.first();li!=null;li=loc.next(li)) {
182                         URI uri=loc.get(li);
183                         if(host!=null && !host.equals(uri.getHost())) {
184                                 break;
185                         }
186                         try {
187                                 ret = retryable.code(new HRcli(this,uri,li,ss));
188                                 access.log(Level.DEBUG,"Success calling",uri,"during call to all services");
189                         } catch (APIException | CadiException e) {
190                                 Throwable t = e.getCause();
191                                 if(t!=null && t instanceof ConnectException) {
192                                         loc.invalidate(li);
193                                         access.log(Level.ERROR,"Connection to",uri,"refused during call to all services");
194                                 } else if(t instanceof SSLHandshakeException) {
195                                         access.log(Level.ERROR,t.getMessage());
196                                         loc.invalidate(li);
197                                 } else if(t instanceof SocketException) {
198                                         if("java.net.SocketException: Connection reset".equals(t.getMessage())) {
199                                                 access.log(Level.ERROR, t.getMessage(), " can mean Certificate Expiration or TLS Protocol issues");
200                                         }
201                                         retryable.item(null);
202                                         throw e;
203                                 } else {
204                                         throw e;
205                                 }
206                         } catch (ConnectException e) {
207                                 loc.invalidate(li);
208                                 access.log(Level.ERROR,"Connection to",uri,"refused during call to all services");
209                         }
210                 }
211                         
212                 if(ret == null && notify) 
213                         throw new LocatorException("No available clients to call");
214                 return ret;
215         }
216         
217
218         public void close() {
219                 // TODO Anything here?
220         }
221
222         public HMangr readTimeout(int timeout) {
223                 this.readTimeout = timeout;
224                 return this;
225         }
226
227         public int readTimeout() {
228                 return readTimeout;
229         }
230         
231         public void connectionTimeout(int t) {
232                 connectionTimeout = t;
233         }
234
235         public int connectionTimout() {
236                 return connectionTimeout;
237         }
238
239         public HMangr apiVersion(String version) {
240                 apiVersion = version;
241                 return this;
242         }
243
244         public String apiVersion() {
245                 return apiVersion;
246         }
247
248 }