Sonar Fixes, Formatting
[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.UnknownHostException;
24 import java.util.List;
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.aaf.Defaults;
30 import org.onap.aaf.cadi.config.Config;
31 import org.onap.aaf.cadi.config.RegistrationPropHolder;
32 import org.onap.aaf.cadi.util.Split;
33
34 import locate.v1_0.MgmtEndpoint;
35 import locate.v1_0.MgmtEndpoint.SpecialPorts;
36 import locate.v1_0.MgmtEndpoints;
37
38 public class RegistrationCreator {
39     private Access access;
40
41     public RegistrationCreator(Access access) {
42         this.access = access;
43     }
44
45     public MgmtEndpoints create(final int port) throws CadiException {
46         MgmtEndpoints me = new MgmtEndpoints();
47         List<MgmtEndpoint> lme = me.getMgmtEndpoint();
48         MgmtEndpoint defData = null;
49         MgmtEndpoint locate = null;
50
51         try {
52             String dot_le;
53             String version=null;
54             String defProtocol="https";
55
56             RegistrationPropHolder ph = new RegistrationPropHolder(access, port);
57
58             String firstPrivateHostname = null;
59             // Now, loop through by Container
60             for(String le : Split.splitTrim(',', ph.lcontainer)) {
61                 if(le.isEmpty()) {
62                     dot_le = le;
63                 } else {
64                     dot_le = "."+le;
65                 }
66
67                 for(String entry : Split.splitTrim(',', ph.lentries)) {
68                     if(defData==null) {
69                         defData = locate = new MgmtEndpoint();
70
71                         version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION);
72                         locate.setProtocol(defProtocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL,defProtocol));
73                         List<String> ls = locate.getSubprotocol();
74                         for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) {
75                             ls.add(sp);
76                         }
77                         locate.setLatitude(ph.latitude);
78                         locate.setLongitude(ph.longitude);
79
80                     } else {
81                         locate = copy(defData);
82                     }
83
84                     locate.setName(ph.getEntryName(entry,dot_le));
85                     /* Cover the situation where there is a Container, and multiple locator Entries,
86                      * the first of which is the only real private FQDN
87                      * example: oauth
88                      *      aaf_locator_entries=oauth,token,introspect
89                      *
90                      *      Entries for token and introspect, but they point to oauth service.
91                      */
92                     String locateHostname;
93                     if(le.isEmpty()) {
94                         locateHostname=ph.getEntryFQDN(entry, dot_le);
95                     } else if(firstPrivateHostname==null) {
96                         firstPrivateHostname=locateHostname=ph.getEntryFQDN(entry, dot_le);
97                     } else {
98                         locateHostname=firstPrivateHostname;
99                     }
100
101                     locate.setHostname(locateHostname);
102                     locate.setPort(ph.getEntryPort(dot_le));
103
104                     String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
105                     if(specificVersion == null && locate == defData) {
106                         specificVersion = version;
107                     }
108                     if(specificVersion!=null) {
109                         String split[] = Split.splitTrim('.', specificVersion);
110                         String deply[]= Split.splitTrim('.', access.getProperty(Config.AAF_DEPLOYED_VERSION, ""));
111                         locate.setMajor(best(split,deply,0));
112                         locate.setMinor(best(split,deply,1));
113                         locate.setPatch(best(split,deply,2));
114                         locate.setPkg(best(split,deply,3));
115                     }
116
117                     String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, defProtocol);
118                     if (protocol!=null) {
119                         locate.setProtocol(protocol);
120                         List<String> ls = locate.getSubprotocol();
121                         // ls cannot be null, per generated getSubprotocol code
122                         if(ls.isEmpty()) {
123                             String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
124                             if(subprotocols==null) {
125                                 subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
126                             }
127                             if(subprotocols!=null) {
128                                 for (String s : Split.split(',', subprotocols)) {
129                                     ls.add(s);
130                                 }
131                             } else {
132                                 access.printf(Level.ERROR, "%s is required for Locator Registration of %s",
133                                         Config.AAF_LOCATOR_SUBPROTOCOL,Config.AAF_LOCATOR_PROTOCOL);
134                             }
135                         }
136                         lme.add(locate);
137                     } else {
138                         access.printf(Level.ERROR, "%s is required for Locator Registration",Config.AAF_LOCATOR_PROTOCOL);
139                     }
140                 }
141             }
142         } catch (NumberFormatException | UnknownHostException e) {
143             throw new CadiException("Error extracting Data from Properties for Registrar",e);
144         }
145
146         if(access.willLog(Level.INFO)) {
147             access.log(Level.INFO, print(new StringBuilder(),me.getMgmtEndpoint()));
148         }
149         return me;
150     }
151
152     /*
153      * Find the best version between Actual Interface and Deployed version
154      */
155     private int best(String[] split, String[] deploy, int i) {
156         StringBuilder sb = new StringBuilder();
157         char c;
158         String s;
159         if(split.length>i) {
160             s=split[i];
161             for(int j=0;j<s.length();++j) {
162                 if(Character.isDigit(c=s.charAt(j))) {
163                     sb.append(c);
164                 } else {
165                     break;
166                 }
167             }
168         }
169
170         if(sb.length()==0 && deploy.length>i) {
171             s=deploy[i];
172             for(int j=0;j<s.length();++j) {
173                 if(Character.isDigit(c=s.charAt(j))) {
174                     sb.append(c);
175                 } else {
176                     break;
177                 }
178             }
179         }
180
181         return sb.length()==0?0:Integer.parseInt(sb.toString());
182     }
183
184     private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
185         int cnt = 0;
186         for(MgmtEndpoint m : lme) {
187             print(sb,cnt++,m);
188         }
189         return sb;
190     }
191
192     private void print(StringBuilder out, int cnt, MgmtEndpoint mep) {
193         out.append("\nManagement Endpoint - ");
194         out.append(cnt);
195         out.append("\n\tName:       ");
196         out.append(mep.getName());
197         out.append("\n\tHostname:   ");
198         out.append(mep.getHostname());
199         out.append("\n\tLatitude:   ");
200         out.append(mep.getLatitude());
201         out.append("\n\tLongitude:  ");
202         out.append(mep.getLongitude());
203         out.append("\n\tVersion:    ");
204         out.append(mep.getMajor());
205         out.append('.');
206         out.append(mep.getMinor());
207         out.append('.');
208         out.append(mep.getPatch());
209         out.append('.');
210         out.append(mep.getPkg());
211         out.append("\n\tPort:       ");
212         out.append(mep.getPort());
213         out.append("\n\tProtocol:   ");
214         out.append(mep.getProtocol());
215         out.append("\n\tSpecial Ports:");
216         for( SpecialPorts sp : mep.getSpecialPorts()) {
217             out.append("\n\t\tName:       ");
218             out.append(sp.getName());
219             out.append("\n\t\tPort:       ");
220             out.append(sp.getPort());
221             out.append("\n\t\tProtocol:   ");
222             out.append(sp.getProtocol());
223             out.append("\n\t\t  Versions: ");
224             boolean first = true;
225             for(String s : sp.getProtocolVersions()) {
226                 if(first) {
227                     first = false;
228                 } else {
229                     out.append(',');
230                 }
231                 out.append(s);
232             }
233         }
234         boolean first = true;
235         out.append("\n\tSubProtocol: ");
236         for(String s : mep.getSubprotocol()) {
237             if(first) {
238                 first = false;
239             } else {
240                 out.append(',');
241             }
242             out.append(s);
243         }
244     }
245
246     private MgmtEndpoint copy(MgmtEndpoint mep) {
247         MgmtEndpoint out = new MgmtEndpoint();
248         out.setName(mep.getName());
249         out.setHostname(mep.getHostname());
250         out.setLatitude(mep.getLatitude());
251         out.setLongitude(mep.getLongitude());
252         out.setMajor(mep.getMajor());
253         out.setMinor(mep.getMinor());
254         out.setPatch(mep.getPatch());
255         out.setPkg(mep.getPkg());
256         out.setPort(mep.getPort());
257         out.setProtocol(mep.getProtocol());
258         out.getSpecialPorts().addAll(mep.getSpecialPorts());
259         out.getSubprotocol().addAll(mep.getSubprotocol());
260         return out;
261     }
262 }