Create Helm based Certificates for Clients
[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                 
55                 RegistrationPropHolder ph = new RegistrationPropHolder(access, port);
56                 
57                 // Now, loop through by Container
58                 for(String le : Split.splitTrim(',', ph.lcontainer)) {
59                         if(le.isEmpty()) {
60                                 dot_le = le;
61                         } else {
62                                 dot_le = "."+le;
63                         }
64
65                         for(String entry : Split.splitTrim(',', ph.lentries)) {
66                                 if(defData==null) {
67                                         defData = locate = new MgmtEndpoint();
68
69                                         version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION);
70                                         locate.setProtocol(access.getProperty(Config.AAF_LOCATOR_PROTOCOL,null));
71                                         List<String> ls = locate.getSubprotocol();
72                                         for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) {
73                                                 ls.add(sp);     
74                                         }
75                                         locate.setLatitude(ph.latitude);
76                                         locate.setLongitude(ph.longitude);
77
78                                 } else {
79                                         locate = copy(defData);
80                                 }
81                                 
82                                 locate.setName(ph.getEntryName(entry,dot_le));
83                                 locate.setHostname(ph.getEntryFQDN(entry,dot_le));
84                                 locate.setPort(ph.getEntryPort(dot_le));
85                                 
86                                 String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
87                                 if(specificVersion == null && locate == defData) {
88                                         specificVersion = version;
89                                 }
90                                 if(specificVersion!=null) {
91                                         String split[] = Split.splitTrim('.', specificVersion);
92                                         locate.setPkg(split.length>3?Integer.parseInt(split[3]):0);
93                                         locate.setPatch(split.length>2?Integer.parseInt(split[2]):0);
94                                         locate.setMinor(split.length>1?Integer.parseInt(split[1]):0);
95                                         locate.setMajor(split.length>0?Integer.parseInt(split[0]):0);
96                                 }
97
98                                 String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, null);
99                                 if (protocol!=null) {
100                                         locate.setProtocol(protocol);
101                                         String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
102                                         if(subprotocols!=null) {
103                                                 List<String> ls = locate.getSubprotocol();
104                                                 for (String s : Split.split(',', subprotocols)) {
105                                                         ls.add(s);
106                                                 }
107                                         }
108                                 }
109                                 lme.add(locate);
110                         }
111                 }
112         } catch (NumberFormatException | UnknownHostException e) {
113                 throw new CadiException("Error extracting Data from Properties for Registrar",e);
114         }
115         
116         if(access.willLog(Level.INFO)) {
117                 access.log(Level.INFO, print(new StringBuilder(),me.getMgmtEndpoint()));
118         }
119         return me;
120     }
121         
122     private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
123         int cnt = 0;
124                 for(MgmtEndpoint m : lme) {
125                         print(sb,cnt++,m);
126                 }
127                 return sb;
128         }
129
130         private void print(StringBuilder out, int cnt, MgmtEndpoint mep) {
131                 out.append("\nManagement Endpoint - ");
132                 out.append(cnt);
133                 out.append("\n\tName:       ");
134                 out.append(mep.getName());
135                 out.append("\n\tHostname:   ");
136                 out.append(mep.getHostname());
137                 out.append("\n\tLatitude:   ");
138                 out.append(mep.getLatitude());
139                 out.append("\n\tLongitude:  ");
140                 out.append(mep.getLongitude());
141                 out.append("\n\tVersion:    ");
142                 out.append(mep.getMajor());
143                 out.append('.');
144                 out.append(mep.getMinor());
145                 out.append('.');
146                 out.append(mep.getPkg());
147                 out.append('.');
148                 out.append(mep.getPatch());
149                 out.append("\n\tPort:       ");
150                 out.append(mep.getPort());
151                 out.append("\n\tProtocol:   ");
152                 out.append(mep.getProtocol());
153                 out.append("\n\tSpecial Ports:");
154                 for( SpecialPorts sp : mep.getSpecialPorts()) {
155                         out.append("\n\t\tName:       ");
156                         out.append(sp.getName());
157                         out.append("\n\t\tPort:       ");
158                         out.append(sp.getPort());
159                         out.append("\n\t\tProtocol:   ");
160                         out.append(sp.getProtocol());
161                         out.append("\n\t\t  Versions: ");
162                         boolean first = true;
163                         for(String s : sp.getProtocolVersions()) {
164                                 if(first) {
165                                         first = false;
166                                 } else {
167                                         out.append(',');
168                                 }
169                                 out.append(s);
170                         }
171                 }
172                 boolean first = true;
173                 out.append("\n\tSubProtocol: ");
174                 for(String s : mep.getSubprotocol()) {
175                         if(first) {
176                                 first = false;
177                         } else {
178                                 out.append(',');
179                         }
180                         out.append(s);
181                 }
182         }
183     
184     private MgmtEndpoint copy(MgmtEndpoint mep) {
185                 MgmtEndpoint out = new MgmtEndpoint();
186                 out.setName(mep.getName());
187                 out.setHostname(mep.getHostname());
188                 out.setLatitude(mep.getLatitude());
189                 out.setLongitude(mep.getLongitude());
190                 out.setMajor(mep.getMajor());
191                 out.setMinor(mep.getMinor());
192                 out.setPkg(mep.getPkg());
193                 out.setPatch(mep.getPatch());
194                 out.setPort(mep.getPort());
195                 out.setProtocol(mep.getProtocol());
196                 out.getSpecialPorts().addAll(mep.getSpecialPorts());
197                 out.getSubprotocol().addAll(mep.getSubprotocol());
198                 return out;
199         }
200 }