Fixes/Refinements from Testing
[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                 // Now, loop through by Container
59                 for(String le : Split.splitTrim(',', ph.lcontainer)) {
60                         if(le.isEmpty()) {
61                                 dot_le = le;
62                         } else {
63                                 dot_le = "."+le;
64                         }
65
66                         for(String entry : Split.splitTrim(',', ph.lentries)) {
67                                 if(defData==null) {
68                                         defData = locate = new MgmtEndpoint();
69
70                                         version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION);
71                                         locate.setProtocol(defProtocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL,defProtocol));
72                                         List<String> ls = locate.getSubprotocol();
73                                         for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) {
74                                                 ls.add(sp);     
75                                         }
76                                         locate.setLatitude(ph.latitude);
77                                         locate.setLongitude(ph.longitude);
78
79                                 } else {
80                                         locate = copy(defData);
81                                 }
82                                 
83                                 locate.setName(ph.getEntryName(entry,dot_le));
84                                 locate.setHostname(ph.getEntryFQDN(entry,dot_le));
85                                 locate.setPort(ph.getEntryPort(dot_le));
86                                 
87                                 String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
88                                 if(specificVersion == null && locate == defData) {
89                                         specificVersion = version;
90                                 }
91                                 if(specificVersion!=null) {
92                                         String split[] = Split.splitTrim('.', specificVersion);
93                                         String deply[]= Split.splitTrim('.', access.getProperty(Config.AAF_DEPLOYED_VERSION, ""));
94                                         locate.setMajor(best(split,deply,0));
95                                         locate.setMinor(best(split,deply,1));
96                                         locate.setPatch(best(split,deply,2));
97                                         locate.setPkg(best(split,deply,3));
98                                 }
99
100                                 String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, defProtocol);
101                                 if (protocol!=null) {
102                                         locate.setProtocol(protocol);
103                                                 List<String> ls = locate.getSubprotocol();
104                                                 // ls cannot be null, per generated getSubprotocol code
105                                                 if(ls.isEmpty()) {
106                                                 String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
107                                                 if(subprotocols==null) {
108                                                         subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
109                                                 }
110                                                 if(subprotocols!=null) {
111                                                         for (String s : Split.split(',', subprotocols)) {
112                                                                 ls.add(s);
113                                                         }
114                                                 } else {
115                                                         access.printf(Level.ERROR, "%s is required for Locator Registration of %s",
116                                                                         Config.AAF_LOCATOR_SUBPROTOCOL,Config.AAF_LOCATOR_PROTOCOL);
117                                                 }
118                                                 }
119                                         lme.add(locate);
120                                 } else {
121                                         access.printf(Level.ERROR, "%s is required for Locator Registration",Config.AAF_LOCATOR_PROTOCOL);
122                                 }
123                         }
124                 }
125         } catch (NumberFormatException | UnknownHostException e) {
126                 throw new CadiException("Error extracting Data from Properties for Registrar",e);
127         }
128         
129         if(access.willLog(Level.INFO)) {
130                 access.log(Level.INFO, print(new StringBuilder(),me.getMgmtEndpoint()));
131         }
132         return me;
133     }
134         
135     /*
136      * Find the best version between Actual Interface and Deployed version
137      */
138     private int best(String[] split, String[] deploy, int i) {
139         StringBuilder sb = new StringBuilder();
140         char c;
141                 String s;
142         if(split.length>i) {
143                 s=split[i];
144                         for(int j=0;j<s.length();++j) {
145                                 if(Character.isDigit(c=s.charAt(j))) {
146                                         sb.append(c);
147                                 } else {
148                                         break;
149                                 }
150                         }
151         }       
152                 
153                 if(sb.length()==0 && deploy.length>i) {
154                         s=deploy[i];
155                         for(int j=0;j<s.length();++j) {
156                                 if(Character.isDigit(c=s.charAt(j))) {
157                                         sb.append(c);
158                                 } else {
159                                         break;
160                                 }
161                         }
162                 }
163                 
164                 return sb.length()==0?0:Integer.parseInt(sb.toString());
165     }
166
167         private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
168         int cnt = 0;
169                 for(MgmtEndpoint m : lme) {
170                         print(sb,cnt++,m);
171                 }
172                 return sb;
173         }
174
175         private void print(StringBuilder out, int cnt, MgmtEndpoint mep) {
176                 out.append("\nManagement Endpoint - ");
177                 out.append(cnt);
178                 out.append("\n\tName:       ");
179                 out.append(mep.getName());
180                 out.append("\n\tHostname:   ");
181                 out.append(mep.getHostname());
182                 out.append("\n\tLatitude:   ");
183                 out.append(mep.getLatitude());
184                 out.append("\n\tLongitude:  ");
185                 out.append(mep.getLongitude());
186                 out.append("\n\tVersion:    ");
187                 out.append(mep.getMajor());
188                 out.append('.');
189                 out.append(mep.getMinor());
190                 out.append('.');
191                 out.append(mep.getPatch());
192                 out.append('.');
193                 out.append(mep.getPkg());
194                 out.append("\n\tPort:       ");
195                 out.append(mep.getPort());
196                 out.append("\n\tProtocol:   ");
197                 out.append(mep.getProtocol());
198                 out.append("\n\tSpecial Ports:");
199                 for( SpecialPorts sp : mep.getSpecialPorts()) {
200                         out.append("\n\t\tName:       ");
201                         out.append(sp.getName());
202                         out.append("\n\t\tPort:       ");
203                         out.append(sp.getPort());
204                         out.append("\n\t\tProtocol:   ");
205                         out.append(sp.getProtocol());
206                         out.append("\n\t\t  Versions: ");
207                         boolean first = true;
208                         for(String s : sp.getProtocolVersions()) {
209                                 if(first) {
210                                         first = false;
211                                 } else {
212                                         out.append(',');
213                                 }
214                                 out.append(s);
215                         }
216                 }
217                 boolean first = true;
218                 out.append("\n\tSubProtocol: ");
219                 for(String s : mep.getSubprotocol()) {
220                         if(first) {
221                                 first = false;
222                         } else {
223                                 out.append(',');
224                         }
225                         out.append(s);
226                 }
227         }
228     
229     private MgmtEndpoint copy(MgmtEndpoint mep) {
230                 MgmtEndpoint out = new MgmtEndpoint();
231                 out.setName(mep.getName());
232                 out.setHostname(mep.getHostname());
233                 out.setLatitude(mep.getLatitude());
234                 out.setLongitude(mep.getLongitude());
235                 out.setMajor(mep.getMajor());
236                 out.setMinor(mep.getMinor());
237                 out.setPatch(mep.getPatch());
238                 out.setPkg(mep.getPkg());
239                 out.setPort(mep.getPort());
240                 out.setProtocol(mep.getProtocol());
241                 out.getSpecialPorts().addAll(mep.getSpecialPorts());
242                 out.getSubprotocol().addAll(mep.getSubprotocol());
243                 return out;
244         }
245 }