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;
45 public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
47 StringBuilder errs = new StringBuilder();
51 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
53 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
55 lcontainer=',' + str; // "" makes a blank default Public Entry
56 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+str, null);
58 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
62 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
65 public_port=Integer.decode(str);
68 hostname = access.getProperty(Config.HOSTNAME, null);
70 hostname = Inet4Address.getLocalHost().getHostName();
73 mustBeDefined(errs,Config.HOSTNAME);
76 public_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname);
78 default_name = access.getProperty(Config.AAF_LOCATOR_NAME, "%CNS.%NS.%N");
81 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
82 if(slatitude == null) {
83 mustBeDefined(errs,Config.CADI_LATITUDE);
85 latitude = Float.parseFloat(slatitude);
89 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
90 if(slongitude == null) {
91 mustBeDefined(errs,Config.CADI_LONGITUDE);
93 longitude = Float.parseFloat(slongitude);
97 // Note: only one of the ports can be public... Therefore, only the last
98 for(String le : Split.splitTrim(',', lcontainer)) {
99 dot_le = le.isEmpty()?"":"."+le;
100 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null);
106 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
107 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
109 if(errs.length()>0) {
110 throw new CadiException(errs.toString());
114 private void mustBeDefined(StringBuilder errs, String propname) {
116 errs.append(propname);
117 errs.append(" must be defined.");
121 public String getEntryFQDN(final String entry, final String dot_le) {
123 if(public_hostname!=null && dot_le.isEmpty()) {
124 str = public_hostname;
126 str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
128 return replacements(str,entry,dot_le);
131 public String getEntryName(final String entry, final String dot_le) {
133 if(dot_le.isEmpty()) {
136 str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
138 return replacements(str,entry,dot_le);
142 private String getNS(String dot_le) {
144 ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null);
146 ns = access.getProperty(Config.AAF_ROOT_NS, "");
152 public String replacements(String source, final String name, final String dot_le) {
155 } else if(source.isEmpty()) {
160 if(source.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
161 str = access.getProperty(Config.AAF_LOCATE_URL, null);
163 if(!str.endsWith("/")) {
166 if(!str.endsWith("/locate/")) {
169 source = source.replace("https://AAF_LOCATE_URL/", str);
173 if(source.indexOf("%NS")>=0) {
175 if(str==null || str.isEmpty()) {
176 source = source.replace("%NS"+'.', str);
178 source = source.replace("%NS", str);
182 if(source.indexOf("AAF_NS")>=0) {
183 str = access.getProperty(Config.AAF_ROOT_NS, null);
185 String temp = source.replace("%AAF_NS", str);
186 if(temp == source) { // intended
187 source = source.replace("AAF_NS", str); // Backward Compatibility
194 int atC = source.indexOf("%C");
196 // aaf_locator_container_ns
197 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
199 source = source.replace("%CNS"+'.', str);
201 source = source.replace("%CNS", str);
203 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le, "");
205 source = source.replace("%C"+'.', str);
207 source = source.replace("%C", str);
210 if(source.indexOf('%')>=0) {
211 // These shouldn't be expected to have dot elements
212 source = source.replace("%N", name);
213 source = source.replace("%DF", default_fqdn);
214 source = source.replace("%PH", public_hostname);
219 public int getEntryPort(final String dot_le) {
220 return public_port!=null && dot_le.isEmpty()?