ed24b1ee5292becf84458d1a9ba8173b2b387b8a
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / v2_0 / AAFLocator.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.aaf.v2_0;
23
24 import java.net.HttpURLConnection;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Collections;
28 import java.util.LinkedList;
29 import java.util.List;
30
31 import org.onap.aaf.cadi.Access;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.LocatorException;
34 import org.onap.aaf.cadi.SecuritySetter;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.config.Config;
37 import org.onap.aaf.cadi.config.SecurityInfoC;
38 import org.onap.aaf.cadi.http.HClient;
39 import org.onap.aaf.cadi.util.FixURIinfo;
40 import org.onap.aaf.cadi.util.Split;
41 import org.onap.aaf.misc.env.APIException;
42 import org.onap.aaf.misc.env.Data.TYPE;
43 import org.onap.aaf.misc.env.impl.BasicTrans;
44 import org.onap.aaf.misc.rosetta.env.RosettaDF;
45 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
46
47 import locate.v1_0.Endpoint;
48 import locate.v1_0.Endpoints;
49
50 public class AAFLocator extends AbsAAFLocator<BasicTrans>  {
51     private static RosettaEnv env;
52     HClient client;
53     private RosettaDF<Endpoints> epsDF;
54
55     public AAFLocator(SecurityInfoC<HttpURLConnection> si, URI locatorURI) throws LocatorException {
56         super(si.access, nameFromLocatorURI(locatorURI), 10000L /* Wait at least 10 seconds between refreshes */);
57         synchronized(sr) {
58             if (env==null) {
59                 env = new RosettaEnv(access.getProperties());
60             }
61         }
62         
63         int connectTimeout = Integer.parseInt(si.access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
64         try {
65             String[] path = Split.split('/',locatorURI.getPath());
66             FixURIinfo fui = new FixURIinfo(locatorURI);
67             if ("AAF_LOCATE_URL".equals(fui.getHost())) {
68                 client = createClient(si.defSS, locatorURI, connectTimeout);
69             } else if (path.length>1 && "locate".equals(path[1])) {
70                 StringBuilder sb = new StringBuilder();
71                 for (int i=3;i<path.length;++i) {
72                     sb.append('/');
73                     sb.append(path[i]);
74                 }
75                 setPathInfo(sb.toString());
76 //                URI uri = new URI(
77 //                            locatorURI.getScheme(),
78 //                            locatorURI.getAuthority(),
79 //                            locatorURI.getPath(),
80 //                            null,
81 //                            null
82 //                            );
83                 client = createClient(si.defSS, locatorURI, connectTimeout);
84             } else {
85                 client = new HClient(si.defSS, locatorURI, connectTimeout);
86             }
87             epsDF = env.newDataFactory(Endpoints.class);
88             
89         } catch (APIException /*| URISyntaxException*/ e) {
90             throw new LocatorException(e);
91         }
92         
93         if(si.access.willLog(Access.Level.DEBUG)) {
94             si.access.log(Access.Level.DEBUG, "Root URI:",client.getURI());
95         }
96     }
97
98     @Override
99     public boolean refresh() {
100         try {
101             client.setMethod("GET");
102             client.send();
103             Future<Endpoints> fr = client.futureRead(epsDF, TYPE.JSON);
104             if (fr.get(client.timeout())) {
105                 List<EP> epl = new LinkedList<>();
106                 for (Endpoint endpoint : fr.value.getEndpoint()) {
107                     epl.add(new EP(endpoint,latitude,longitude));
108                 }
109                 
110                 Collections.sort(epl);
111                 replace(epl);
112                 return true;
113             } else {
114                 env.error().printf("Error reading location information from %s: %d %s\n",client.getURI().toString(),fr.code(),fr.body());
115             }
116         } catch (CadiException | URISyntaxException | APIException e) {
117             env.error().log(e,"Error connecting " + client.getURI() + " for location.");
118         }
119         return false;
120     }
121
122     /* (non-Javadoc)
123      * @see org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator#getURI()
124      */
125     @Override
126     protected URI getURI() {
127         return client.getURI();
128     }
129     
130     protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
131         return new HClient(ss, uri, connectTimeout);
132     }
133     
134 }