/******************************************************************************* * ============LICENSE_START==================================================== * * org.onap.aaf * * =========================================================================== * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. * * =========================================================================== * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * * ============LICENSE_END==================================================== * * * * ECOMP is a trademark and service mark of AT&T Intellectual Property. * * ******************************************************************************/ package org.onap.aaf.cadi.dme2; import java.net.InetAddress; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; import java.util.Arrays; import java.util.Comparator; import java.util.Random; import org.onap.aaf.cadi.Access; import org.onap.aaf.cadi.Locator; import org.onap.aaf.cadi.LocatorException; import org.onap.aaf.cadi.Access.Level; // import com.att.aft.dme2.api.DME2Exception; import com.att.aft.dme2.api.DME2Manager; import com.att.aft.dme2.api.DME2Server; import com.att.aft.dme2.manager.registry.DME2Endpoint; public class DME2Locator implements Locator { private DME2Manager dm; private DME2Endpoint[] endpoints; private Access access; private String service; private String version; private String routeOffer; private String envContext; private String thisMachine; private String pathInfo; private int thisPort; private boolean removeSelf; private final static Random random = new Random(); // Default is to not bother trying to remove self public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer) throws DME2Exception, UnknownHostException, LocatorException { this(access,dm,service,version,envContext,routeOffer,false); } public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException { this.access = access; if(dm==null) { this.dm = new DME2Manager("DME2Locator created DME2Manager",System.getProperties()); } else { this.dm = dm; } this.service = service; this.version = version; this.envContext = envContext; this.routeOffer = routeOffer; refresh(); DME2Server server = dm.getServer(); if(server == null) { thisMachine = InetAddress.getLocalHost().getHostName(); thisPort = 0; } else { try { thisMachine = server.getServerProperties().getHostname(); //thisPort = server.getPort(); thisPort = server.getServerProperties().getPort(); } catch(NullPointerException np) { // BAD BOY, DME2... access.log(Level.ERROR, "WARNING: DME2 threw a NullPointer Exception getting Server Machine and Port"); thisMachine = InetAddress.getLocalHost().getHostName(); thisPort = 0; } } this.removeSelf = removeSelf; } // Default is to not bother trying to remove self public DME2Locator(Access access, DME2Manager dm, String aafurl) throws DME2Exception, UnknownHostException, LocatorException { this(access,dm,aafurl,false); } public DME2Locator(Access access, DME2Manager dm, String aafurl, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException { if(aafurl==null) throw new LocatorException("URL is null"); this.access = access; if(dm==null) { dm = this.dm = new DME2Manager("DME2Locator created DME2Manager",System.getProperties()); } else { this.dm = dm; } String[] split = aafurl.split("/"); StringBuilder sb = new StringBuilder(); boolean dme2Entered = false; for(String s : split) { if(s.startsWith( "service=")) this.service = s.substring(8); else if(s.startsWith("version=")) this.version = s.substring(8); else if(s.startsWith("envContext=")) this.envContext = s.substring(11); else if(s.startsWith("routeOffer=")) { this.routeOffer = s.substring(11); dme2Entered = true; } else if(dme2Entered) { sb.append('/'); sb.append(s); } pathInfo = sb.toString(); } DME2Server server = dm.getServer(); if(server == null) { thisMachine = InetAddress.getLocalHost().getHostName(); thisPort = 0; } else { thisMachine = server.getServerProperties().getHostname(); if(thisMachine==null) { // even if server !=null, apparently, it can be uninitialized thisMachine = InetAddress.getLocalHost().getHostName(); thisPort = 0; } else { try { thisPort = server.getServerProperties().getPort(); } catch (Exception e) { thisPort = 0; } } } this.removeSelf=removeSelf; refresh(); } @Override public boolean refresh() { try { dm.refresh(); endpoints = dm.findEndpoints(service, version, envContext, routeOffer, true); if(removeSelf) { for(int i=0;i0; } @Override public void invalidate(Locator.Item item) throws LocatorException { if(item instanceof Item) { int idx = ((Item)item).idx; if(idx () { @Override public int compare(DoubIndex a, DoubIndex b) { if(a.db.d) return 1; return (random.nextInt()%1)==0?1:0;// randomize if the same } }); return new Item(remote[0].idx); } } } private class DoubIndex { public final double d; public final int idx; public DoubIndex(double doub, int i) { d = doub; idx = i; } } @Override public Item first() { if(endpoints==null)return null; for(int i=0;i