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.Access.Level;
34 import org.onap.aaf.cadi.CadiException;
35 import org.onap.aaf.cadi.Locator;
36 import org.onap.aaf.cadi.Locator.Item;
37 import org.onap.aaf.cadi.LocatorException;
38 import org.onap.aaf.cadi.SecuritySetter;
39 import org.onap.aaf.cadi.client.Rcli;
40 import org.onap.aaf.cadi.client.Retryable;
41 import org.onap.aaf.cadi.util.FixURIinfo;
42 import org.onap.aaf.misc.env.APIException;
45 private String apiVersion;
46 private int readTimeout, connectionTimeout;
47 public final Locator<URI> loc;
48 private Access access;
50 public HMangr(Access access, Locator<URI> loc) throws LocatorException {
52 connectionTimeout=3000;
54 throw new LocatorException("Null Locator passed");
61 * Reuse the same service. This is helpful for multiple calls that change service side cached data so that
62 * there is not a speed issue.
64 * If the service goes down, another service will be substituted, if available.
72 * @throws URISyntaxException
75 public<RET> RET same(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws APIException, CadiException, LocatorException {
78 Rcli<HttpURLConnection> client = retryable.lastClient();
82 // if no previous state, get the best
83 if (retryable.item()==null) {
86 throw new LocatorException("No Services Found for " + loc);
89 retryable.lastClient = null;
92 item = retryable.item();
93 URI uri=loc.get(item);
95 loc.invalidate(retryable.item());
97 retryable.item(loc.next(retryable.item()));
100 throw new LocatorException("No clients available for " + loc.toString());
103 client = new HRcli(this, uri,item,ss)
104 .connectionTimeout(connectionTimeout)
105 .readTimeout(readTimeout)
106 .apiVersion(apiVersion);
108 client.setSecuritySetter(ss);
113 ret = retryable.code(client);
114 } catch (APIException | CadiException e) {
115 item = retryable.item();
116 loc.invalidate(item);
117 retryable.item(loc.next(item));
119 Throwable ec = e.getCause();
120 if (ec instanceof java.net.ConnectException) {
121 if (client!=null && loc.hasItems()) {
122 access.log(Level.WARN,"Connection refused, trying next available service");
125 throw new CadiException("Connection refused, no more services to try");
127 } else if (ec instanceof java.net.SocketException) {
128 if (client!=null && loc.hasItems()) {
129 access.log(Level.WARN,"Socket prematurely closed, trying next available service");
132 throw new CadiException("Socket prematurely closed, no more services to try");
134 } else if (ec instanceof SocketException) {
135 if ("java.net.SocketException: Connection reset".equals(ec.getMessage())) {
136 access.log(Level.ERROR, ec.getMessage(), " can mean Certificate Expiration or TLS Protocol issues");
138 retryable.item(null);
141 retryable.item(null);
147 } catch (ConnectException e) {
148 item = retryable.item();
149 loc.invalidate(item);
150 retryable.item(loc.next(item));
154 retryable.lastClient = client;
160 public<RET> RET best(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
161 retryable.item(loc.best());
162 return same(ss,retryable);
164 public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
165 return call(ss,retryable,true,null);
168 public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify) throws LocatorException, CadiException, APIException {
169 return call(ss,retryable,notify,null);
172 public <RET> RET allExcept(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify, String selfHost) throws LocatorException, CadiException, APIException {
173 return call(ss,retryable,notify,selfHost);
176 public<RET> RET oneOf(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify,String host) throws LocatorException, CadiException, APIException {
177 return call(ss,retryable,notify,host);
179 private<RET> RET call(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify,String host) throws LocatorException, CadiException, APIException {
181 // make sure we have all current references:
183 for (Item li=loc.first();li!=null;li=loc.next(li)) {
185 FixURIinfo fui = new FixURIinfo(uri);
186 if (host!=null && !host.equals(fui.getHost())) {
190 ret = retryable.code(new HRcli(this,uri,li,ss));
191 access.log(Level.DEBUG,"Success calling",uri,"during call to all services");
192 } catch (APIException | CadiException e) {
193 Throwable t = e.getCause();
194 if (t!=null && t instanceof ConnectException) {
196 access.log(Level.ERROR,"Connection to",uri,"refused during call to all services");
197 } else if (t instanceof SSLHandshakeException) {
198 access.log(Level.ERROR,t.getMessage());
200 } else if (t instanceof SocketException) {
201 if ("java.net.SocketException: Connection reset".equals(t.getMessage())) {
202 access.log(Level.ERROR, t.getMessage(), " can mean Certificate Expiration or TLS Protocol issues");
204 retryable.item(null);
209 } catch (ConnectException e) {
211 access.log(Level.ERROR,"Connection to",uri,"refused during call to all services");
215 if (ret == null && notify)
216 throw new LocatorException("No available clients to call");
221 public void close() {
222 // TODO Anything here?
225 public HMangr readTimeout(int timeout) {
226 this.readTimeout = timeout;
230 public int readTimeout() {
234 public void connectionTimeout(int t) {
235 connectionTimeout = t;
238 public int connectionTimeout() {
239 return connectionTimeout;
242 public HMangr apiVersion(String version) {
243 apiVersion = version;
247 public String apiVersion() {