1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi.dme2;
\r
26 import java.net.InetAddress;
\r
27 import java.net.URI;
\r
28 import java.net.URISyntaxException;
\r
29 import java.net.UnknownHostException;
\r
30 import java.util.Arrays;
\r
31 import java.util.Comparator;
\r
32 import java.util.Random;
\r
34 import org.onap.aaf.cadi.Access;
\r
35 import org.onap.aaf.cadi.Locator;
\r
36 import org.onap.aaf.cadi.LocatorException;
\r
37 import org.onap.aaf.cadi.Access.Level;
\r
40 import com.att.aft.dme2.api.DME2Exception;
\r
41 import com.att.aft.dme2.api.DME2Manager;
\r
42 import com.att.aft.dme2.api.DME2Server;
\r
43 import com.att.aft.dme2.manager.registry.DME2Endpoint;
\r
45 public class DME2Locator implements Locator {
\r
46 private DME2Manager dm;
\r
47 private DME2Endpoint[] endpoints;
\r
48 private Access access;
\r
49 private String service;
\r
50 private String version;
\r
51 private String routeOffer;
\r
52 private String envContext;
\r
53 private String thisMachine;
\r
54 private String pathInfo;
\r
55 private int thisPort;
\r
56 private boolean removeSelf;
\r
57 private final static Random random = new Random();
\r
59 // Default is to not bother trying to remove self
\r
60 public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer) throws DME2Exception, UnknownHostException, LocatorException {
\r
61 this(access,dm,service,version,envContext,routeOffer,false);
\r
64 public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException {
\r
65 this.access = access;
\r
67 this.dm = new DME2Manager("DME2Locator created DME2Manager",System.getProperties());
\r
71 this.service = service;
\r
72 this.version = version;
\r
73 this.envContext = envContext;
\r
74 this.routeOffer = routeOffer;
\r
76 DME2Server server = dm.getServer();
\r
77 if(server == null) {
\r
78 thisMachine = InetAddress.getLocalHost().getHostName();
\r
82 thisMachine = server.getServerProperties().getHostname();
\r
83 //thisPort = server.getPort();
\r
84 thisPort = server.getServerProperties().getPort();
\r
85 } catch(NullPointerException np) { // BAD BOY, DME2...
\r
86 access.log(Level.ERROR, "WARNING: DME2 threw a NullPointer Exception getting Server Machine and Port");
\r
87 thisMachine = InetAddress.getLocalHost().getHostName();
\r
91 this.removeSelf = removeSelf;
\r
94 // Default is to not bother trying to remove self
\r
95 public DME2Locator(Access access, DME2Manager dm, String aafurl) throws DME2Exception, UnknownHostException, LocatorException {
\r
96 this(access,dm,aafurl,false);
\r
99 public DME2Locator(Access access, DME2Manager dm, String aafurl, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException {
\r
100 if(aafurl==null) throw new LocatorException("URL is null");
\r
101 this.access = access;
\r
103 dm = this.dm = new DME2Manager("DME2Locator created DME2Manager",System.getProperties());
\r
107 String[] split = aafurl.split("/");
\r
108 StringBuilder sb = new StringBuilder();
\r
109 boolean dme2Entered = false;
\r
110 for(String s : split) {
\r
111 if(s.startsWith( "service=")) this.service = s.substring(8);
\r
112 else if(s.startsWith("version=")) this.version = s.substring(8);
\r
113 else if(s.startsWith("envContext=")) this.envContext = s.substring(11);
\r
114 else if(s.startsWith("routeOffer=")) {
\r
115 this.routeOffer = s.substring(11);
\r
116 dme2Entered = true;
\r
118 else if(dme2Entered) {
\r
122 pathInfo = sb.toString();
\r
124 DME2Server server = dm.getServer();
\r
125 if(server == null) {
\r
126 thisMachine = InetAddress.getLocalHost().getHostName();
\r
129 thisMachine = server.getServerProperties().getHostname();
\r
130 if(thisMachine==null) { // even if server !=null, apparently, it can be uninitialized
\r
131 thisMachine = InetAddress.getLocalHost().getHostName();
\r
135 thisPort = server.getServerProperties().getPort();
\r
136 } catch (Exception e) {
\r
141 this.removeSelf=removeSelf;
\r
146 public boolean refresh() {
\r
149 endpoints = dm.findEndpoints(service, version, envContext, routeOffer, true);
\r
151 for(int i=0;i<endpoints.length;++i) {
\r
152 if(endpoints[i].getPort()==thisPort && endpoints[i].getHost().equals(thisMachine))
\r
156 return endpoints.length!=0;
\r
157 } catch (Exception e) {
\r
158 access.log(Level.ERROR, e.getMessage());
\r
163 private String noEndpointsString() {
\r
164 StringBuilder sb = new StringBuilder("No DME2 Endpoints found for ");
\r
165 sb.append(service);
\r
167 sb.append(version);
\r
169 sb.append(envContext);
\r
171 sb.append(routeOffer);
\r
172 return sb.toString();
\r
176 public URI get(Locator.Item item) throws LocatorException {
\r
178 throw new LocatorException(noEndpointsString());
\r
182 Item li = ((Item)item);
\r
183 // if URI has been created, use it
\r
184 if(li.uri!=null)return li.uri;
\r
186 // URI not created, create it
\r
187 if(li.idx<endpoints.length) {
\r
188 DME2Endpoint de = endpoints[li.idx];
\r
191 return li.uri=new URI(de.getProtocol(),null,de.getHost(),de.getPort(),pathInfo,null,null);
\r
192 } catch (URISyntaxException e) {
\r
193 throw new LocatorException(e);
\r
201 public boolean hasItems() {
\r
202 return endpoints!=null && endpoints.length>0;
\r
206 public void invalidate(Locator.Item item) throws LocatorException {
\r
207 if(item instanceof Item) {
\r
208 int idx = ((Item)item).idx;
\r
209 if(idx<endpoints.length) {
\r
210 DME2Endpoint uhoh = endpoints[idx]; // Sometimes, DME2Endpoint, at least on File system, returns bogus entries.
\r
211 endpoints[idx]=null;
\r
212 boolean noneLeft=true;
\r
213 for(int i=0;i<endpoints.length && noneLeft;++i) {
\r
214 noneLeft = endpoints[i]==null;
\r
216 if(noneLeft && refresh()) { // make sure DME2 isn't giving us the same invalidated entry...
\r
217 for(int i=0;i<endpoints.length && noneLeft;++i) {
\r
218 DME2Endpoint ep = endpoints[i];
\r
220 ep.getHost().equals(uhoh.getHost()) &&
\r
221 ep.getPort()==uhoh.getPort()) {
\r
231 public class Item implements Locator.Item {
\r
232 private final int idx;
\r
234 private Item(int i) {
\r
241 public Item best() throws LocatorException {
\r
242 if(!hasItems()) // checks endpoints
\r
243 if(!refresh()) throw new LocatorException("No DME2 Endpoints Available");
\r
245 // Some endpoints in Array are null. Need sub array of usable endpoints
\r
246 int usable[] = new int[endpoints.length];
\r
248 for(int i=0;i<endpoints.length;++i) {
\r
249 if(endpoints[i]!=null) {
\r
250 usable[count++] = i;
\r
254 case 0: refresh(); return null;
\r
255 case 1: return new Item(usable[0]);
\r
257 int samemach[] = new int[count];
\r
258 int samecount = 0,closecount=0;
\r
259 // has to be sortable
\r
260 Integer closemach[] = new Integer[count];
\r
262 // Analyze for Same Machine or Remote machines
\r
263 for(int i=0;i<count;++i) {
\r
264 DME2Endpoint ep = endpoints[usable[i]];
\r
265 String host = ep.getHost();
\r
266 if(thisMachine.equalsIgnoreCase(host)) {
\r
267 samemach[samecount++] = usable[i];
\r
269 closemach[closecount++] = usable[i];
\r
273 switch(samecount) {
\r
275 case 1: return new Item(samemach[0]);
\r
276 default: // return randomized is multiple Endpoints on local machine.
\r
277 int i = random.nextInt();
\r
278 return new Item(usable[Math.abs(i%samecount)]);
\r
281 // Analyze for closest remote
\r
282 switch(closecount) {
\r
283 case 0: return null;
\r
284 case 1: return new Item(closemach[0]);
\r
285 default: // return closest machine
\r
286 DoubIndex remote[] = new DoubIndex[closecount];
\r
287 int remotecount = 0;
\r
288 for(int i=0;i<closecount;++i) {
\r
289 DME2Endpoint de = endpoints[usable[i]];
\r
290 remote[remotecount++] = new DoubIndex(de.getDistance(),i);
\r
292 Arrays.sort(remote,new Comparator<DoubIndex> () {
\r
294 public int compare(DoubIndex a, DoubIndex b) {
\r
295 if(a.d<b.d) return -1;
\r
296 if(a.d>b.d) return 1;
\r
297 return (random.nextInt()%1)==0?1:0;// randomize if the same
\r
301 return new Item(remote[0].idx);
\r
306 private class DoubIndex {
\r
307 public final double d;
\r
308 public final int idx;
\r
310 public DoubIndex(double doub, int i) {
\r
316 public Item first() {
\r
317 if(endpoints==null)return null;
\r
318 for(int i=0;i<endpoints.length;++i) {
\r
319 if(endpoints[i]!=null)
\r
320 return new Item(i);
\r
326 public Item next(Locator.Item item) throws LocatorException {
\r
327 if(endpoints==null || endpoints.length==0 || !(item instanceof Item))return null;
\r
328 int idx = ((Item)item).idx +1;
\r
329 for(int i=idx;i<endpoints.length;++i) {
\r
330 if(endpoints[i]!=null)
\r
331 return new Item(i);
\r
333 // This is a mistake.. will start infinite loops
\r
334 // // Did not have any at end... try beginning
\r
335 // for(int i=0;i<idx-1;++i) {
\r
336 // if(endpoints[i]!=null)
\r
337 // return new Item(i);
\r
339 // // If still nothing, refresh
\r
345 public void destroy() {
\r
346 // TODO Auto-generated method stub
\r