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.aaf.v2_0;
24 import java.net.HttpURLConnection;
26 import java.net.URISyntaxException;
27 import java.util.Collections;
28 import java.util.LinkedList;
29 import java.util.List;
31 import org.onap.aaf.cadi.Access;
32 import org.onap.aaf.cadi.Access.Level;
33 import org.onap.aaf.cadi.CadiException;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.SecuritySetter;
36 import org.onap.aaf.cadi.client.Future;
37 import org.onap.aaf.cadi.config.Config;
38 import org.onap.aaf.cadi.config.SecurityInfoC;
39 import org.onap.aaf.cadi.http.HClient;
40 import org.onap.aaf.cadi.locator.DNSLocator;
41 import org.onap.aaf.cadi.locator.SingleEndpointLocator;
42 import org.onap.aaf.cadi.locator.SizedLocator;
43 import org.onap.aaf.cadi.util.FixURIinfo;
44 import org.onap.aaf.cadi.util.Split;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.Data.TYPE;
47 import org.onap.aaf.misc.env.impl.BasicTrans;
48 import org.onap.aaf.misc.rosetta.env.RosettaDF;
49 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
51 import locate.v1_0.Endpoint;
52 import locate.v1_0.Endpoints;
54 public class AAFLocator extends AbsAAFLocator<BasicTrans> {
55 private static RosettaEnv env;
56 private HClient client;
57 private HClient lclient;
58 private RosettaDF<Endpoints> epsDF;
59 private SizedLocator<URI> locatorLocator;
60 private Item locatorItem;
63 public AAFLocator(SecurityInfoC<HttpURLConnection> si, URI locatorURI) throws LocatorException {
64 super(si.access, nameFromLocatorURI(locatorURI), 10000L /* Wait at least 10 seconds between refreshes */);
67 env = new RosettaEnv(access.getProperties());
71 int connectTimeout = Integer.parseInt(si.access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
73 String[] path = Split.split('/',locatorURI.getPath());
74 FixURIinfo fui = new FixURIinfo(locatorURI);
75 if ("AAF_LOCATE_URL".equals(fui.getHost())) {
76 client = createClient(si.defSS, locatorURI, connectTimeout);
77 } else if (path.length>1 && "locate".equals(path[1])) {
78 StringBuilder sb = new StringBuilder();
79 for (int i=3;i<path.length;++i) {
83 setPathInfo(sb.toString());
84 client = createClient(si.defSS, locatorURI, connectTimeout);
86 client = new HClient(si.defSS, locatorURI, connectTimeout);
88 epsDF = env.newDataFactory(Endpoints.class);
90 } catch (APIException /*| URISyntaxException*/ e) {
91 throw new LocatorException(e);
93 lclient = new HClient(si.defSS, locatorURI, connectTimeout);
95 if(si.access.willLog(Access.Level.DEBUG)) {
96 si.access.log(Access.Level.DEBUG, "Root URI:",client.getURI());
100 if(locatorURI.getPort()<0) {
101 dnsString=locatorURI.getScheme() + "://" + locatorURI.getHost();
103 dnsString=locatorURI.getScheme() + "://" +locatorURI.getHost()+':'+locatorURI.getPort();
105 if(dnsString.contains("null")) { // for Testing Purposes, mostly.
106 locatorLocator = null;
108 locatorLocator = new DNSLocator(access, dnsString);
109 if(locatorLocator.hasItems()) {
110 locatorItem = locatorLocator.best();
112 // For when DNS doesn't work, including some K8s Installations
113 locatorLocator = new SingleEndpointLocator(dnsString);
118 private URI locatorFail(URI uri) throws LocatorException, URISyntaxException {
119 locatorLocator.invalidate(locatorItem);
120 locatorItem = locatorLocator.best();
121 URI newURI = locatorLocator.get(locatorItem);
122 return new URI(uri.getScheme(),
131 protected final int maxIters() {
133 return locatorLocator.size();
138 public boolean refresh() {
140 int max = locatorLocator.size();
141 for(int i=0;i<max;) {
144 lclient.setMethod("GET");
147 } catch (APIException connectE) {
148 Throwable ce = connectE.getCause();
149 if(ce!=null && ce instanceof java.net.ConnectException && i< maxIters()) {
151 URI old = client.getURI();
152 lclient.setURI(locatorFail(old));
153 access.printf(Level.INFO, "AAF Locator changed from %s to %s",old, lclient.getURI());
155 } catch (LocatorException e) {
159 // last one, just throw
163 Future<Endpoints> fr = lclient.futureRead(epsDF, TYPE.JSON);
164 if (fr.get(lclient.timeout())) {
165 List<EP> epl = new LinkedList<>();
166 for (Endpoint endpoint : fr.value.getEndpoint()) {
167 epl.add(new EP(endpoint,latitude,longitude));
170 Collections.sort(epl);
174 env.error().printf("Error reading location information from %s: %d %s\n",client.getURI().toString(),fr.code(),fr.body());
176 } catch (CadiException | URISyntaxException | APIException e) {
177 env.error().log(e,"Error connecting " + client.getURI() + " for location.");
183 * @see org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator#getURI()
186 protected URI getURI() {
187 return client.getURI();
190 protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
191 return new HClient(ss, uri, connectTimeout);