Switch to new Locate Features
[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                                         locate.setPkg(split.length>3?Integer.parseInt(split[3]):0);
94                                         locate.setPatch(split.length>2?Integer.parseInt(split[2]):0);
95                                         locate.setMinor(split.length>1?Integer.parseInt(split[1]):0);
96                                         locate.setMajor(split.length>0?Integer.parseInt(split[0]):0);
97                                 }
98
99                                 String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, defProtocol);
100                                 if (protocol!=null) {
101                                         locate.setProtocol(protocol);
102                                                 List<String> ls = locate.getSubprotocol();
103                                                 // ls cannot be null, per generated getSubprotocol code
104                                                 if(ls.isEmpty()) {
105                                                 String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
106                                                 if(subprotocols==null) {
107                                                         subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
108                                                 }
109                                                 if(subprotocols!=null) {
110                                                         for (String s : Split.split(',', subprotocols)) {
111                                                                 ls.add(s);
112                                                         }
113                                                 } else {
114                                                         access.printf(Level.ERROR, "%s is required for Locator Registration of %s",
115                                                                         Config.AAF_LOCATOR_SUBPROTOCOL,Config.AAF_LOCATOR_PROTOCOL);
116                                                 }
117                                                 }
118                                         lme.add(locate);
119                                 } else {
120                                         access.printf(Level.ERROR, "%s is required for Locator Registration",Config.AAF_LOCATOR_PROTOCOL);
121                                 }
122                         }
123                 }
124         } catch (NumberFormatException | UnknownHostException e) {
125                 throw new CadiException("Error extracting Data from Properties for Registrar",e);
126         }
127         
128         if(access.willLog(Level.INFO)) {
129                 access.log(Level.INFO, print(new StringBuilder(),me.getMgmtEndpoint()));
130         }
131         return me;
132     }
133         
134     private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
135         int cnt = 0;
136                 for(MgmtEndpoint m : lme) {
137                         print(sb,cnt++,m);
138                 }
139                 return sb;
140         }
141
142         private void print(StringBuilder out, int cnt, MgmtEndpoint mep) {
143                 out.append("\nManagement Endpoint - ");
144                 out.append(cnt);
145                 out.append("\n\tName:       ");
146                 out.append(mep.getName());
147                 out.append("\n\tHostname:   ");
148                 out.append(mep.getHostname());
149                 out.append("\n\tLatitude:   ");
150                 out.append(mep.getLatitude());
151                 out.append("\n\tLongitude:  ");
152                 out.append(mep.getLongitude());
153                 out.append("\n\tVersion:    ");
154                 out.append(mep.getMajor());
155                 out.append('.');
156                 out.append(mep.getMinor());
157                 out.append('.');
158                 out.append(mep.getPkg());
159                 out.append('.');
160                 out.append(mep.getPatch());
161                 out.append("\n\tPort:       ");
162                 out.append(mep.getPort());
163                 out.append("\n\tProtocol:   ");
164                 out.append(mep.getProtocol());
165                 out.append("\n\tSpecial Ports:");
166                 for( SpecialPorts sp : mep.getSpecialPorts()) {
167                         out.append("\n\t\tName:       ");
168                         out.append(sp.getName());
169                         out.append("\n\t\tPort:       ");
170                         out.append(sp.getPort());
171                         out.append("\n\t\tProtocol:   ");
172                         out.append(sp.getProtocol());
173                         out.append("\n\t\t  Versions: ");
174                         boolean first = true;
175                         for(String s : sp.getProtocolVersions()) {
176                                 if(first) {
177                                         first = false;
178                                 } else {
179                                         out.append(',');
180                                 }
181                                 out.append(s);
182                         }
183                 }
184                 boolean first = true;
185                 out.append("\n\tSubProtocol: ");
186                 for(String s : mep.getSubprotocol()) {
187                         if(first) {
188                                 first = false;
189                         } else {
190                                 out.append(',');
191                         }
192                         out.append(s);
193                 }
194         }
195     
196     private MgmtEndpoint copy(MgmtEndpoint mep) {
197                 MgmtEndpoint out = new MgmtEndpoint();
198                 out.setName(mep.getName());
199                 out.setHostname(mep.getHostname());
200                 out.setLatitude(mep.getLatitude());
201                 out.setLongitude(mep.getLongitude());
202                 out.setMajor(mep.getMajor());
203                 out.setMinor(mep.getMinor());
204                 out.setPkg(mep.getPkg());
205                 out.setPatch(mep.getPatch());
206                 out.setPort(mep.getPort());
207                 out.setProtocol(mep.getProtocol());
208                 out.getSpecialPorts().addAll(mep.getSpecialPorts());
209                 out.getSubprotocol().addAll(mep.getSubprotocol());
210                 return out;
211         }
212 }