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