Collection syntax change because of Sonar
[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.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;
44
45 import locate.v1_0.Endpoint;
46 import locate.v1_0.Endpoints;
47
48 public class AAFLocator extends AbsAAFLocator<BasicTrans>  {
49         private static RosettaEnv env;
50         HClient client;
51         private RosettaDF<Endpoints> epsDF;
52
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                 synchronized(sr) {
56                         if(env==null) {
57                                 env = new RosettaEnv(access.getProperties());
58                         }
59                 }
60                 
61                 int connectTimeout = Integer.parseInt(si.access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
62                 try {
63                         String[] path = Split.split('/',locatorURI.getPath());
64                         String host = locatorURI.getHost();
65                         if(host==null) {
66                                 host = locatorURI.getAuthority(); // this happens when no port
67                         }
68                         if("AAF_LOCATE_URL".equals(host)) {
69                                 URI uri = new URI(
70                                                 locatorURI.getScheme(),
71                                                 locatorURI.getUserInfo(),
72                                                 aaf_locator_uri.getHost(),
73                                                 aaf_locator_uri.getPort(),
74                                                 "/locate"+locatorURI.getPath(),
75                                                 null,
76                                                 null
77                                                 );
78                                 client = createClient(si.defSS, uri, connectTimeout);
79                         } else if(path.length>1 && "locate".equals(path[1])) {
80                                 StringBuilder sb = new StringBuilder();
81                                 for(int i=3;i<path.length;++i) {
82                                         sb.append('/');
83                                         sb.append(path[i]);
84                                 }
85                                 setPathInfo(sb.toString());
86                                 URI uri = new URI(
87                                                         locatorURI.getScheme(),
88                                                         locatorURI.getUserInfo(),
89                                                         locatorURI.getHost(),
90                                                         locatorURI.getPort(),
91                                                         "/locate/"+name + '/' + version,
92                                                         null,
93                                                         null
94                                                         );
95                                 client = createClient(si.defSS, uri, connectTimeout);
96                         } else {
97                                 client = new HClient(si.defSS, locatorURI, connectTimeout);
98                         }
99                         epsDF = env.newDataFactory(Endpoints.class);
100                 } catch (APIException | URISyntaxException e) {
101                         throw new LocatorException(e);
102                 }
103         }
104
105         @Override
106         public boolean refresh() {
107                 try {
108                         client.setMethod("GET");
109                         client.send();
110                         Future<Endpoints> fr = client.futureRead(epsDF, TYPE.JSON);
111                         if(fr.get(client.timeout())) {
112                                 List<EP> epl = new LinkedList<>();
113                                 for(Endpoint endpoint : fr.value.getEndpoint()) {
114                                         epl.add(new EP(endpoint,latitude,longitude));
115                                 }
116                                 
117                                 Collections.sort(epl);
118                                 replace(epl);
119                                 return true;
120                         } else {
121                                 env.error().printf("Error reading location information from %s: %d %s\n",client.getURI().toString(),fr.code(),fr.body());
122                         }
123                 } catch (CadiException | URISyntaxException | APIException e) {
124                         env.error().log(e,"Error connecting " + client.getURI() + " for location.");
125                 }
126                 return false;
127         }
128
129         /* (non-Javadoc)
130          * @see org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator#getURI()
131          */
132         @Override
133         protected URI getURI() {
134                 return client.getURI();
135         }
136         
137         protected HClient createClient(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout) throws LocatorException {
138                 return new HClient(ss, uri, connectTimeout);
139         }
140 }