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.CadiException;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.SecuritySetter;
34 import org.onap.aaf.cadi.client.Future;
35 import org.onap.aaf.cadi.config.Config;
36 import org.onap.aaf.cadi.config.SecurityInfoC;
37 import org.onap.aaf.cadi.http.HClient;
38 import org.onap.aaf.cadi.util.Split;
39 import org.onap.aaf.misc.env.APIException;
40 import org.onap.aaf.misc.env.Data.TYPE;
41 import org.onap.aaf.misc.env.impl.BasicTrans;
42 import org.onap.aaf.misc.rosetta.env.RosettaDF;
43 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
45 import locate.v1_0.Endpoint;
46 import locate.v1_0.Endpoints;
48 public class AAFLocator extends AbsAAFLocator<BasicTrans> {
49 private static RosettaEnv env;
51 private RosettaDF<Endpoints> epsDF;
53 public AAFLocator(SecurityInfoC<HttpURLConnection> si, URI locatorURI) throws LocatorException {
54 super(si.access, nameFromLocatorURI(locatorURI), 10000L /* Wait at least 10 seconds between refreshes */);
55 SecuritySetter<HttpURLConnection> ss;
57 ss=AAFConHttp.bestSS(si);
58 } catch (APIException | CadiException e1) {
59 throw new LocatorException(e1);
63 env = new RosettaEnv(access.getProperties());
67 int connectTimeout = Integer.parseInt(si.access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
69 String[] path = Split.split('/',locatorURI.getPath());
70 if(path.length>2 && "locate".equals(path[1])) {
71 StringBuilder sb = new StringBuilder();
72 for(int i=3;i<path.length;++i) {
76 setPathInfo(sb.toString());
77 String host = locatorURI.getHost();
78 if(aaf_locator_host!=null && (host==null || "AAF_LOCATOR_URL".equals(host))) {
79 int slash = aaf_locator_host.lastIndexOf("//");
80 host = aaf_locator_host.substring(slash+2);
83 locatorURI.getScheme(),
84 locatorURI.getUserInfo(),
87 "/locate/"+name + '/' + version,
91 client = createClient(ss, uri, connectTimeout);
93 client = new HClient(ss, locatorURI, connectTimeout);
95 epsDF = env.newDataFactory(Endpoints.class);
97 } catch (APIException | URISyntaxException e) {
98 throw new LocatorException(e);
103 public boolean refresh() {
105 client.setMethod("GET");
107 Future<Endpoints> fr = client.futureRead(epsDF, TYPE.JSON);
108 if(fr.get(client.timeout())) {
109 List<EP> epl = new LinkedList<EP>();
110 for(Endpoint endpoint : fr.value.getEndpoint()) {
111 epl.add(new EP(endpoint,latitude,longitude));
114 Collections.sort(epl);
118 env.error().printf("Error reading location information from %s: %d %s\n",client.getURI().toString(),fr.code(),fr.body());
120 } catch (CadiException | URISyntaxException | APIException e) {
121 env.error().log(e,"Error connecting " + client.getURI() + " for location.");
127 * @see org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator#getURI()
130 protected URI getURI() {
131 return client.getURI();
134 protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
135 return new HClient(ss, uri, connectTimeout);