diff options
author | Sai Gandham <sai.gandham@att.com> | 2019-02-01 15:13:16 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-02-01 15:13:16 +0000 |
commit | e36daf12cf4c5aa4b22fa3cec66a79ff2e2b8b94 (patch) | |
tree | 76d8b5e81d853e33a06f86c4478f5616e83a1b51 /cadi | |
parent | 73223229c68129715fcd7b0c9d128345f13b3b4d (diff) | |
parent | 3505a52ac853c134ecaf40f2977667de8770ec59 (diff) |
Merge "Implement public private locator"
Diffstat (limited to 'cadi')
8 files changed, 657 insertions, 209 deletions
diff --git a/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/TestConnectivity.java b/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/TestConnectivity.java index 30ea1c21..bf7ed6d9 100644 --- a/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/TestConnectivity.java +++ b/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/TestConnectivity.java @@ -46,6 +46,7 @@ import org.onap.aaf.cadi.config.SecurityInfoC; import org.onap.aaf.cadi.http.HBasicAuthSS; import org.onap.aaf.cadi.http.HClient; import org.onap.aaf.cadi.http.HX509SS; +import org.onap.aaf.cadi.locator.SingleEndpointLocator; import org.onap.aaf.cadi.oauth.HRenewingTokenSS; import org.onap.aaf.cadi.util.FixURIinfo; import org.onap.aaf.misc.env.APIException; @@ -80,45 +81,56 @@ public class TestConnectivity { List<SecuritySetter<HttpURLConnection>> lss = loadSetters(access,si); ///////// - print(true,"Test Connections driven by AAFLocator"); - URI serviceURI = uri(access,"service"); - - for (URI uri : new URI[] { - serviceURI, - uri(access,"token"), - uri(access,"introspect"), - uri(access,"cm"), - uri(access,"gui"), - uri(access,"fs"), - uri(access,"hello") - }) { - Locator<URI> locator = new AAFLocator(si, uri); - try { - connectTest(locator, uri); - } catch (Exception e) { - e.printStackTrace(); - System.err.flush(); - } - } - - ///////// - print(true,"Test Service for Perms driven by AAFLocator"); - Locator<URI> locator = new AAFLocator(si,serviceURI); - for (SecuritySetter<HttpURLConnection> ss : lss) { + String directAAFURL = access.getProperty(Config.AAF_URL,null); + if(directAAFURL!=null && !directAAFURL.contains("AAF_LOCATE")) { + print(true,"Test Connections by non-located aaf_url"); + Locator<URI> locator = new SingleEndpointLocator(directAAFURL); + connectTest(locator,new URI(directAAFURL)); + + SecuritySetter<HttpURLConnection> ss = si.defSS; permTest(locator,ss); - } + } else { + ///////// + print(true,"Test Connections driven by AAFLocator"); + URI serviceURI = uri(access,"service"); + + for (URI uri : new URI[] { + serviceURI, + uri(access,"token"), + uri(access,"introspect"), + uri(access,"cm"), + uri(access,"gui"), + uri(access,"fs"), + uri(access,"hello") + }) { + Locator<URI> locator = new AAFLocator(si, uri); + try { + connectTest(locator, uri); + } catch (Exception e) { + e.printStackTrace(); + System.err.flush(); + } + } - ////////// - print(true,"Test essential BasicAuth Service call, driven by AAFLocator"); - boolean hasBath=false; - for (SecuritySetter<HttpURLConnection> ss : lss) { - if (ss instanceof HBasicAuthSS) { - hasBath=true; - basicAuthTest(new AAFLocator(si, serviceURI),ss); - } - } - if(!hasBath) { - System.out.println("No User/Password to test"); + ///////// + print(true,"Test Service for Perms driven by AAFLocator"); + Locator<URI> locator = new AAFLocator(si,serviceURI); + for (SecuritySetter<HttpURLConnection> ss : lss) { + permTest(locator,ss); + } + + ////////// + print(true,"Test essential BasicAuth Service call, driven by AAFLocator"); + boolean hasBath=false; + for (SecuritySetter<HttpURLConnection> ss : lss) { + if (ss instanceof HBasicAuthSS) { + hasBath=true; + basicAuthTest(new AAFLocator(si, serviceURI),ss); + } + } + if(!hasBath) { + System.out.println("No User/Password to test"); + } } } catch (Exception e) { @@ -266,13 +278,16 @@ public class TestConnectivity { if (ss instanceof HRenewingTokenSS) { System.out.println(" " + ((HRenewingTokenSS)ss).tokenURL()); } else { - System.out.println(); + System.out.println(); } HClient client = new HClient(ss, uri, 3000); client.setMethod("GET"); String user = ss.getID(); - client.setPathInfo("/authz/perms/user/"+user); + String pathInfo = "/authz/perms/user/"+user; + client.setPathInfo(pathInfo); + System.out.println(pathInfo); + client.send(); Future<String> future = client.futureReadString(); if (future.get(7000)) { diff --git a/cadi/aaf/src/main/java/org/onap/aaf/cadi/register/RegistrationCreator.java b/cadi/aaf/src/main/java/org/onap/aaf/cadi/register/RegistrationCreator.java index 7e519c52..aba13fb4 100644 --- a/cadi/aaf/src/main/java/org/onap/aaf/cadi/register/RegistrationCreator.java +++ b/cadi/aaf/src/main/java/org/onap/aaf/cadi/register/RegistrationCreator.java @@ -20,7 +20,6 @@ package org.onap.aaf.cadi.register; -import java.net.Inet4Address; import java.net.UnknownHostException; import java.util.List; @@ -28,13 +27,13 @@ import org.onap.aaf.cadi.Access; import org.onap.aaf.cadi.CadiException; 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.util.Split; import locate.v1_0.MgmtEndpoint; import locate.v1_0.MgmtEndpoints; public class RegistrationCreator { - private static final String MUST_BE_DEFINED = " must be defined\n"; private Access access; public RegistrationCreator(Access access) { @@ -47,171 +46,41 @@ public class RegistrationCreator { MgmtEndpoint defData = null; MgmtEndpoint locate = null; - - StringBuilder errs = new StringBuilder(); try { - String hostname = access.getProperty(Config.HOSTNAME, null); - if (hostname==null) { - hostname = Inet4Address.getLocalHost().getHostName(); - } - if (hostname==null) { - errs.append(Config.HOSTNAME); - errs.append(MUST_BE_DEFINED); - } - - Float latitude=null; - String slatitude = access.getProperty(Config.CADI_LATITUDE, null); - if(slatitude == null) { - errs.append(Config.CADI_LATITUDE); - errs.append(MUST_BE_DEFINED); - } else { - latitude = Float.parseFloat(slatitude); - } - - Float longitude=null; - String slongitude = access.getProperty(Config.CADI_LONGITUDE, null); - if(slongitude == null) { - errs.append(Config.CADI_LONGITUDE); - errs.append(MUST_BE_DEFINED); - } else { - longitude = Float.parseFloat(slongitude); - } - - if(errs.length()>0) { - throw new CadiException(errs.toString()); - } - String dot_le; - String ns; String version=null; - String lentries = access.getProperty(Config.AAF_LOCATOR_CONTAINER, null); - if(lentries==null) { - lentries=""; - } else { - lentries=',' + lentries; // "" makes a blank default Public Entry - } - - String defaultName = null; - String str; - int public_port = port; - // Note: only one of the ports can be public... Therefore, only the la - for(String le : Split.splitTrim(',', lentries)) { - dot_le = le.isEmpty()?"":"."+le; - str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+dot_le, null); - if(str!=null) { // Get Public Port - public_port = Integer.decode(str); - } - } - - String public_hostname = hostname; - for(String le : Split.splitTrim(',', lentries)) { - dot_le = le.isEmpty()?"":"."+le; - String ph = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null); - if( ph != null) { - public_hostname=ph; - } - } - String default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, public_hostname); + RegistrationPropHolder ph = new RegistrationPropHolder(access, port); - // Now, loop through by Container - for(String le : Split.splitTrim(',', lentries)) { - // Add variable entries - String names; - if(le.length()>0) { - dot_le = '.' + le; - names = access.getProperty(Config.AAF_LOCATOR_NAMES+dot_le,null); - if(names==null) { - // Go for Default - names = access.getProperty(Config.AAF_LOCATOR_NAMES,""); - } + for(String le : Split.splitTrim(',', ph.lcontainer)) { + if(le.isEmpty()) { + dot_le = le; } else { - dot_le = ""; - names=access.getProperty(Config.AAF_LOCATOR_NAMES,dot_le); + dot_le = "."+le; } - - for(String name : Split.splitTrim(',', names)) { + + for(String entry : Split.splitTrim(',', ph.lentries)) { if(defData==null) { defData = locate = new MgmtEndpoint(); - defaultName = name; version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION); locate.setProtocol(access.getProperty(Config.AAF_LOCATOR_PROTOCOL,null)); List<String> ls = locate.getSubprotocol(); for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) { ls.add(sp); } - locate.setLatitude(latitude); - locate.setLongitude(longitude); + locate.setLatitude(ph.latitude); + locate.setLongitude(ph.longitude); } else { locate = copy(defData); } - str = access.getProperty(Config.HOSTNAME+dot_le, null); - if(str==null) { - str = access.getProperty(Config.HOSTNAME, hostname); - } - locate.setHostname(hostname); - - ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null); - if(ns==null) { - ns = access.getProperty(Config.AAF_LOCATOR_NS,""); - } - switch(ns) { - case Defaults.AAF_NS: - ns = access.getProperty(Config.AAF_ROOT_NS, ""); - // Fallthrough on purpose. - } - - String ns_dot; - if(ns.isEmpty()) { - ns_dot = ns; - } else { - ns_dot = ns + '.'; - } - - String container_id = access.getProperty(Config.AAF_LOCATOR_CONTAINER_ID+dot_le, ""); - if(!container_id.isEmpty()) { - ns_dot = container_id + '.' + ns_dot; - } - - if(!le.isEmpty()) { - ns_dot = le + '.' + ns_dot; - } - - if(name.isEmpty()) { - locate.setName(ns_dot + defaultName); - } else { - locate.setName(ns_dot + name); - } - - if(dot_le.isEmpty()) { - locate.setHostname(access.getProperty(Config.AAF_LOCATOR_FQDN, default_fqdn)); - } else { - str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, null); - if(str==null) { - locate.setHostname(default_fqdn); - } else { - String container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, ""); - str = str.replace("%CNS", container_ns); - String container = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le, ""); - str = str.replace("%C", container); - str = str.replace("%NS", ns); - str = str.replace("%N", name); - str = str.replace("%DF", default_fqdn); - str = str.replace("%PH", public_hostname); - locate.setHostname(str); - } - } + locate.setName(ph.getEntryName(entry,dot_le)); + locate.setHostname(ph.getEntryFQDN(entry,dot_le)); + locate.setPort(ph.getEntryPort(dot_le)); - if(le.isEmpty()) { - locate.setPort(public_port); - } else { - locate.setPort(port); - } - String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null); if(specificVersion == null && locate == defData) { specificVersion = version; diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/register/test/JU_RegistrationCreator.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/register/test/JU_RegistrationCreator.java new file mode 100644 index 00000000..dc671c83 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/register/test/JU_RegistrationCreator.java @@ -0,0 +1,177 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 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==================================================== + */ + +package org.onap.aaf.cadi.register.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.net.Inet4Address; +import java.net.UnknownHostException; + +import org.junit.Test; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.register.RegistrationCreator; + +import junit.framework.Assert; +import locate.v1_0.MgmtEndpoint; + +public class JU_RegistrationCreator { + + private static final String DOT_OOM = ".oom"; + + @Test + public void test() { + PropAccess pa = new PropAccess(); + pa.setProperty(Config.CADI_LATITUDE, "32.7"); + pa.setProperty(Config.CADI_LONGITUDE, "-72.0"); + + try { + String hostname = Inet4Address.getLocalHost().getHostName(); + String entry = ""; + RegistrationCreator rc = new RegistrationCreator(pa); + int port = 999; + for(MgmtEndpoint me : rc.create(port).getMgmtEndpoint()) { + assertEquals(hostname,me.getHostname()); + assertEquals(port,me.getPort()); + assertEquals(pa.getProperty(Config.CADI_LATITUDE),Float.toString(me.getLatitude())); + assertEquals(pa.getProperty(Config.CADI_LONGITUDE),Float.toString(me.getLongitude())); + assertEquals(2,me.getMajor()); + assertEquals(1,me.getMinor()); + assertEquals(0,me.getPatch()); + assertEquals(0,me.getPkg()); + assertEquals(entry,me.getName()); + assertEquals(null,me.getProtocol()); + assertEquals(0,me.getSpecialPorts().size()); + } + + String protocol = "http"; + pa.setProperty(Config.AAF_LOCATOR_PROTOCOL, protocol); + rc = new RegistrationCreator(pa); + for(MgmtEndpoint me : rc.create(port).getMgmtEndpoint()) { + assertEquals(hostname,me.getHostname()); + assertEquals(port,me.getPort()); + assertEquals(pa.getProperty(Config.CADI_LATITUDE),Float.toString(me.getLatitude())); + assertEquals(pa.getProperty(Config.CADI_LONGITUDE),Float.toString(me.getLongitude())); + assertEquals(2,me.getMajor()); + assertEquals(1,me.getMinor()); + assertEquals(0,me.getPatch()); + assertEquals(0,me.getPkg()); + assertEquals("",me.getName()); + assertEquals(protocol,me.getProtocol()); + assertEquals(0,me.getSpecialPorts().size()); + } + + pa.setProperty(Config.AAF_LOCATOR_ENTRIES, "service"); + rc = new RegistrationCreator(pa); + for(MgmtEndpoint me : rc.create(port).getMgmtEndpoint()) { + switch(me.getName()) { + case "": + assertEquals(hostname,me.getHostname()); + assertEquals(port,me.getPort()); + break; + case "service": + assertEquals(hostname,me.getHostname()); + assertEquals(port,me.getPort()); + break; + default: + fail("unknown Locator Entry"); + } + assertEquals(pa.getProperty(Config.CADI_LATITUDE),Float.toString(me.getLatitude())); + assertEquals(pa.getProperty(Config.CADI_LONGITUDE),Float.toString(me.getLongitude())); + assertEquals(2,me.getMajor()); + assertEquals(1,me.getMinor()); + assertEquals(0,me.getPatch()); + assertEquals(0,me.getPkg()); + assertEquals(protocol,me.getProtocol()); + assertEquals(0,me.getSpecialPorts().size()); + } + + entry = "service"; + pa.setProperty(Config.AAF_LOCATOR_ENTRIES, entry); + rc = new RegistrationCreator(pa); + for(MgmtEndpoint me : rc.create(port).getMgmtEndpoint()) { + switch(me.getName()) { + case "": + assertEquals(hostname,me.getHostname()); + assertEquals(port,me.getPort()); + break; + case "service": + assertEquals(hostname,me.getHostname()); + assertEquals(port,me.getPort()); + break; + default: + fail("unknown Locator Entry"); + } + assertEquals(pa.getProperty(Config.CADI_LATITUDE),Float.toString(me.getLatitude())); + assertEquals(pa.getProperty(Config.CADI_LONGITUDE),Float.toString(me.getLongitude())); + assertEquals(2,me.getMajor()); + assertEquals(1,me.getMinor()); + assertEquals(0,me.getPatch()); + assertEquals(0,me.getPkg()); + assertEquals(protocol,me.getProtocol()); + assertEquals(0,me.getSpecialPorts().size()); + } + + pa.setProperty(Config.AAF_LOCATOR_CONTAINER, "oom"); + pa.setProperty(Config.AAF_ROOT_NS, Config.AAF_ROOT_NS_DEF); + pa.setProperty(Config.AAF_LOCATOR_NAME,"%NS.%N"); + pa.setProperty(Config.AAF_LOCATOR_NAME+DOT_OOM,"%CNS.%NS.%N"); + pa.setProperty(Config.AAF_LOCATOR_CONTAINER_NS+DOT_OOM, "onap"); + String k8s_public_hostname="k8s.public.com"; + int public_port = 30001; + + pa.setProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME,k8s_public_hostname); + pa.setProperty(Config.AAF_LOCATOR_PUBLIC_PORT+DOT_OOM,Integer.toString(public_port)); + rc = new RegistrationCreator(pa); + for(MgmtEndpoint me : rc.create(port).getMgmtEndpoint()) { + switch(me.getName()) { + case "org.osaaf.aaf.service": + assertEquals(k8s_public_hostname,me.getHostname()); + assertEquals(public_port,me.getPort()); + break; + case "onap.org.osaaf.aaf.service": + assertEquals(hostname,me.getHostname()); + assertEquals(port,me.getPort()); + break; + default: + fail("unknown Entry Name, " + me.getName()); + } + assertEquals(pa.getProperty(Config.CADI_LATITUDE),Float.toString(me.getLatitude())); + assertEquals(pa.getProperty(Config.CADI_LONGITUDE),Float.toString(me.getLongitude())); + assertEquals(2,me.getMajor()); + assertEquals(1,me.getMinor()); + assertEquals(0,me.getPatch()); + assertEquals(0,me.getPkg()); + assertEquals(protocol,me.getProtocol()); + assertEquals(0,me.getSpecialPorts().size()); + } + + + } catch (CadiException | UnknownHostException e) { + Assert.fail(e.getMessage()); + } + + + } + +} diff --git a/cadi/core/src/main/java/org/onap/aaf/cadi/config/Config.java b/cadi/core/src/main/java/org/onap/aaf/cadi/config/Config.java index 245b6539..f265947a 100644 --- a/cadi/core/src/main/java/org/onap/aaf/cadi/config/Config.java +++ b/cadi/core/src/main/java/org/onap/aaf/cadi/config/Config.java @@ -36,6 +36,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import javax.servlet.Registration; + import org.onap.aaf.cadi.AbsUserCache; import org.onap.aaf.cadi.Access; import org.onap.aaf.cadi.Access.Level; @@ -147,7 +149,7 @@ public class Config { public static final String AAF_ROOT_NS_DEF = "org.osaaf.aaf"; public static final String AAF_ROOT_COMPANY = "aaf_root_company"; public static final String AAF_LOCATE_URL = "aaf_locate_url"; //URL for AAF locator - private static final String AAF_LOCATE_URL_TAG = "AAF_LOCATE_URL"; // Name of Above for use in Config Variables. + public static final String AAF_LOCATE_URL_TAG = "AAF_LOCATE_URL"; // Name of Above for use in Config Variables. public static final String AAF_DEFAULT_API_VERSION = "2.1"; public static final String AAF_API_VERSION = "aaf_api_version"; public static final String AAF_URL = "aaf_url"; //URL for AAF... Use to trigger AAF configuration @@ -174,8 +176,9 @@ public class Config { public static final String AAF_LOCATOR_PROTOCOL = "aaf_locator_protocol"; public static final String AAF_LOCATOR_SUBPROTOCOL = "aaf_locator_subprotocol"; public static final String AAF_LOCATOR_NS = "aaf_locator_ns"; - public static final String AAF_LOCATOR_NAMES = "aaf_locator_names"; + public static final String AAF_LOCATOR_ENTRIES = "aaf_locator_entries"; public static final String AAF_LOCATOR_FQDN = "aaf_locator_fqdn"; + public static final String AAF_LOCATOR_NAME = "aaf_locator_name"; public static final String AAF_LOCATOR_PUBLIC_PORT = "aaf_locator_public_port"; public static final String AAF_LOCATOR_PUBLIC_HOSTNAME = "aaf_locator_public_hostname"; @@ -814,28 +817,37 @@ public class Config { if (_url==null) { access.log(Level.INIT,"No URL passed to 'loadLocator'. Disabled"); } else { - String url = _url.replace("/AAF_NS.", "/%C%CID%AAF_NS."); - String root_ns = access.getProperty(Config.AAF_ROOT_NS, null); - if(url.indexOf('%')>=0) { - String str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_ID, null); - if(str==null) { - url = url.replace("%CID",""); - } else { - url = url.replace("%CID",str+'.'); - } - str = access.getProperty(Config.AAF_LOCATOR_CONTAINER, null); - if(str==null) { - url = url.replace("%C",""); - } else { - url = url.replace("%C",str+'.'); - } - - if (root_ns==null) { - url = url.replace("%AAF_NS",""); - } else { - url = url.replace("%AAF_NS",root_ns); - } - } +// String url = _url.replace("/AAF_NS.", "/%C%CID%AAF_NS."); +// String root_ns = access.getProperty(Config.AAF_ROOT_NS, null); + String url; + RegistrationPropHolder rph; + try { + rph = new RegistrationPropHolder(access, 0); + url = rph.replacements(_url, null, null); + } catch (UnknownHostException | CadiException e1) { + throw new LocatorException(e1); + } + +// if(url.indexOf('%')>=0) { +// String str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_ID, null); +// if(str==null) { +// url = url.replace("%CID",""); +// } else { +// url = url.replace("%CID",str+'.'); +// } +// str = access.getProperty(Config.AAF_LOCATOR_CONTAINER, null); +// if(str==null) { +// url = url.replace("%C",""); +// } else { +// url = url.replace("%C",str+'.'); +// } +// +// if (root_ns==null) { +// url = url.replace("%AAF_NS",""); +// } else { +// url = url.replace("%AAF_NS",root_ns); +// } +// } String replacement; int idxAAFLocateUrl; if ((idxAAFLocateUrl=url.indexOf(AAF_LOCATE_URL_TAG))>0 && ((replacement=access.getProperty(AAF_LOCATE_URL, null))!=null)) { diff --git a/cadi/core/src/main/java/org/onap/aaf/cadi/config/RegistrationPropHolder.java b/cadi/core/src/main/java/org/onap/aaf/cadi/config/RegistrationPropHolder.java new file mode 100644 index 00000000..c8139646 --- /dev/null +++ b/cadi/core/src/main/java/org/onap/aaf/cadi/config/RegistrationPropHolder.java @@ -0,0 +1,220 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 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==================================================== + */ + +package org.onap.aaf.cadi.config; + +import java.net.Inet4Address; +import java.net.UnknownHostException; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.util.Split; + +public class RegistrationPropHolder { + + private final Access access; + public String hostname; + private int port; + public String public_hostname; + private Integer public_port; + public Float latitude; + public Float longitude; + public final String default_fqdn; + public final String default_container_ns; + public final String lentries; + public final String lcontainer; + + public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException { + this.access = access; + StringBuilder errs = new StringBuilder(); + String str; + this.port = port; + + lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,""); + + str = access.getProperty(Config.AAF_LOCATOR_CONTAINER, ""); + if(!str.isEmpty()) { + lcontainer=',' + str; // "" makes a blank default Public Entry + str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+str, null); + if(str==null) { + str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null); + } + } else { + lcontainer=str; + str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null); + } + if(str!=null) { + public_port=Integer.decode(str); + } + + hostname = access.getProperty(Config.HOSTNAME, null); + if (hostname==null) { + hostname = Inet4Address.getLocalHost().getHostName(); + } + if (hostname==null) { + mustBeDefined(errs,Config.HOSTNAME); + } + + public_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname); + + latitude=null; + String slatitude = access.getProperty(Config.CADI_LATITUDE, null); + if(slatitude == null) { + mustBeDefined(errs,Config.CADI_LATITUDE); + } else { + latitude = Float.parseFloat(slatitude); + } + + longitude=null; + String slongitude = access.getProperty(Config.CADI_LONGITUDE, null); + if(slongitude == null) { + mustBeDefined(errs,Config.CADI_LONGITUDE); + } else { + longitude = Float.parseFloat(slongitude); + } + + String dot_le; + // Note: only one of the ports can be public... Therefore, only the last + for(String le : Split.splitTrim(',', lcontainer)) { + dot_le = le.isEmpty()?"":"."+le; + str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null); + if( str != null) { + public_hostname=str; + } + } + + default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, public_hostname); + default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,""); + + if(errs.length()>0) { + throw new CadiException(errs.toString()); + } + } + + private void mustBeDefined(StringBuilder errs, String propname) { + errs.append('\n'); + errs.append(propname); + errs.append(" must be defined."); + + } + + public String getEntryFQDN(final String entry, final String dot_le) { + String str; + if(public_hostname!=null && dot_le.isEmpty()) { + str = public_hostname; + } else { + str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, null); + if(str==null) { + str = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname); + } + } + return replacements(str,entry,dot_le); + } + + public String getEntryName(final String entry, final String dot_le) { + String str; + str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, "%NS.%N"); + return replacements(str,entry,dot_le); + } + + + private String getNS(String dot_le) { + String ns; + ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null); + if(ns==null) { + ns = access.getProperty(Config.AAF_ROOT_NS, ""); + } + return ns; + } + + + public String replacements(String source, final String name, final String dot_le) { + if(source == null) { + return ""; + } else if(source.isEmpty()) { + return source; + } + String str; + // aaf_locate_url + if(source.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) { + str = access.getProperty(Config.AAF_LOCATE_URL, null); + if(str!=null) { + if(!str.endsWith("/")) { + str+='/'; + } + if(!str.endsWith("/locate/")) { + str+="locate/"; + } + source = source.replace("https://AAF_LOCATE_URL/", str); + } + } + + if(source.indexOf("%NS")>=0) { + str = getNS(dot_le); + if(str==null || str.isEmpty()) { + source = source.replace("%NS"+'.', str); + } + source = source.replace("%NS", str); + } + + // aaf_root_ns + if(source.indexOf("AAF_NS")>=0) { + str = access.getProperty(Config.AAF_ROOT_NS, null); + if(str!=null) { + String temp = source.replace("%AAF_NS", str); + if(temp == source) { // intended + source = source.replace("AAF_NS", str); // Backward Compatibility + } else { + source = temp; + } + } + } + + int atC = source.indexOf("%C"); + if(atC>=0) { + // aaf_locator_container_ns + str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns); + if(str.isEmpty()) { + source = source.replace("%CNS"+'.', str); + } + source = source.replace("%CNS", str); + + str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le, ""); + if(str.isEmpty()) { + source = source.replace("%C"+'.', str); + } + source = source.replace("%C", str); + } + + if(source.indexOf('%')>=0) { + // These shouldn't be expected to have dot elements + source = source.replace("%N", name); + source = source.replace("%DF", default_fqdn); + source = source.replace("%PH", public_hostname); + } + return source; + } + + public int getEntryPort(final String dot_le) { + return public_port!=null && dot_le.isEmpty()? + public_port: + port; + } +}
\ No newline at end of file diff --git a/cadi/core/src/main/java/org/onap/aaf/cadi/util/FixURIinfo.java b/cadi/core/src/main/java/org/onap/aaf/cadi/util/FixURIinfo.java index 7e3a239b..b4085361 100644 --- a/cadi/core/src/main/java/org/onap/aaf/cadi/util/FixURIinfo.java +++ b/cadi/core/src/main/java/org/onap/aaf/cadi/util/FixURIinfo.java @@ -37,7 +37,7 @@ public class FixURIinfo { public FixURIinfo(URI uri) { auth = uri.getAuthority(); host = uri.getHost(); - if(host==null) { + if(host==null || (auth!=null && auth.startsWith(host))) { if(auth!=null) { int colon = auth.indexOf(':'); if(colon >= 0 ) { diff --git a/cadi/core/src/main/java/org/onap/aaf/cadi/util/Split.java b/cadi/core/src/main/java/org/onap/aaf/cadi/util/Split.java index 97afa65c..c6f8bd0a 100644 --- a/cadi/core/src/main/java/org/onap/aaf/cadi/util/Split.java +++ b/cadi/core/src/main/java/org/onap/aaf/cadi/util/Split.java @@ -76,7 +76,7 @@ public class Split { if (count==1) { rv[0]=value.substring(start,end).trim(); } else { - int last=0; + int last=start; count=-1; for (idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,idx)) { rv[++count]=value.substring(last,idx).trim(); diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_RegistrationPropHolder.java b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_RegistrationPropHolder.java new file mode 100644 index 00000000..f3e05d9b --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_RegistrationPropHolder.java @@ -0,0 +1,155 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 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==================================================== + */ + +package org.onap.aaf.cadi.config.test; + +import static org.junit.Assert.assertEquals; + +import java.net.UnknownHostException; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.config.RegistrationPropHolder; + +public class JU_RegistrationPropHolder { + + @Test + public void testBlank() { + PropAccess pa = new PropAccess(); + RegistrationPropHolder rph; + int ju_port = 20; + try { + //////////////// + // Check Required Properties + //////////////// + try { + rph = new RegistrationPropHolder(pa,20); + } catch (CadiException e) { + Assert.assertEquals( + "\ncadi_latitude must be defined." + + "\ncadi_longitude must be defined.",e.getMessage()); + } + + try { + pa.setProperty(Config.CADI_LATITUDE, "32.7"); + rph = new RegistrationPropHolder(pa,20); + } catch (CadiException e) { + Assert.assertEquals( + "\ncadi_longitude must be defined.",e.getMessage()); + } + + pa.setProperty(Config.CADI_LONGITUDE, "-72.0"); + rph = new RegistrationPropHolder(pa,ju_port); + + //////////////// + // Validate Default Properties + //////////////// + for(String dot_le : new String[] {"",".helm"}) { + assertEquals(rph.hostname,rph.default_fqdn); + assertEquals("",rph.lcontainer); + assertEquals(rph.hostname,rph.public_hostname); + assertEquals(ju_port,rph.getEntryPort(dot_le)); + assertEquals(rph.hostname,rph.getEntryFQDN("",dot_le)); + } + + String ns = "myns"; + pa.setProperty(Config.AAF_LOCATOR_NS, ns); + for(String dot_le : new String[] {"",".helm"}) { + assertEquals(rph.hostname,rph.default_fqdn); + assertEquals("",rph.lcontainer); + assertEquals(rph.hostname,rph.public_hostname); + assertEquals(ju_port,rph.getEntryPort(dot_le)); + assertEquals(rph.hostname,rph.getEntryFQDN("",dot_le)); + } + + String ns2 = "onap"; + pa.setProperty(Config.AAF_LOCATOR_NS+".helm", ns2); + for(String dot_le : new String[] {"",".helm"}) { + String pns; + if(dot_le.isEmpty()) { + pns = ns; + } else { + pns = ns2; + } + assertEquals(rph.hostname,rph.default_fqdn); + assertEquals("",rph.lcontainer); + assertEquals(rph.hostname,rph.public_hostname); + assertEquals(ju_port,rph.getEntryPort(dot_le)); + assertEquals(rph.hostname,rph.getEntryFQDN("",dot_le)); + } + + //////////////// + // Validate Public Host and Port settings + //////////////// + String public_hostname = "com.public.hostname"; + int public_port = 999; + pa.setProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, public_hostname); + pa.setProperty(Config.AAF_LOCATOR_PUBLIC_PORT,Integer.toString(public_port)); + RegistrationPropHolder pubRPH = new RegistrationPropHolder(pa,ju_port); + assertEquals(public_hostname,pubRPH.public_hostname); + assertEquals(public_port,pubRPH.getEntryPort("")); + + + final String url = "https://aaf.osaaf.org:8095/org.osaaf.aaf.service:2.1"; + String name="theName"; + assertEquals(url,rph.replacements(url, name, "")); + + String alu = "aaf.osaaf.org:8095"; + String curl = url.replace(alu, Config.AAF_LOCATE_URL_TAG); + pa.setProperty(Config.AAF_LOCATE_URL,"https://"+alu); + assertEquals(url.replace("8095","8095/locate"),rph.replacements(curl, name, "")); + + String root_ns = "org.osaaf.aaf"; + curl = url.replace(root_ns, "AAF_NS"); + pa.setProperty(Config.AAF_ROOT_NS,root_ns); + assertEquals(url,rph.replacements(curl, name, "")); + + curl = url.replace(root_ns, "%AAF_NS"); + pa.setProperty(Config.AAF_ROOT_NS,root_ns); + assertEquals(url,rph.replacements(curl, name, "")); + + final String fqdn = "%C.%CNS.%NS.%N"; + String target = "myns.theName"; + assertEquals(target,rph.replacements(fqdn, name, "")); + + pa.setProperty(Config.AAF_LOCATOR_CONTAINER_NS+".hello", "mycontns"); + target = "mycontns.org.osaaf.aaf.theName"; + assertEquals(target,rph.replacements(fqdn, name, ".hello")); + + pa.setProperty(Config.AAF_LOCATOR_CONTAINER+".hello","hello"); + target = "hello.mycontns.org.osaaf.aaf.theName"; + assertEquals(target,rph.replacements(fqdn, name, ".hello")); + + pa.setProperty(Config.AAF_LOCATOR_CONTAINER_NS,"c_ns"); + target = "c_ns.myns.theName"; + assertEquals(target,rph.replacements(fqdn, name, "")); + + + } catch (UnknownHostException | CadiException e) { + e.printStackTrace(); + Assert.fail(); + } + } + + +} |