2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
21 package org.onap.aaf.cadi.config;
23 import java.net.Inet4Address;
24 import java.net.UnknownHostException;
26 import org.onap.aaf.cadi.Access;
27 import org.onap.aaf.cadi.Access.Level;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.util.Split;
31 public class RegistrationPropHolder {
32 private final String PUBLIC_NAME="%NS.%N";
33 private final String REGI="RegistrationProperty: %s='%s'";
34 private final Access access;
35 public String hostname;
37 public String public_fqdn;
38 private Integer public_port;
39 public Float latitude;
40 public Float longitude;
41 public final String default_fqdn;
42 public final String default_container_ns;
43 public final String default_name;
44 public final String lentries;
45 public final String lcontainer;
46 public final String default_container;
47 private static boolean firstlog = true;
49 public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
51 StringBuilder errs = new StringBuilder();
55 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
56 default_container = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
58 access.printf(Level.INIT, REGI,"default_container",default_container);
60 if(!default_container.isEmpty()) {
61 lcontainer=',' + default_container; // "" makes a blank default Public Entry
62 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+default_container, null);
64 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
67 lcontainer=default_container;
68 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
71 public_port=Integer.decode(str);
74 access.printf(Level.INIT, "RegistrationProperty: public_port='%d'",public_port);
77 hostname = access.getProperty(Config.HOSTNAME, null);
79 hostname = Inet4Address.getLocalHost().getHostName();
82 mustBeDefined(errs,Config.HOSTNAME);
85 access.printf(Level.INIT, REGI,"hostname",hostname);
88 public_fqdn = access.getProperty(Config.AAF_LOCATOR_PUBLIC_FQDN, hostname);
90 access.printf(Level.INIT, REGI,"public_fqdn",public_fqdn);
93 // Allow Container to reset the standard name for public
94 String container_public_name = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME+'.'+default_container, null);
95 if(container_public_name==null) {
96 container_public_name = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME, null);
97 if(container_public_name==null) {
98 container_public_name = access.getProperty(Config.AAF_LOCATOR_NAME, PUBLIC_NAME);
101 default_name = container_public_name;
104 access.printf(Level.INIT, REGI,"default_name",default_name);
108 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
109 if(slatitude == null) {
110 mustBeDefined(errs,Config.CADI_LATITUDE);
112 latitude = Float.parseFloat(slatitude);
115 access.printf(Level.INIT, REGI,"latitude",slatitude);
119 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
120 if(slongitude == null) {
121 mustBeDefined(errs,Config.CADI_LONGITUDE);
123 longitude = Float.parseFloat(slongitude);
126 access.printf(Level.INIT, REGI,"longitude",slongitude);
130 // Note: only one of the ports can be public... Therefore, only the last
131 for(String le : Split.splitTrim(',', lcontainer)) {
132 dot_le = le.isEmpty()?le :"."+le;
133 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_FQDN+dot_le,null);
134 if( str != null && !str.isEmpty()) {
137 access.printf(Level.INIT, "RegistrationProperty: public_hostname(overloaded by %s)='%s'",dot_le,public_fqdn);
142 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
144 access.printf(Level.INIT, REGI,"default_fqdn",default_fqdn);
146 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
148 access.printf(Level.INIT, REGI,"default_container_ns",default_container_ns);
150 if(errs.length()>0) {
151 throw new CadiException(errs.toString());
156 private void mustBeDefined(StringBuilder errs, String propname) {
158 errs.append(propname);
159 errs.append(" must be defined.");
163 public String getEntryFQDN(final String entry, final String dot_le) {
165 if(public_fqdn!=null && dot_le.isEmpty()) {
168 str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
170 return replacements("RegistrationPropHolder.getEntryFQDN",str,entry,dot_le);
173 public String getEntryName(final String entry, final String dot_le) {
175 if(dot_le.isEmpty()) {
178 str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
180 return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
183 public String getPublicEntryName(final String entry, final String dot_le) {
184 String str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME+dot_le, null);
186 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME,null);
191 return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
195 private String getNS(String dot_le) {
197 ns = access.getProperty(Config.AAF_LOCATOR_APP_NS+dot_le,null);
199 ns = access.getProperty(Config.AAF_LOCATOR_APP_NS, "AAF_NS");
205 public String replacements(final String fromCode, final String source, final String name, final String _dot_le) {
208 } else if(source.isEmpty()) {
211 String value = source;
214 dot_le = default_container.isEmpty()?"":'.'+default_container;
219 String aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL+dot_le,null);
220 if(aaf_locator_host==null) {
221 aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL,null);
225 if(aaf_locator_host!=null) {
226 if("https://AAF_LOCATE_URL".equals(value)) {
227 value = aaf_locator_host;
229 str = aaf_locator_host;
230 if(value.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
231 if(!str.endsWith("/")) {
234 if(!str.endsWith("/locate/")) {
237 if(value.startsWith("http:")) {
238 value = value.replace("http://AAF_LOCATE_URL/", str);
240 value = value.replace("https://AAF_LOCATE_URL/", str);
247 int atC = value.indexOf("%C");
249 // aaf_locator_container_ns
250 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
252 value = value.replace("%CNS"+'.', str);
254 value = value.replace("%CNS", str);
256 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le,default_container);
258 value = value.replace("%C"+'.', str);
260 value = value.replace("%C", str);
263 if(value.indexOf("%NS")>=0) {
265 if(str==null || str.isEmpty()) {
266 value = value.replace("%NS"+'.', "");
268 value = value.replace("%NS", str);
273 if(value.indexOf("AAF_NS")>=0) {
274 str = access.getProperty(Config.AAF_ROOT_NS, Config.AAF_ROOT_NS_DEF) + '.';
275 String temp = value.replace("%AAF_NS.", str);
276 if(temp.equals(value)) { // intended
277 value = value.replace("AAF_NS.", str); // Backward Compatibility
284 if(value.indexOf('%')>=0) {
285 // These shouldn't be expected to have dot elements
287 value = value.replace("%N", name);
289 if(default_fqdn!=null) {
290 value = value.replace("%DF", default_fqdn);
292 if(public_fqdn!=null) {
293 value = value.replace("%PH", public_fqdn);
296 access.printf(Level.DEBUG,
297 "RegistrationReplacement from %s, source: %s, dot_le: %s, value: %s",
298 fromCode,source,dot_le,value);
303 public int getEntryPort(final String dot_le) {
304 return public_port!=null && dot_le.isEmpty()?
309 public Access access() {