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.util.Split;
30 public class RegistrationPropHolder {
32 private final Access access;
33 public String hostname;
35 public String public_hostname;
36 private Integer public_port;
37 public Float latitude;
38 public Float longitude;
39 public final String default_fqdn;
40 public final String default_container_ns;
41 public final String lentries;
42 public final String lcontainer;
44 public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
46 StringBuilder errs = new StringBuilder();
50 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
52 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
54 lcontainer=',' + str; // "" makes a blank default Public Entry
55 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+str, null);
57 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
61 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
64 public_port=Integer.decode(str);
67 hostname = access.getProperty(Config.HOSTNAME, null);
69 hostname = Inet4Address.getLocalHost().getHostName();
72 mustBeDefined(errs,Config.HOSTNAME);
75 public_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname);
78 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
79 if(slatitude == null) {
80 mustBeDefined(errs,Config.CADI_LATITUDE);
82 latitude = Float.parseFloat(slatitude);
86 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
87 if(slongitude == null) {
88 mustBeDefined(errs,Config.CADI_LONGITUDE);
90 longitude = Float.parseFloat(slongitude);
94 // Note: only one of the ports can be public... Therefore, only the last
95 for(String le : Split.splitTrim(',', lcontainer)) {
96 dot_le = le.isEmpty()?"":"."+le;
97 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null);
103 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, public_hostname);
104 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
106 if(errs.length()>0) {
107 throw new CadiException(errs.toString());
111 private void mustBeDefined(StringBuilder errs, String propname) {
113 errs.append(propname);
114 errs.append(" must be defined.");
118 public String getEntryFQDN(final String entry, final String dot_le) {
120 if(public_hostname!=null && dot_le.isEmpty()) {
121 str = public_hostname;
123 str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, null);
125 str = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
128 return replacements(str,entry,dot_le);
131 public String getEntryName(final String entry, final String dot_le) {
133 str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, "%NS.%N");
134 return replacements(str,entry,dot_le);
138 private String getNS(String dot_le) {
140 ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null);
142 ns = access.getProperty(Config.AAF_ROOT_NS, "");
148 public String replacements(String source, final String name, final String dot_le) {
151 } else if(source.isEmpty()) {
156 if(source.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
157 str = access.getProperty(Config.AAF_LOCATE_URL, null);
159 if(!str.endsWith("/")) {
162 if(!str.endsWith("/locate/")) {
165 source = source.replace("https://AAF_LOCATE_URL/", str);
169 if(source.indexOf("%NS")>=0) {
171 if(str==null || str.isEmpty()) {
172 source = source.replace("%NS"+'.', str);
174 source = source.replace("%NS", str);
178 if(source.indexOf("AAF_NS")>=0) {
179 str = access.getProperty(Config.AAF_ROOT_NS, null);
181 String temp = source.replace("%AAF_NS", str);
182 if(temp == source) { // intended
183 source = source.replace("AAF_NS", str); // Backward Compatibility
190 int atC = source.indexOf("%C");
192 // aaf_locator_container_ns
193 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
195 source = source.replace("%CNS"+'.', str);
197 source = source.replace("%CNS", str);
199 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le, "");
201 source = source.replace("%C"+'.', str);
203 source = source.replace("%C", str);
206 if(source.indexOf('%')>=0) {
207 // These shouldn't be expected to have dot elements
208 source = source.replace("%N", name);
209 source = source.replace("%DF", default_fqdn);
210 source = source.replace("%PH", public_hostname);
215 public int getEntryPort(final String dot_le) {
216 return public_port!=null && dot_le.isEmpty()?