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.CadiException;
28 import org.onap.aaf.cadi.Access.Level;
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_hostname;
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_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname);
90 access.printf(Level.INIT, REGI,"public_hostname",public_hostname);
93 default_name = access.getProperty(Config.AAF_LOCATOR_NAME, PUBLIC_NAME);
95 access.printf(Level.INIT, REGI,"default_name",default_name);
99 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
100 if(slatitude == null) {
101 mustBeDefined(errs,Config.CADI_LATITUDE);
103 latitude = Float.parseFloat(slatitude);
106 access.printf(Level.INIT, REGI,"latitude",slatitude);
110 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
111 if(slongitude == null) {
112 mustBeDefined(errs,Config.CADI_LONGITUDE);
114 longitude = Float.parseFloat(slongitude);
117 access.printf(Level.INIT, REGI,"longitude",slongitude);
121 // Note: only one of the ports can be public... Therefore, only the last
122 for(String le : Split.splitTrim(',', lcontainer)) {
123 dot_le = le.isEmpty()?le :"."+le;
124 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null);
125 if( str != null && !str.isEmpty()) {
128 access.printf(Level.INIT, "RegistrationProperty: public_hostname(overloaded by %s)='%s'",dot_le,public_hostname);
133 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
135 access.printf(Level.INIT, REGI,"default_fqdn",default_fqdn);
137 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
139 access.printf(Level.INIT, REGI,"default_container_ns",default_container_ns);
141 if(errs.length()>0) {
142 throw new CadiException(errs.toString());
147 private void mustBeDefined(StringBuilder errs, String propname) {
149 errs.append(propname);
150 errs.append(" must be defined.");
154 public String getEntryFQDN(final String entry, final String dot_le) {
156 if(public_hostname!=null && dot_le.isEmpty()) {
157 str = public_hostname;
159 str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
161 return replacements("RegistrationPropHolder.getEntryFQDN",str,entry,dot_le);
164 public String getEntryName(final String entry, final String dot_le) {
166 if(dot_le.isEmpty()) {
169 str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
171 return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
175 private String getNS(String dot_le) {
177 ns = access.getProperty(Config.AAF_LOCATOR_APP_NS+dot_le,null);
179 ns = access.getProperty(Config.AAF_LOCATOR_APP_NS, "");
185 public String replacements(final String fromCode, final String source, final String name, final String _dot_le) {
188 } else if(source.isEmpty()) {
191 String value = source;
194 dot_le = default_container.isEmpty()?"":'.'+default_container;
199 String aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL+dot_le,null);
200 if(aaf_locator_host==null) {
201 aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL,null);
205 if(aaf_locator_host!=null) {
206 if("https://AAF_LOCATE_URL".equals(value)) {
207 value = aaf_locator_host;
209 str = aaf_locator_host;
210 if(value.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
211 if(!str.endsWith("/")) {
214 if(!str.endsWith("/locate/")) {
217 value = value.replace("https://AAF_LOCATE_URL/", str);
222 int atC = value.indexOf("%C");
224 // aaf_locator_container_ns
225 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
227 value = value.replace("%CNS"+'.', str);
229 value = value.replace("%CNS", str);
231 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le,default_container);
233 value = value.replace("%C"+'.', str);
235 value = value.replace("%C", str);
238 if(value.indexOf("%NS")>=0) {
240 if(str==null || str.isEmpty()) {
241 value = value.replace("%NS"+'.', str);
243 value = value.replace("%NS", str);
247 if(value.indexOf("AAF_NS")>=0) {
248 str = access.getProperty(Config.AAF_ROOT_NS, Config.AAF_ROOT_NS_DEF);
249 String temp = value.replace("%AAF_NS", str);
250 if(temp.equals(value)) { // intended
251 value = value.replace("AAF_NS", str); // Backward Compatibility
258 if(value.indexOf('%')>=0) {
259 // These shouldn't be expected to have dot elements
261 value = value.replace("%N", name);
263 if(default_fqdn!=null) {
264 value = value.replace("%DF", default_fqdn);
266 if(public_hostname!=null) {
267 value = value.replace("%PH", public_hostname);
270 access.printf(Level.DEBUG,
271 "RegistrationReplacement from %s, source: %s, dot_le: %s, value: %s",
272 fromCode,source,dot_le,value);
277 public int getEntryPort(final String dot_le) {
278 return public_port!=null && dot_le.isEmpty()?
283 public Access access() {