Improved multi Proxy DNSLocator based
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / v2_0 / AbsAAFLocator.java
index 9b630a7..a654e6f 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.aaf.cadi.aaf.v2_0;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.UnknownHostException;
 import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -32,10 +33,11 @@ import java.util.NoSuchElementException;
 
 import org.onap.aaf.cadi.Access;
 import org.onap.aaf.cadi.Access.Level;
+import org.onap.aaf.cadi.CadiException;
 import org.onap.aaf.cadi.Locator;
 import org.onap.aaf.cadi.LocatorException;
-import org.onap.aaf.cadi.aaf.Defaults;
 import org.onap.aaf.cadi.config.Config;
+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;
@@ -58,23 +60,34 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     protected String myhostname;
     protected int myport;
     protected final String aaf_locator_host;
-    protected final URI aaf_locator_uri;
+    protected URI aaf_locator_uri;
     private long earliest;
     private final long refreshWait;
 
 
     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 {
+        RegistrationPropHolder rph;
+        try {
+            rph = new RegistrationPropHolder(access, 0);
+        } catch (UnknownHostException | CadiException e1) {
+            throw new LocatorException(e1);
+        }
+        URI aaf_locator_uri;
+        try {
+            aaf_locator_host = rph.replacements(getClass().getSimpleName(),"https://"+Config.AAF_LOCATE_URL_TAG,null,null);
+            if(aaf_locator_host.endsWith("/locate")) {
                 aaf_locator_uri = new URI(aaf_locator_host);
-            } catch (URISyntaxException e) {
-                throw new LocatorException(e);
+            } else {
+                aaf_locator_uri = new URI(aaf_locator_host+"/locate");
             }
+            access.printf(Level.INFO, "AbsAAFLocator AAF URI is %s",aaf_locator_uri);
+        } catch (URISyntaxException e) {
+            throw new LocatorException(e);
         }
 
+        name = rph.replacements(getClass().getSimpleName(),name, null,null);
+        access.printf(Level.INFO, "AbsAAFLocator name is %s",aaf_locator_uri);
+
         epList = new LinkedList<>();
         refreshWait = refreshMin;
 
@@ -88,12 +101,6 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
             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
             this.name = name;
@@ -103,9 +110,8 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
             this.name = split[0];
             this.version = (split.length > 1) ? split[1] : access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
         }
-        
     }
-
+    
     /**
      * This is the way to setup specialized AAFLocators ahead of time.
      * @param preload
@@ -115,6 +121,9 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     }
         
     public static Locator<URI> create(final String name, final String version) throws LocatorException {
+        if(locatorCreator==null) {
+            throw new LocatorException("LocatorCreator is not set");
+        }
         return locatorCreator.create(name, version);
     }
 
@@ -128,7 +137,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
         if (path.length>1 && "locate".equals(path[1])) {
            return path[2];
         } else if(path.length>1) {
-               return path[1];
+             return path[1];
         } else {
             return locatorURI.toString();
         }
@@ -227,7 +236,10 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
     @Override
     public Item best() throws LocatorException {
         if (!hasItems()) {
-            throw new LocatorException("No Entries found for '" + aaf_locator_uri.toString() + "/locate/" + name + ':' + version + '\'');
+            throw new LocatorException(String.format("No Entries found for '%s/%s:%s'",
+                       (aaf_locator_uri==null?aaf_locator_host:aaf_locator_uri.toString()),
+                       name,
+                       version));
         }
         List<EP> lep = new ArrayList<>();
         EP first = null;
@@ -358,7 +370,7 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
         }
         return null;
     }
-
+    
     protected static class AAFLItem implements Item {
             private Iterator<EP> iter;
             private URI uri;
@@ -460,5 +472,10 @@ public abstract class AbsAAFLocator<TRANS extends Trans> implements Locator<URI>
         return rv;
     }
 
+    protected void clear() {
+        epList.clear();
+        earliest=0L;
+    }
+
 
 }