Public and Private Locate entries
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / register / RegistrationCreator.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.register;
22
23 import java.net.Inet4Address;
24 import java.net.UnknownHostException;
25 import java.util.List;
26
27 import org.onap.aaf.cadi.Access;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.aaf.Defaults;
30 import org.onap.aaf.cadi.config.Config;
31 import org.onap.aaf.cadi.util.Split;
32
33 import locate.v1_0.MgmtEndpoint;
34 import locate.v1_0.MgmtEndpoints;
35
36 public class RegistrationCreator {
37     private static final String MUST_BE_DEFINED = " must be defined\n";
38         private Access access;
39     
40     public RegistrationCreator(Access access) {
41         this.access = access;
42     }
43     
44     public MgmtEndpoints create(final int port) throws CadiException {
45         MgmtEndpoints me = new MgmtEndpoints();
46         List<MgmtEndpoint> lme = me.getMgmtEndpoint();
47         MgmtEndpoint defData = null;
48         MgmtEndpoint locate = null;
49
50
51         StringBuilder errs = new StringBuilder();
52         try {
53                 String hostname = access.getProperty(Config.HOSTNAME, null);
54                 if (hostname==null) {
55                         hostname = Inet4Address.getLocalHost().getHostName();
56                 }
57                 if (hostname==null) {
58                         errs.append(Config.HOSTNAME);
59                         errs.append(MUST_BE_DEFINED);
60                 }
61
62                 Float latitude=null;
63                 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
64                 if(slatitude == null) {
65                         errs.append(Config.CADI_LATITUDE);
66                         errs.append(MUST_BE_DEFINED);
67                 } else {
68                         latitude = Float.parseFloat(slatitude);
69                 }
70
71                 Float longitude=null;
72                 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
73                 if(slongitude == null) {
74                         errs.append(Config.CADI_LONGITUDE);
75                         errs.append(MUST_BE_DEFINED);
76                 } else {
77                         longitude = Float.parseFloat(slongitude);
78                 }
79
80                 if(errs.length()>0) {
81                         throw new CadiException(errs.toString());
82                 }
83
84                 String dot_le;
85                 String ns;
86                 String version=null;
87                 String lentries = access.getProperty(Config.AAF_LOCATOR_CONTAINER, null);
88                 if(lentries==null) {
89                         lentries="";
90                 } else {
91                         lentries=',' + lentries; // "" makes a blank default Public Entry
92                 }
93
94                 String defaultName = null;
95                 String str;
96                 int public_port = port;
97                 // Note: only one of the ports can be public...  Therefore, only the la
98                 for(String le : Split.splitTrim(',', lentries)) {
99                         dot_le = le.isEmpty()?"":"."+le;
100                                 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+dot_le, null);
101                                 if(str!=null) { // Get Public Port
102                                         public_port = Integer.decode(str);
103                                 }
104                 }
105                 
106                 String public_hostname = hostname;
107                 for(String le : Split.splitTrim(',', lentries)) {
108                         dot_le = le.isEmpty()?"":"."+le;
109                                 String ph = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null);
110                                 if( ph != null) {
111                                         public_hostname=ph;
112                                 }
113                 }
114                 
115                 String default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, public_hostname);
116                 
117
118                 // Now, loop through by Container
119                 for(String le : Split.splitTrim(',', lentries)) {
120                         // Add variable entries
121                         String names;
122                         if(le.length()>0) {
123                                 dot_le = '.' + le;
124                                 names = access.getProperty(Config.AAF_LOCATOR_NAMES+dot_le,null);
125                                 if(names==null) {
126                                         // Go for Default
127                                         names = access.getProperty(Config.AAF_LOCATOR_NAMES,"");
128                                 }
129                         } else {
130                                 dot_le = "";
131                                 names=access.getProperty(Config.AAF_LOCATOR_NAMES,dot_le);
132                         }
133                         
134                         for(String name : Split.splitTrim(',', names)) {
135                                 if(defData==null) {
136                                         defData = locate = new MgmtEndpoint();
137
138                                         defaultName = name;
139                                         version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION);
140                                         locate.setProtocol(access.getProperty(Config.AAF_LOCATOR_PROTOCOL,null));
141                                         List<String> ls = locate.getSubprotocol();
142                                         for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) {
143                                                 ls.add(sp);     
144                                         }
145                                         locate.setLatitude(latitude);
146                                         locate.setLongitude(longitude);
147
148                                 } else {
149                                         locate = copy(defData);
150                                 }
151                                 
152                                 str = access.getProperty(Config.HOSTNAME+dot_le, null);
153                                 if(str==null) {
154                                         str = access.getProperty(Config.HOSTNAME, hostname);
155                                 }
156                                 locate.setHostname(hostname);
157                                 
158                                 ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null);
159                                 if(ns==null) {
160                                         ns = access.getProperty(Config.AAF_LOCATOR_NS,"");
161                                 }
162                                 switch(ns) {
163                                         case Defaults.AAF_NS:
164                                                 ns = access.getProperty(Config.AAF_ROOT_NS, "");
165                                                 // Fallthrough on purpose.
166                                 }
167
168                                 String ns_dot;
169                                 if(ns.isEmpty()) {
170                                         ns_dot = ns;
171                                 } else {
172                                         ns_dot = ns + '.';
173                                 }
174
175                                 String container_id = access.getProperty(Config.AAF_LOCATOR_CONTAINER_ID+dot_le, "");
176                                 if(!container_id.isEmpty()) {
177                                         ns_dot = container_id + '.' + ns_dot;
178                                 }
179
180                                 if(!le.isEmpty()) {
181                                         ns_dot = le + '.' + ns_dot;
182                                 }
183
184                                 if(name.isEmpty()) {
185                                                 locate.setName(ns_dot + defaultName);
186                                 } else {
187                                         locate.setName(ns_dot + name);
188                                 }
189
190                                 if(dot_le.isEmpty()) {
191                                         locate.setHostname(access.getProperty(Config.AAF_LOCATOR_FQDN, default_fqdn));
192                                 } else {
193                                         str =  access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, null);
194                                         if(str==null) {
195                                                 locate.setHostname(default_fqdn);
196                                         } else {
197                                                 String container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, "");
198                                                 str = str.replace("%CNS", container_ns);
199                                                 String container = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le, "");
200                                                 str = str.replace("%C", container);
201                                                 str = str.replace("%NS", ns);
202                                                 str = str.replace("%N", name);
203                                                 str = str.replace("%DF", default_fqdn);
204                                                 str = str.replace("%PH", public_hostname);
205                                                 locate.setHostname(str);
206                                         }
207                                 }
208                                 
209                                 if(le.isEmpty()) {
210                                         locate.setPort(public_port);
211                                 } else {
212                                         locate.setPort(port);
213                                 }
214
215                                 String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
216                                 if(specificVersion == null && locate == defData) {
217                                         specificVersion = version;
218                                 }
219                                 if(specificVersion!=null) {
220                                         String split[] = Split.splitTrim('.', specificVersion);
221                                         locate.setPkg(split.length>3?Integer.parseInt(split[3]):0);
222                                         locate.setPatch(split.length>2?Integer.parseInt(split[2]):0);
223                                         locate.setMinor(split.length>1?Integer.parseInt(split[1]):0);
224                                         locate.setMajor(split.length>0?Integer.parseInt(split[0]):0);
225                                 }
226
227                                 String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, null);
228                                 if (protocol!=null) {
229                                         locate.setProtocol(protocol);
230                                         String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
231                                         if(subprotocols!=null) {
232                                                 List<String> ls = locate.getSubprotocol();
233                                                 for (String s : Split.split(',', subprotocols)) {
234                                                         ls.add(s);
235                                                 }
236                                         }
237                                 }
238                                 lme.add(locate);
239                         }
240                 }
241         } catch (NumberFormatException | UnknownHostException e) {
242                 throw new CadiException("Error extracting Data from Properties for Registrar",e);
243         }
244
245         return me;
246     }
247         
248     private MgmtEndpoint copy(MgmtEndpoint mep) {
249                 MgmtEndpoint out = new MgmtEndpoint();
250                 out.setName(mep.getName());
251                 out.setHostname(mep.getHostname());
252                 out.setLatitude(mep.getLatitude());
253                 out.setLongitude(mep.getLongitude());
254                 out.setMajor(mep.getMajor());
255                 out.setMinor(mep.getMinor());
256                 out.setPkg(mep.getPkg());
257                 out.setPatch(mep.getPatch());
258                 out.setPort(mep.getPort());
259                 out.setProtocol(mep.getProtocol());
260                 out.getSpecialPorts().addAll(mep.getSpecialPorts());
261                 out.getSubprotocol().addAll(mep.getSubprotocol());
262                 return out;
263         }
264 }