K8s doesn't necessarily support DNS
[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.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;
50
51 import locate.v1_0.Endpoint;
52 import locate.v1_0.Endpoints;
53
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;
61
62
63     public AAFLocator(SecurityInfoC<HttpURLConnection> si, URI locatorURI) throws LocatorException {
64         super(si.access, nameFromLocatorURI(locatorURI), 10000L /* Wait at least 10 seconds between refreshes */);
65         synchronized(sr) {
66             if (env==null) {
67                 env = new RosettaEnv(access.getProperties());
68             }
69         }
70         
71         int connectTimeout = Integer.parseInt(si.access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
72         try {
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) {
80                     sb.append('/');
81                     sb.append(path[i]);
82                 }
83                 setPathInfo(sb.toString());
84                 client = createClient(si.defSS, locatorURI, connectTimeout);
85             } else {
86                 client = new HClient(si.defSS, locatorURI, connectTimeout);
87             }
88             epsDF = env.newDataFactory(Endpoints.class);
89             
90         } catch (APIException /*| URISyntaxException*/ e) {
91             throw new LocatorException(e);
92         }
93         lclient = new HClient(si.defSS, locatorURI, connectTimeout);
94         
95         if(si.access.willLog(Access.Level.DEBUG)) {
96             si.access.log(Access.Level.DEBUG, "Root URI:",client.getURI());
97         }
98         
99         String dnsString;
100         if(locatorURI.getPort()<0) {
101                 dnsString=locatorURI.getScheme() + "://" + locatorURI.getHost();
102         } else {
103                 dnsString=locatorURI.getScheme() + "://" +locatorURI.getHost()+':'+locatorURI.getPort();
104         }
105         if(dnsString.contains("null")) { // for Testing Purposes, mostly.
106                 locatorLocator = null;
107         } else {
108                 locatorLocator = new DNSLocator(access, dnsString);
109                 if(locatorLocator.hasItems()) {
110                         locatorItem = locatorLocator.best();
111                 } else {
112                         // For when DNS doesn't work, including some K8s Installations
113                                 locatorLocator = new SingleEndpointLocator(dnsString);
114                 }
115         }
116     }
117
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(),
123                        uri.getUserInfo(),
124                        newURI.getHost(),
125                        newURI.getPort(),
126                        uri.getPath(),
127                        uri.getQuery(),
128                        uri.getFragment());
129     }
130
131     protected final int maxIters() {
132         
133         return locatorLocator.size();
134     }
135
136
137     @Override
138     public boolean refresh() {
139         try {
140             int max = locatorLocator.size();
141             for(int i=0;i<max;) {
142                 ++i;
143                 try {
144                     lclient.setMethod("GET");
145                     lclient.send();
146                     break;
147                 } catch (APIException connectE) {
148                     Throwable ce = connectE.getCause();
149                     if(ce!=null && ce instanceof java.net.ConnectException && i< maxIters()) {
150                         try {
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());
154                             continue;
155                         } catch (LocatorException e) {
156                             throw connectE;
157                         }
158                     }
159                     // last one, just throw
160                     throw connectE;
161                 }
162             }
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));
168                 }
169                 
170                 Collections.sort(epl);
171                 replace(epl);
172                 return true;
173             } else {
174                 env.error().printf("Error reading location information from %s: %d %s\n",client.getURI().toString(),fr.code(),fr.body());
175             }
176         } catch (CadiException | URISyntaxException | APIException e) {
177             env.error().log(e,"Error connecting " + client.getURI() + " for location.");
178         }
179         return false;
180     }
181
182     /* (non-Javadoc)
183      * @see org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator#getURI()
184      */
185     @Override
186     protected URI getURI() {
187         return client.getURI();
188     }
189     
190     protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
191         return new HClient(ss, uri, connectTimeout);
192     }
193     
194 }