fix new openssl, data, etc
[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.CadiException;
28 import org.onap.aaf.cadi.util.Split;
29
30 public class RegistrationPropHolder {
31         private final String PUBLIC_NAME="%NS.%N";
32         private final Access access;
33         public String hostname;
34         private int port;
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;
45
46         public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
47                 this.access = access;
48                 StringBuilder errs = new StringBuilder();
49                 String str;
50                 this.port = port;
51
52                 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
53                 
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);
58                         if(str==null) {
59                                 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
60                         }
61                 } else {
62                         lcontainer=default_container;
63                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
64                 }
65                 if(str!=null) {
66                         public_port=Integer.decode(str);
67                 }
68                 
69                 hostname = access.getProperty(Config.HOSTNAME, null);
70                 if (hostname==null) {
71                         hostname = Inet4Address.getLocalHost().getHostName();
72                 }
73                 if (hostname==null) {
74                         mustBeDefined(errs,Config.HOSTNAME);
75                 }
76
77                 public_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname);
78                                 
79                 default_name = access.getProperty(Config.AAF_LOCATOR_NAME, "%CNS.%NS.%N");
80                 
81                 latitude=null;
82                 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
83                 if(slatitude == null) {
84                         mustBeDefined(errs,Config.CADI_LATITUDE);
85                 } else {
86                         latitude = Float.parseFloat(slatitude);
87                 }
88
89                 longitude=null;
90                 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
91                 if(slongitude == null) {
92                         mustBeDefined(errs,Config.CADI_LONGITUDE);
93                 } else {
94                         longitude = Float.parseFloat(slongitude);
95                 }
96
97                 String dot_le;
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);
102                         if( str != null) {
103                                 public_hostname=str;
104                         }
105                 }
106                 
107                 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
108                 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
109                 
110                 if(errs.length()>0) {
111                         throw new CadiException(errs.toString());
112                 }
113         }
114
115         private void mustBeDefined(StringBuilder errs, String propname) {
116                 errs.append('\n');
117                 errs.append(propname);
118                 errs.append(" must be defined.");
119                 
120         }
121
122         public String getEntryFQDN(final String entry, final String dot_le) {
123                 String str;
124                 if(public_hostname!=null && dot_le.isEmpty()) {
125                         str = public_hostname;
126                 } else {
127                         str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
128                 }
129                 return replacements(str,entry,dot_le);
130         }
131         
132         public String getEntryName(final String entry, final String dot_le) {
133                 String str;
134                 if(dot_le.isEmpty()) {
135                         str = PUBLIC_NAME;
136                 } else {
137                         str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
138                 }
139                 return replacements(str,entry,dot_le);
140         }
141         
142         
143         private String getNS(String dot_le) {
144                 String ns;
145                 ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null);
146                 if(ns==null) {
147                         ns = access.getProperty(Config.AAF_ROOT_NS, "");
148                 }
149                 return ns;
150         }
151
152         
153         public String replacements(String source, final String name, final String _dot_le) {
154                 if(source == null) {
155                         return "";
156                 } else if(source.isEmpty()) {
157                         return source;
158                 }
159                 
160                 String dot_le;
161                 if(_dot_le==null) {
162                         dot_le = default_container.isEmpty()?"":'.'+default_container;
163                 } else {
164                         dot_le = _dot_le;
165                 }
166
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);
170         }
171
172         String str;
173         if(aaf_locator_host!=null) {
174                         if("https://AAF_LOCATE_URL".equals(source)) {
175                                 source = aaf_locator_host;
176                         } else {
177                         str = aaf_locator_host;
178                                 if(source.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
179                                         if(!str.endsWith("/")) {
180                                                 str+='/';
181                                         }
182                                         if(!str.endsWith("/locate/")) {
183                                                 str+="locate/";
184                                         }
185                                         source = source.replace("https://AAF_LOCATE_URL/", str);
186                                 }
187                         }
188         }
189
190                 int atC = source.indexOf("%C"); 
191                 if(atC>=0) {
192                         // aaf_locator_container_ns
193                         str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
194                         if(str.isEmpty()) {
195                                 source = source.replace("%CNS"+'.', str);
196                         }
197                         source = source.replace("%CNS", str);
198                         
199                         str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le,default_container);
200                         if(str.isEmpty()) {
201                                 source = source.replace("%C"+'.', str);
202                         }
203                         source = source.replace("%C", str);
204                 }
205                 
206                 if(source.indexOf("%NS")>=0) {
207                         str = getNS(dot_le);
208                         if(str==null || str.isEmpty()) {
209                                 source = source.replace("%NS"+'.', str);
210                         }
211                         source = source.replace("%NS", str);
212                 }
213
214                 // aaf_root_ns
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
220                         } else {
221                                 source = temp;
222                         }
223                 }
224
225                 
226                 if(source.indexOf('%')>=0) {
227             // These shouldn't be expected to have dot elements
228             if(name!=null) {
229               source = source.replace("%N", name);
230             }
231             if(default_fqdn!=null) {
232               source = source.replace("%DF", default_fqdn);
233             }
234             if(public_hostname!=null) {
235               source = source.replace("%PH", public_hostname);
236             }
237                 }
238                 return source;
239         }
240         
241         public int getEntryPort(final String dot_le) {
242                 return public_port!=null && dot_le.isEmpty()?
243                                 public_port:
244                                 port;
245         }
246
247         public Access access() {
248                 return access;
249         }
250 }