+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.locator;\r
-\r
-\r
-import java.net.InetAddress;\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-import java.net.UnknownHostException;\r
-import java.util.Arrays;\r
-import java.util.Comparator;\r
-import java.util.Properties;\r
-import java.util.Random;\r
-\r
-import org.onap.aaf.cadi.Access;\r
-import org.onap.aaf.cadi.Locator;\r
-import org.onap.aaf.cadi.LocatorException;\r
-import org.onap.aaf.cadi.PropAccess;\r
-import org.onap.aaf.cadi.Access.Level;\r
-\r
-import java.security.SecureRandom;\r
-\r
-//import com.att.aft.dme2.api.DME2Endpoint;\r
-import com.att.aft.dme2.api.DME2Exception;\r
-import com.att.aft.dme2.api.DME2Manager;\r
-import com.att.aft.dme2.api.DME2Server;\r
-import com.att.aft.dme2.manager.registry.DME2Endpoint;\r
-\r
-public class DME2Locator implements Locator<URI> {\r
- private DME2Manager dm;\r
- private DME2Endpoint[] endpoints;\r
- private Access access;\r
- private String service;\r
- private String version;\r
- private String routeOffer;\r
- private String envContext;\r
- private String thisMachine;\r
- private String pathInfo;\r
- private int thisPort;\r
- private boolean removeSelf;\r
- private final static SecureRandom random = new SecureRandom();\r
-\r
- // Default is to not bother trying to remove self\r
- public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer) throws DME2Exception, UnknownHostException, LocatorException {\r
- this(access,dm,service,version,envContext,routeOffer,false);\r
- }\r
- \r
- public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException {\r
- this.access = access;\r
- if(dm==null) {\r
- this.dm = new DME2Manager("DME2Locator created DME2Manager",System.getProperties());\r
- } else {\r
- this.dm = dm;\r
- }\r
- this.service = service;\r
- this.version = version;\r
- this.envContext = envContext;\r
- this.routeOffer = routeOffer;\r
- refresh();\r
- if(thisMachine==null) {\r
- // Can't get from dm... \r
- thisMachine = InetAddress.getLocalHost().getHostName();\r
- thisPort = 0;\r
- } else {\r
- thisPort = dm.getPort();\r
- }\r
-\r
- this.removeSelf = removeSelf;\r
- }\r
-\r
- // Default is to not bother trying to remove self\r
- public DME2Locator(Access access, DME2Manager dm, String aafurl) throws DME2Exception, UnknownHostException, LocatorException {\r
- this(access,dm,aafurl,false);\r
- }\r
- \r
- public DME2Locator(Access access, DME2Manager dm, String aafurl, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException {\r
- if(aafurl==null) {\r
- throw new LocatorException("URL is null");\r
- }\r
- this.access = access;\r
- if(dm==null) {\r
- Properties dprops;\r
- if(access instanceof PropAccess) {\r
- dprops = ((PropAccess)access).getDME2Properties();\r
- } else {\r
- dprops = System.getProperties();\r
- }\r
- dm = this.dm = new DME2Manager("DME2Locator created DME2Manager",dprops);\r
- } else {\r
- this.dm = dm;\r
- }\r
- String[] split = aafurl.split("/");\r
- StringBuilder sb = new StringBuilder();\r
- boolean dme2Entered = false;\r
- for(String s : split) {\r
- if(s.startsWith("service=")) {\r
- this.service = s.substring(8);\r
- } else if(s.startsWith("version=")) {\r
- this.version = s.substring(8);\r
- } else if(s.startsWith("envContext=")) {\r
- this.envContext = s.substring(11);\r
- } else if(s.startsWith("routeOffer=")) {\r
- this.routeOffer = s.substring(11);\r
- dme2Entered = true;\r
- } else if(dme2Entered) {\r
- sb.append('/');\r
- sb.append(s);\r
- }\r
- }\r
- pathInfo = sb.toString();\r
- thisMachine = dm.getHostname();\r
- if(thisMachine==null) {\r
- // Can't get from dm... \r
- thisMachine = InetAddress.getLocalHost().getHostName();\r
- thisPort = 0;\r
- } else {\r
- thisPort = dm.getPort();\r
- }\r
- this.removeSelf=removeSelf;\r
- refresh();\r
- }\r
- \r
- @Override\r
- public boolean refresh() {\r
- try {\r
- dm.refresh();\r
- //endpoints = dm.findEndpoints(service, version, envContext, routeOffer, true);\r
- if(removeSelf) {\r
-// for(int i=0;i<endpoints.length;++i) {\r
-// if(endpoints[i].getPort()==thisPort && endpoints[i].getHost().equals(thisMachine))\r
-// endpoints[i]=null;\r
- }\r
- //}\r
- //return endpoints.length!=0;\r
- } catch (Exception e) {\r
- access.log(Level.ERROR, e.getMessage());\r
- }\r
- return false;\r
- }\r
-\r
- private String noEndpointsString() {\r
- StringBuilder sb = new StringBuilder("No DME2 Endpoints found for ");\r
- sb.append(service);\r
- sb.append('/');\r
- sb.append(version);\r
- sb.append('/');\r
- sb.append(envContext);\r
- sb.append('/');\r
- sb.append(routeOffer);\r
- return sb.toString();\r
- }\r
-\r
- @Override\r
- public URI get(Locator.Item item) throws LocatorException {\r
- if(!hasItems()) \r
- throw new LocatorException(noEndpointsString());\r
- if(item == null) \r
- return null;\r
-\r
- DME2Item li = ((DME2Item)item);\r
- // if URI has been created, use it\r
- if(li.uri!=null)return li.uri;\r
- \r
- // URI not created, create it\r
-// if(li.idx<endpoints.length) {\r
-// DME2Endpoint de = endpoints[li.idx];\r
-// if(de!=null) {\r
-// try {\r
-// return li.uri=new URI(de.getProtocol().toLowerCase(),null,de.getHost(),de.getPort(),pathInfo,null,null);\r
-// } catch (URISyntaxException e) {\r
-// throw new LocatorException(e);\r
-// }\r
-// }\r
-// }\r
- return null;\r
- }\r
- \r
- @Override\r
- public boolean hasItems() {\r
- //return endpoints!=null && endpoints.length>0;\r
- return true;\r
- }\r
-\r
- @Override\r
- public void invalidate(Locator.Item item) throws LocatorException {\r
- if(item instanceof DME2Item) {\r
- int idx = ((DME2Item)item).idx;\r
-// if(idx<endpoints.length) {\r
-// DME2Endpoint uhoh = endpoints[idx]; // Sometimes, DME2Endpoint, at least on File system, returns bogus entries.\r
-// endpoints[idx]=null;\r
-// boolean noneLeft=true;\r
-// for(int i=0;i<endpoints.length && noneLeft;++i) {\r
-// noneLeft = endpoints[i]==null;\r
-// }\r
-// if(noneLeft && refresh()) { // make sure DME2 isn't giving us the same invalidated entry...\r
-// for(int i=0;i<endpoints.length && noneLeft;++i) {\r
-// DME2Endpoint ep = endpoints[i];\r
-// if(ep != null && \r
-// ep.getHost().equals(uhoh.getHost()) &&\r
-// ep.getPort()==uhoh.getPort()) {\r
-// endpoints[i]=null;\r
-// }\r
-// }\r
-// }\r
-// \r
-// }\r
- }\r
- }\r
-\r
- public class DME2Item implements Locator.Item {\r
- private final int idx;\r
- private URI uri;\r
- private DME2Item(int i) {\r
- idx = i;\r
- uri = null;\r
- }\r
- }\r
-\r
- @Override\r
- public DME2Item best() throws LocatorException {\r
- if(!hasItems()) // checks endpoints\r
- if(!refresh()) throw new LocatorException("No DME2 Endpoints Available");\r
- \r
- // Some endpoints in Array are null. Need sub array of usable endpoints\r
- //int usable[] = new int[endpoints.length];\r
- int count=0;\r
-int[] usable = null;\r
- // for(int i=0;i<endpoints.length;++i) {\r
-// if(endpoints[i]!=null) {\r
-// usable[count++] = i;\r
-// }\r
-// }\r
- switch(count) {\r
- case 0: refresh(); return null;\r
- case 1: return new DME2Item(usable[0]);\r
- default:\r
- int samemach[] = new int[count];\r
- int samecount = 0,closecount=0;\r
- // has to be sortable\r
- Integer closemach[] = new Integer[count];\r
- \r
- // Analyze for Same Machine or Remote machines\r
-// for(int i=0;i<count;++i) {\r
-// DME2Endpoint ep = endpoints[usable[i]];\r
-// String host = ep.getHost();\r
-// if(thisMachine.equalsIgnoreCase(host)) {\r
-// samemach[samecount++] = usable[i];\r
-// } else {\r
-// closemach[closecount++] = usable[i];\r
-// }\r
-// }\r
- \r
- switch(samecount) {\r
- case 0: break;\r
- case 1: return new DME2Item(samemach[0]);\r
- default: // return randomized is multiple Endpoints on local machine.\r
- int i = random.nextInt();\r
- return new DME2Item(usable[Math.abs(i%samecount)]);\r
- }\r
- \r
- // Analyze for closest remote\r
- switch(closecount) {\r
- case 0: return null;\r
- case 1: return new DME2Item(closemach[0]);\r
- default: // return closest machine\r
- DoubIndex remote[] = new DoubIndex[closecount];\r
- int remotecount = 0;\r
- for(int i=0;i<closecount;++i) {\r
- //DME2Endpoint de = endpoints[usable[i]];\r
- // remote[remotecount++] = new DoubIndex(de.getDistance(),i);\r
- }\r
- Arrays.sort(remote,new Comparator<DoubIndex> () {\r
- @Override\r
- public int compare(DoubIndex a, DoubIndex b) {\r
- if(a.d<b.d) return -1;\r
- if(a.d>b.d) return 1;\r
- return (random.nextInt()%1)==0?1:0;// randomize if the same\r
- }\r
- \r
- });\r
- return new DME2Item(remote[0].idx);\r
- }\r
- }\r
- }\r
- \r
- private static class DoubIndex {\r
- public final double d;\r
- public final int idx;\r
- \r
- public DoubIndex(double doub, int i) {\r
- d = doub;\r
- idx = i;\r
- }\r
- }\r
- @Override\r
- public DME2Item first() {\r
-// if(endpoints==null)return null;\r
-// for(int i=0;i<endpoints.length;++i) {\r
-// if(endpoints[i]!=null)\r
-// return new DME2Item(i); \r
-// }\r
- return null;\r
- }\r
-\r
- @Override\r
- public DME2Item next(Locator.Item item) throws LocatorException {\r
- //if(endpoints==null || endpoints.length==0 || !(item instanceof DME2Item))return null;\r
- int idx = ((DME2Item)item).idx +1;\r
-// for(int i=idx;i<endpoints.length;++i) {\r
-// if(endpoints[i]!=null)\r
-// return new DME2Item(i); \r
-// }\r
-// This is a mistake.. will start infinite loops\r
-// // Did not have any at end... try beginning\r
-// for(int i=0;i<idx-1;++i) {\r
-// if(endpoints[i]!=null)\r
-// return new Item(i); \r
-// }\r
-// // If still nothing, refresh\r
-// refresh();\r
- return null;\r
- }\r
- \r
- @Override\r
- public void destroy() {\r
- }\r
-}\r