import java.net.URI;
import java.net.URISyntaxException;
+import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import org.onap.aaf.cadi.Access;
import org.onap.aaf.cadi.Access.Level;
-import org.onap.aaf.cadi.aaf.Defaults;
+import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.Locator;
import org.onap.aaf.cadi.LocatorException;
import org.onap.aaf.cadi.config.Config;
-import org.onap.aaf.cadi.locator.PropertyLocator;
+import org.onap.aaf.cadi.config.RegistrationPropHolder;
import org.onap.aaf.cadi.routing.GreatCircle;
import org.onap.aaf.misc.env.Trans;
import org.onap.aaf.misc.env.util.Split;
public AbsAAFLocator(Access access, String name, final long refreshMin) throws LocatorException {
- aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL, null);
- if(aaf_locator_host==null) {
- aaf_locator_uri = null;
- } else {
- try {
- aaf_locator_uri = new URI(aaf_locator_host);
- } catch (URISyntaxException e) {
- throw new LocatorException(e);
- }
+ RegistrationPropHolder rph;
+ try {
+ rph = new RegistrationPropHolder(access, 0);
+ } catch (UnknownHostException | CadiException e1) {
+ throw new LocatorException(e1);
+ }
+ try {
+ aaf_locator_host = rph.replacements("https://"+Config.AAF_LOCATE_URL_TAG,null,null);
+ aaf_locator_uri = new URI(aaf_locator_host);
+ access.printf(Level.INFO, "AbsAAFLocator AAF URI is %s",aaf_locator_uri);
+ } catch (URISyntaxException e) {
+ throw new LocatorException(e);
}
+ name = rph.replacements(name, null,null);
+ access.printf(Level.INFO, "AbsAAFLocator name is %s",aaf_locator_uri);
+
epList = new LinkedList<>();
refreshWait = refreshMin;
this.access = access;
String lat = access.getProperty(Config.CADI_LATITUDE,null);
String lng = access.getProperty(Config.CADI_LONGITUDE,null);
- if(lat==null || lng==null) {
+ if (lat==null || lng==null) {
throw new LocatorException(Config.CADI_LATITUDE + " and " + Config.CADI_LONGITUDE + " properties are required.");
} else {
latitude = Double.parseDouble(lat);
longitude = Double.parseDouble(lng);
}
- if(name.startsWith(Defaults.AAF_NS)) {
- String root_ns = access.getProperty(Config.AAF_ROOT_NS, null);
- if(root_ns!=null) {
- name=name.replace(Defaults.AAF_NS, root_ns);
- }
- }
- if(name.startsWith("http")) { // simple URL
+
+
+ if (name.startsWith("http")) { // simple URL
this.name = name;
- this.version = Config.AAF_DEFAULT_VERSION;
+ this.version = access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
} else {
String[] split = Split.split(':', name);
this.name = split[0];
- this.version = (split.length > 1) ? split[1] : Config.AAF_DEFAULT_VERSION;
+ this.version = (split.length > 1) ? split[1] : access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
}
}
public static void setCreator(LocatorCreator lc) {
locatorCreator = lc;
}
-
- public static Locator<URI> create(String key) throws LocatorException {
- String name = null;
- String version = Config.AAF_DEFAULT_VERSION;
- String pathInfo = null;
- int prev = key.indexOf("/locate");
- if(prev>0) {
- prev = key.indexOf('/',prev+6);
- if(prev>0) {
- int next = key.indexOf('/',++prev);
- if(next>0) {
- name = key.substring(prev, next);
- pathInfo=key.substring(next);
- } else {
- name = key.substring(prev);
- }
- String[] split = Split.split(':', name);
- switch(split.length) {
- case 3:
- case 2:
- version = split[1];
- name = split[0];
- break;
- default:
- break;
- }
- }
- }
-
- if(key.startsWith("http")) {
- if(name!=null) {
- if(locatorCreator != null) {
- AbsAAFLocator<?> aal = locatorCreator.create(name, version);
- if(pathInfo!=null) {
- aal.setPathInfo(pathInfo);
- }
- return aal;
- }
- } else {
- return new PropertyLocator(key);
- }
- }
- return null;
- }
-
+
public static Locator<URI> create(final String name, final String version) throws LocatorException {
return locatorCreator.create(name, version);
}
protected static String nameFromLocatorURI(URI locatorURI) {
String[] path = Split.split('/', locatorURI.getPath());
- if(path.length>2 && "locate".equals(path[1])) {
- return path[2];
+ if (path.length>1 && "locate".equals(path[1])) {
+ return path[2];
+ } else if(path.length>1) {
+ return path[1];
} else {
return locatorURI.toString();
}
public static void setCreatorSelf(final String hostname, final int port) {
- if(locatorCreator!=null) {
+ if (locatorCreator!=null) {
locatorCreator.setSelf(hostname,port);
}
}
private final boolean _refresh() {
boolean rv = false;
long now=System.currentTimeMillis();
- if(noEntries()) {
- if(earliest<now) {
+ if (noEntries()) {
+ if (earliest<now) {
synchronized(epList) {
rv = refresh();
earliest = now + refreshWait; // call only up to 10 seconds.
@Override
public URI get(Item item) throws LocatorException {
- if(item==null) {
+ if (item==null) {
return null;
- } else if(item instanceof AAFLItem) {
+ } else if (item instanceof AAFLItem) {
return getURI(((AAFLItem)item).uri);
} else {
throw new LocatorException(item.getClass().getName() + " does not belong to AAFLocator");
@Override
public boolean hasItems() {
boolean isEmpty = epList.isEmpty();
- if(!isEmpty) {
- for(Iterator<EP> iter = epList.iterator(); iter.hasNext(); ) {
+ if (!isEmpty) {
+ for (Iterator<EP> iter = epList.iterator(); iter.hasNext(); ) {
EP ep = iter.next();
- if(ep.valid) {
+ if (ep.valid) {
return true;
}
}
isEmpty = true;
}
- if(_refresh()) { // is refreshed... check again
+ if (_refresh()) { // is refreshed... check again
isEmpty = epList.isEmpty();
}
return !isEmpty;
@Override
public void invalidate(Item item) throws LocatorException {
- if(item!=null) {
- if(item instanceof AAFLItem) {
+ if (item!=null) {
+ if (item instanceof AAFLItem) {
AAFLItem ali =(AAFLItem)item;
EP ep = ali.ep;
synchronized(epList) {
@Override
public Item best() throws LocatorException {
- if(!hasItems()) {
+ if (!hasItems()) {
throw new LocatorException("No Entries found for '" + aaf_locator_uri.toString() + "/locate/" + name + ':' + version + '\'');
}
List<EP> lep = new ArrayList<>();
// Note: Deque is sorted on the way by closest distance
Iterator<EP> iter = getIterator();
EP ep;
- while(iter.hasNext()) {
+ while (iter.hasNext()) {
ep = iter.next();
- if(ep.valid) {
- if(first==null) {
+ if (ep.valid) {
+ if (first==null) {
first = ep;
lep.add(first);
} else {
- if(Math.abs(ep.distance-first.distance)<.1) { // allow for nearby/precision issues.
+ if (Math.abs(ep.distance-first.distance)<.1) { // allow for nearby/precision issues.
lep.add(ep);
} else {
break;
default:
int rand = sr.nextInt(); // Sonar chokes without.
int i = Math.abs(rand)%lep.size();
- if(i<0) {
+ if (i<0) {
return null;
} else {
return new AAFLItem(iter,lep.get(i));
private Iterator<EP> getIterator() {
Object[] epa = epList.toArray();
- if(epa.length==0) {
+ if (epa.length==0) {
_refresh();
epa = epList.toArray();
}
@Override
public boolean hasNext() {
- if(idx<0) {
+ if (idx<0) {
return false;
} else {
Object obj;
- while(idx<epa.length) {
- if((obj=epa[idx])==null || !((EP)obj).valid) {
+ while (idx<epa.length) {
+ if ((obj=epa[idx])==null || !((EP)obj).valid) {
++idx;
continue;
}
@Override
public EP next() {
- if(!hasNext() ) {
+ if (!hasNext() ) {
throw new NoSuchElementException();
}
return (EP)epa[idx++];
@Override
public void remove() {
- if(idx>=0 && idx<epa.length) {
+ if (idx>=0 && idx<epa.length) {
synchronized(epList) {
epList.remove(epa[idx]);
}
public Item first() {
Iterator<EP> iter = getIterator();
EP ep = AAFLItem.next(iter);
- if(ep==null) {
+ if (ep==null) {
return null;
}
return new AAFLItem(iter,ep);
@Override
public Item next(Item prev) throws LocatorException {
- if(prev==null) {
+ if (prev==null) {
StringBuilder sb = new StringBuilder("Locator Item passed in next(item) is null.");
int lines = 0;
- for(StackTraceElement st : Thread.currentThread().getStackTrace()) {
+ for (StackTraceElement st : Thread.currentThread().getStackTrace()) {
sb.append("\n\t");
sb.append(st.toString());
- if(++lines > 5) {
+ if (++lines > 5) {
sb.append("\n\t...");
break;
}
}
access.log(Level.ERROR, sb);
} else {
- if(prev instanceof AAFLItem) {
+ if (prev instanceof AAFLItem) {
AAFLItem ali = (AAFLItem)prev;
EP ep = AAFLItem.next(ali.iter);
- if(ep!=null) {
+ if (ep!=null) {
return new AAFLItem(ali.iter,ep);
}
} else {
private static EP next(Iterator<EP> iter) {
EP ep=null;
- while(iter.hasNext() && (ep==null || !ep.valid)) {
+ while (iter.hasNext() && (ep==null || !ep.valid)) {
ep = iter.next();
}
return ep;
@Override
public int compareTo(EP o) {
- if(distance<o.distance) {
+ if (distance<o.distance) {
return -1;
- } else if(distance>o.distance) {
+ } else if (distance>o.distance) {
return 1;
} else {
return 0;
protected abstract URI getURI();
protected URI getURI(URI rv) throws LocatorException {
- if(additional) {
+ if (additional) {
try {
return new URI(rv.getScheme(),rv.getUserInfo(),rv.getHost(),rv.getPort(),pathInfo,query,fragment);
} catch (URISyntaxException e) {