Implement public private locator
[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
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 lentries;
42         public final String lcontainer;
43
44         public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
45                 this.access = access;
46                 StringBuilder errs = new StringBuilder();
47                 String str;
48                 this.port = port;
49
50                 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
51                 
52                 str = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
53                 if(!str.isEmpty()) {
54                         lcontainer=',' + str; // "" makes a blank default Public Entry
55                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+str, null);
56                         if(str==null) {
57                                 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
58                         }
59                 } else {
60                         lcontainer=str;
61                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
62                 }
63                 if(str!=null) {
64                         public_port=Integer.decode(str);
65                 }
66                 
67                 hostname = access.getProperty(Config.HOSTNAME, null);
68                 if (hostname==null) {
69                         hostname = Inet4Address.getLocalHost().getHostName();
70                 }
71                 if (hostname==null) {
72                         mustBeDefined(errs,Config.HOSTNAME);
73                 }
74
75                 public_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname);
76                                 
77                 latitude=null;
78                 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
79                 if(slatitude == null) {
80                         mustBeDefined(errs,Config.CADI_LATITUDE);
81                 } else {
82                         latitude = Float.parseFloat(slatitude);
83                 }
84
85                 longitude=null;
86                 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
87                 if(slongitude == null) {
88                         mustBeDefined(errs,Config.CADI_LONGITUDE);
89                 } else {
90                         longitude = Float.parseFloat(slongitude);
91                 }
92
93                 String dot_le;
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);
98                         if( str != null) {
99                                 public_hostname=str;
100                         }
101                 }
102                 
103                 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, public_hostname);
104                 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
105                 
106                 if(errs.length()>0) {
107                         throw new CadiException(errs.toString());
108                 }
109         }
110
111         private void mustBeDefined(StringBuilder errs, String propname) {
112                 errs.append('\n');
113                 errs.append(propname);
114                 errs.append(" must be defined.");
115                 
116         }
117
118         public String getEntryFQDN(final String entry, final String dot_le) {
119                 String str;
120                 if(public_hostname!=null && dot_le.isEmpty()) {
121                         str = public_hostname;
122                 } else {
123                         str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, null);
124                         if(str==null) {
125                                 str = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
126                         }
127                 }
128                 return replacements(str,entry,dot_le);
129         }
130         
131         public String getEntryName(final String entry, final String dot_le) {
132                 String str;
133                 str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, "%NS.%N");
134                 return replacements(str,entry,dot_le);
135         }
136         
137         
138         private String getNS(String dot_le) {
139                 String ns;
140                 ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null);
141                 if(ns==null) {
142                         ns = access.getProperty(Config.AAF_ROOT_NS, "");
143                 }
144                 return ns;
145         }
146
147         
148         public String replacements(String source, final String name, final String dot_le) {
149                 if(source == null) {
150                         return "";
151                 } else if(source.isEmpty()) {
152                         return source;
153                 }
154                 String str;
155                 // aaf_locate_url
156                 if(source.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
157                         str = access.getProperty(Config.AAF_LOCATE_URL, null);
158                         if(str!=null) {
159                                 if(!str.endsWith("/")) {
160                                         str+='/';
161                                 }
162                                 if(!str.endsWith("/locate/")) {
163                                         str+="locate/";
164                                 }
165                                 source = source.replace("https://AAF_LOCATE_URL/", str);
166                         }
167                 }
168
169                 if(source.indexOf("%NS")>=0) {
170                         str = getNS(dot_le);
171                         if(str==null || str.isEmpty()) {
172                                 source = source.replace("%NS"+'.', str);
173                         }
174                         source = source.replace("%NS", str);
175                 }
176
177                 // aaf_root_ns
178                 if(source.indexOf("AAF_NS")>=0) {
179                         str = access.getProperty(Config.AAF_ROOT_NS, null);
180                         if(str!=null) {
181                                 String temp = source.replace("%AAF_NS", str);
182                                 if(temp == source) { // intended
183                                         source = source.replace("AAF_NS", str); // Backward Compatibility
184                                 } else {
185                                         source = temp;
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, "");
200                         if(str.isEmpty()) {
201                                 source = source.replace("%C"+'.', str);
202                         }
203                         source = source.replace("%C", str);
204                 }
205                 
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);
211                 }
212                 return source;
213         }
214         
215         public int getEntryPort(final String dot_le) {
216                 return public_port!=null && dot_le.isEmpty()?
217                                 public_port:
218                                 port;
219         }
220 }