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 {
31 private final String PUBLIC_NAME="%NS.%N";
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 default_name;
42 public final String lentries;
43 public final String lcontainer;
44 public final String default_container;
46 public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
48 StringBuilder errs = new StringBuilder();
52 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
54 default_container = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
55 if(!default_container.isEmpty()) {
56 lcontainer=',' + default_container; // "" makes a blank default Public Entry
57 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+default_container, null);
59 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
62 lcontainer=default_container;
63 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
66 public_port=Integer.decode(str);
69 hostname = access.getProperty(Config.HOSTNAME, null);
71 hostname = Inet4Address.getLocalHost().getHostName();
74 mustBeDefined(errs,Config.HOSTNAME);
77 public_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname);
79 default_name = access.getProperty(Config.AAF_LOCATOR_NAME, "%CNS.%NS.%N");
82 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
83 if(slatitude == null) {
84 mustBeDefined(errs,Config.CADI_LATITUDE);
86 latitude = Float.parseFloat(slatitude);
90 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
91 if(slongitude == null) {
92 mustBeDefined(errs,Config.CADI_LONGITUDE);
94 longitude = Float.parseFloat(slongitude);
98 // Note: only one of the ports can be public... Therefore, only the last
99 for(String le : Split.splitTrim(',', lcontainer)) {
100 dot_le = le.isEmpty()?"":"."+le;
101 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null);
107 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
108 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
110 if(errs.length()>0) {
111 throw new CadiException(errs.toString());
115 private void mustBeDefined(StringBuilder errs, String propname) {
117 errs.append(propname);
118 errs.append(" must be defined.");
122 public String getEntryFQDN(final String entry, final String dot_le) {
124 if(public_hostname!=null && dot_le.isEmpty()) {
125 str = public_hostname;
127 str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
129 return replacements(str,entry,dot_le);
132 public String getEntryName(final String entry, final String dot_le) {
134 if(dot_le.isEmpty()) {
137 str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
139 return replacements(str,entry,dot_le);
143 private String getNS(String dot_le) {
145 ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null);
147 ns = access.getProperty(Config.AAF_ROOT_NS, "");
153 public String replacements(String source, final String name, final String _dot_le) {
156 } else if(source.isEmpty()) {
162 dot_le = default_container.isEmpty()?"":'.'+default_container;
167 String aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL+dot_le,null);
168 if(aaf_locator_host==null) {
169 aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL,null);
173 if(aaf_locator_host!=null) {
174 if("https://AAF_LOCATE_URL".equals(source)) {
175 source = aaf_locator_host;
177 str = aaf_locator_host;
178 if(source.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
179 if(!str.endsWith("/")) {
182 if(!str.endsWith("/locate/")) {
185 source = source.replace("https://AAF_LOCATE_URL/", str);
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,default_container);
201 source = source.replace("%C"+'.', str);
203 source = source.replace("%C", str);
206 if(source.indexOf("%NS")>=0) {
208 if(str==null || str.isEmpty()) {
209 source = source.replace("%NS"+'.', str);
211 source = source.replace("%NS", str);
215 if(source.indexOf("AAF_NS")>=0) {
216 str = access.getProperty(Config.AAF_ROOT_NS, Config.AAF_ROOT_NS_DEF);
217 String temp = source.replace("%AAF_NS", str);
218 if(temp.equals(source)) { // intended
219 source = source.replace("AAF_NS", str); // Backward Compatibility
226 if(source.indexOf('%')>=0) {
227 // These shouldn't be expected to have dot elements
229 source = source.replace("%N", name);
231 if(default_fqdn!=null) {
232 source = source.replace("%DF", default_fqdn);
234 if(public_hostname!=null) {
235 source = source.replace("%PH", public_hostname);
241 public int getEntryPort(final String dot_le) {
242 return public_port!=null && dot_le.isEmpty()?
247 public Access access() {