Merge "Sonar fix: Perm.java"
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / register / RegistrationCreator.java
index 5221d8e..fc3c18b 100644 (file)
@@ -51,9 +51,11 @@ public class RegistrationCreator {
        try {
                String dot_le;
                String version=null;
+               String defProtocol="https";
                
                RegistrationPropHolder ph = new RegistrationPropHolder(access, port);
                
+               String firstPrivateHostname = null;
                // Now, loop through by Container
                for(String le : Split.splitTrim(',', ph.lcontainer)) {
                        if(le.isEmpty()) {
@@ -67,7 +69,7 @@ public class RegistrationCreator {
                                        defData = locate = new MgmtEndpoint();
 
                                        version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION);
-                                       locate.setProtocol(access.getProperty(Config.AAF_LOCATOR_PROTOCOL,null));
+                                       locate.setProtocol(defProtocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL,defProtocol));
                                        List<String> ls = locate.getSubprotocol();
                                        for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) {
                                                ls.add(sp);     
@@ -80,7 +82,23 @@ public class RegistrationCreator {
                                }
                                
                                locate.setName(ph.getEntryName(entry,dot_le));
-                               locate.setHostname(ph.getEntryFQDN(entry,dot_le));
+                               /* Cover the situation where there is a Container, and multiple locator Entries,
+                                * the first of which is the only real private FQDN
+                                * example: oauth
+                                *      aaf_locator_entries=oauth,token,introspect
+                                *      
+                                *      Entries for token and introspect, but they point to oauth service.
+                                */
+                               String locateHostname;
+                               if(le.isEmpty()) {                      
+                                       locateHostname=ph.getEntryFQDN(entry, dot_le);
+                               } else if(firstPrivateHostname==null) {
+                                       firstPrivateHostname=locateHostname=ph.getEntryFQDN(entry, dot_le);
+                               } else {
+                                       locateHostname=firstPrivateHostname;
+                               }
+                               
+                               locate.setHostname(locateHostname);
                                locate.setPort(ph.getEntryPort(dot_le));
                                
                                String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
@@ -89,17 +107,19 @@ public class RegistrationCreator {
                                }
                                if(specificVersion!=null) {
                                        String split[] = Split.splitTrim('.', specificVersion);
-                                       locate.setPkg(split.length>3?Integer.parseInt(split[3]):0);
-                                       locate.setPatch(split.length>2?Integer.parseInt(split[2]):0);
-                                       locate.setMinor(split.length>1?Integer.parseInt(split[1]):0);
-                                       locate.setMajor(split.length>0?Integer.parseInt(split[0]):0);
+                                       String deply[]= Split.splitTrim('.', access.getProperty(Config.AAF_DEPLOYED_VERSION, ""));
+                                       locate.setMajor(best(split,deply,0));
+                                       locate.setMinor(best(split,deply,1));
+                                       locate.setPatch(best(split,deply,2));
+                                       locate.setPkg(best(split,deply,3));
                                }
 
-                               String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, null);
+                               String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, defProtocol);
                                if (protocol!=null) {
                                        locate.setProtocol(protocol);
                                                List<String> ls = locate.getSubprotocol();
-                                               if(ls==null || ls.isEmpty()) {
+                                               // ls cannot be null, per generated getSubprotocol code
+                                               if(ls.isEmpty()) {
                                                String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
                                                if(subprotocols==null) {
                                                        subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
@@ -129,7 +149,39 @@ public class RegistrationCreator {
        return me;
     }
        
-    private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
+    /*
+     * Find the best version between Actual Interface and Deployed version
+     */
+    private int best(String[] split, String[] deploy, int i) {
+       StringBuilder sb = new StringBuilder();
+       char c;
+               String s;
+       if(split.length>i) {
+               s=split[i];
+                       for(int j=0;j<s.length();++j) {
+                               if(Character.isDigit(c=s.charAt(j))) {
+                                       sb.append(c);
+                               } else {
+                                       break;
+                               }
+                       }
+       }       
+               
+               if(sb.length()==0 && deploy.length>i) {
+                       s=deploy[i];
+                       for(int j=0;j<s.length();++j) {
+                               if(Character.isDigit(c=s.charAt(j))) {
+                                       sb.append(c);
+                               } else {
+                                       break;
+                               }
+                       }
+               }
+               
+               return sb.length()==0?0:Integer.parseInt(sb.toString());
+    }
+
+       private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
        int cnt = 0;
                for(MgmtEndpoint m : lme) {
                        print(sb,cnt++,m);
@@ -153,9 +205,9 @@ public class RegistrationCreator {
                out.append('.');
                out.append(mep.getMinor());
                out.append('.');
-               out.append(mep.getPkg());
-               out.append('.');
                out.append(mep.getPatch());
+               out.append('.');
+               out.append(mep.getPkg());
                out.append("\n\tPort:       ");
                out.append(mep.getPort());
                out.append("\n\tProtocol:   ");
@@ -199,8 +251,8 @@ public class RegistrationCreator {
                out.setLongitude(mep.getLongitude());
                out.setMajor(mep.getMajor());
                out.setMinor(mep.getMinor());
-               out.setPkg(mep.getPkg());
                out.setPatch(mep.getPatch());
+               out.setPkg(mep.getPkg());
                out.setPort(mep.getPort());
                out.setProtocol(mep.getProtocol());
                out.getSpecialPorts().addAll(mep.getSpecialPorts());