2 * ============LICENSE_START====================================================
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
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====================================================
22 package org.onap.aaf.cadi.http;
24 import java.net.ConnectException;
25 import java.net.HttpURLConnection;
26 import java.net.SocketException;
28 import java.net.URISyntaxException;
30 import javax.net.ssl.SSLHandshakeException;
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;
44 private String apiVersion;
45 private int readTimeout, connectionTimeout;
46 public final Locator<URI> loc;
47 private Access access;
49 public HMangr(Access access, Locator<URI> loc) throws LocatorException {
51 connectionTimeout=3000;
53 throw new LocatorException("Null Locator passed");
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.
63 * If the service goes down, another service will be substituted, if available.
71 * @throws URISyntaxException
74 public<RET> RET same(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws APIException, CadiException, LocatorException {
77 Rcli<HttpURLConnection> client = retryable.lastClient();
81 // if no previous state, get the best
82 if(retryable.item()==null) {
85 throw new LocatorException("No Services Found for " + loc);
88 retryable.lastClient = null;
91 item = retryable.item();
92 URI uri=loc.get(item);
94 loc.invalidate(retryable.item());
96 retryable.item(loc.next(retryable.item()));
99 throw new LocatorException("No clients available for " + loc.toString());
102 client = new HRcli(this, uri,item,ss)
103 .connectionTimeout(connectionTimeout)
104 .readTimeout(readTimeout)
105 .apiVersion(apiVersion);
107 client.setSecuritySetter(ss);
112 ret = retryable.code(client);
113 } catch (APIException | CadiException e) {
114 item = retryable.item();
115 loc.invalidate(item);
116 retryable.item(loc.next(item));
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");
124 throw new CadiException("Connection refused, no more services to try");
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");
131 throw new CadiException("Socket prematurely closed, no more services to try");
133 } else if(ec instanceof SSLHandshakeException) {
134 retryable.item(null);
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");
140 retryable.item(null);
143 retryable.item(null);
149 } catch (ConnectException e) {
150 item = retryable.item();
151 loc.invalidate(item);
152 retryable.item(loc.next(item));
156 retryable.lastClient = client;
162 public<RET> RET best(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
164 throw new LocatorException("No Locator Configured");
166 retryable.item(loc.best());
167 return same(ss,retryable);
169 public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
170 return oneOf(ss,retryable,true,null);
173 public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify) throws LocatorException, CadiException, APIException {
174 return oneOf(ss,retryable,notify,null);
177 public<RET> RET oneOf(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify,String host) throws LocatorException, CadiException, APIException {
179 // make sure we have all current references:
181 for(Item li=loc.first();li!=null;li=loc.next(li)) {
183 if(host!=null && !host.equals(uri.getHost())) {
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) {
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());
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");
201 retryable.item(null);
206 } catch (ConnectException e) {
208 access.log(Level.ERROR,"Connection to",uri,"refused during call to all services");
212 if(ret == null && notify)
213 throw new LocatorException("No available clients to call");
218 public void close() {
219 // TODO Anything here?
222 public HMangr readTimeout(int timeout) {
223 this.readTimeout = timeout;
227 public int readTimeout() {
231 public void connectionTimeout(int t) {
232 connectionTimeout = t;
235 public int connectionTimout() {
236 return connectionTimeout;
239 public HMangr apiVersion(String version) {
240 apiVersion = version;
244 public String apiVersion() {