2a8760f40b1e94c56d0817f46c69043edb3e8185
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / config / RegistrationPropHolder.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  */
20
21 package org.onap.aaf.cadi.config;
22
23 import java.net.Inet4Address;
24 import java.net.UnknownHostException;
25
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;
30
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;
36         private int port;
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;
48
49         public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
50                 this.access = access;
51                 StringBuilder errs = new StringBuilder();
52                 String str;
53                 this.port = port;
54
55                 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
56                 default_container = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
57                 if(firstlog) {
58                         access.printf(Level.INIT, REGI,"default_container",default_container);
59                 }
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);
63                         if(str==null) {
64                                 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
65                         }
66                 } else {
67                         lcontainer=default_container;
68                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
69                 }
70                 if(str!=null) {
71                         public_port=Integer.decode(str);
72                 }
73                 if(firstlog) {
74                         access.printf(Level.INIT, "RegistrationProperty: public_port='%d'",public_port);
75                 }
76
77                 hostname = access.getProperty(Config.HOSTNAME, null);
78                 if (hostname==null) {
79                         hostname = Inet4Address.getLocalHost().getHostName();
80                 }
81                 if (hostname==null) {
82                         mustBeDefined(errs,Config.HOSTNAME);
83                 }
84                 if(firstlog) {
85                         access.printf(Level.INIT, REGI,"hostname",hostname);
86                 }
87
88                 public_fqdn = access.getProperty(Config.AAF_LOCATOR_PUBLIC_FQDN, hostname);
89                 if(firstlog) {
90                         access.printf(Level.INIT, REGI,"public_fqdn",public_fqdn);
91                 }
92
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);
99                         }
100                 }
101                 default_name = container_public_name;
102                 
103                 if(firstlog) {
104                         access.printf(Level.INIT, REGI,"default_name",default_name);
105                 }
106                 
107                 latitude=null;
108                 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
109                 if(slatitude == null) {
110                         mustBeDefined(errs,Config.CADI_LATITUDE);
111                 } else {
112                         latitude = Float.parseFloat(slatitude);
113                 }
114                 if(firstlog) {
115                         access.printf(Level.INIT, REGI,"latitude",slatitude);
116                 }
117
118                 longitude=null;
119                 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
120                 if(slongitude == null) {
121                         mustBeDefined(errs,Config.CADI_LONGITUDE);
122                 } else {
123                         longitude = Float.parseFloat(slongitude);
124                 }
125                 if(firstlog) {
126                         access.printf(Level.INIT, REGI,"longitude",slongitude);
127                 }
128
129                 String dot_le;
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()) {
135                                 public_fqdn=str;
136                                 if(firstlog) {
137                                         access.printf(Level.INIT, "RegistrationProperty: public_hostname(overloaded by %s)='%s'",dot_le,public_fqdn);
138                                 }
139                         }
140                 }
141                 
142                 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
143                 if(firstlog) {
144                         access.printf(Level.INIT, REGI,"default_fqdn",default_fqdn);
145                 }
146                 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
147                 if(firstlog) {
148                         access.printf(Level.INIT, REGI,"default_container_ns",default_container_ns);
149                 }
150                 if(errs.length()>0) {
151                         throw new CadiException(errs.toString());
152                 }
153                 firstlog = false;
154         }
155
156         private void mustBeDefined(StringBuilder errs, String propname) {
157                 errs.append('\n');
158                 errs.append(propname);
159                 errs.append(" must be defined.");
160                 
161         }
162
163         public String getEntryFQDN(final String entry, final String dot_le) {
164                 String str;
165                 if(public_fqdn!=null && dot_le.isEmpty()) {
166                         str = public_fqdn;
167                 } else {
168                         str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
169                 }
170                 return replacements("RegistrationPropHolder.getEntryFQDN",str,entry,dot_le);
171         }
172         
173         public String getEntryName(final String entry, final String dot_le) {
174                 String str;
175                 if(dot_le.isEmpty()) {
176                         str = default_name;
177                 } else {
178                         str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
179                 }
180                 return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
181         }
182         
183         public String getPublicEntryName(final String entry, final String dot_le) {
184                 String str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME+dot_le, null);
185                 if(str==null) {
186                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME,null);
187                 }
188                 if(str==null) {
189                         str = default_name;
190                 }
191                 return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
192         }
193         
194         
195         private String getNS(String dot_le) {
196                 String ns;
197                 ns = access.getProperty(Config.AAF_LOCATOR_APP_NS+dot_le,null);
198                 if(ns==null) {
199                         ns = access.getProperty(Config.AAF_LOCATOR_APP_NS, "AAF_NS");
200                 }
201                 return ns;
202         }
203
204         
205         public String replacements(final String fromCode, final String source, final String name, final String _dot_le) {
206                 if(source == null) {
207                         return "";
208                 } else if(source.isEmpty()) {
209                         return source;
210                 }
211                 String value = source;
212                 String dot_le;
213                 if(_dot_le==null) {
214                         dot_le = default_container.isEmpty()?"":'.'+default_container;
215                 } else {
216                         dot_le = _dot_le;
217                 }
218
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);
222         }
223
224         String str;
225         if(aaf_locator_host!=null) {
226                         if("https://AAF_LOCATE_URL".equals(value)) {
227                                 value = aaf_locator_host;
228                         } else {
229                         str = aaf_locator_host;
230                                 if(value.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
231                                         if(!str.endsWith("/")) {
232                                                 str+='/';
233                                         }
234                                         if(!str.endsWith("/locate/")) {
235                                                 str+="locate/";
236                                         }
237                                         if(value.startsWith("http:")) {
238                                                 value = value.replace("http://AAF_LOCATE_URL/", str);
239                                         } else {
240                                                 value = value.replace("https://AAF_LOCATE_URL/", str);
241                                                 
242                                         }
243                                 }
244                         }
245         }
246
247                 int atC = value.indexOf("%C"); 
248                 if(atC>=0) {
249                         // aaf_locator_container_ns
250                         str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
251                         if(str.isEmpty()) {
252                                 value = value.replace("%CNS"+'.', str);
253                         }
254                         value = value.replace("%CNS", str);
255                         
256                         str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le,default_container);
257                         if(str.isEmpty()) {
258                                 value = value.replace("%C"+'.', str);
259                         }
260                         value = value.replace("%C", str);
261                 }
262                 
263                 if(value.indexOf("%NS")>=0) {
264                         str = getNS(dot_le);
265                         if(str==null || str.isEmpty()) {
266                                 value = value.replace("%NS"+'.', "");
267                         } else {
268                                 value = value.replace("%NS", str);
269                         }
270                 }
271
272                 // aaf_root_ns
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
278                         } else {
279                                 value = temp;
280                         }
281                 }
282
283                 
284                 if(value.indexOf('%')>=0) {
285             // These shouldn't be expected to have dot elements
286             if(name!=null) {
287               value = value.replace("%N", name);
288             }
289             if(default_fqdn!=null) {
290               value = value.replace("%DF", default_fqdn);
291             }
292             if(public_fqdn!=null) {
293               value = value.replace("%PH", public_fqdn);
294             }
295                 }
296                 access.printf(Level.DEBUG, 
297                                 "RegistrationReplacement from %s, source: %s, dot_le: %s, value: %s",
298                                 fromCode,source,dot_le,value);
299
300                 return value;
301         }
302         
303         public int getEntryPort(final String dot_le) {
304                 return public_port!=null && dot_le.isEmpty()?
305                                 public_port:
306                                 port;
307         }
308
309         public Access access() {
310                 return access;
311         }
312 }