2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.cadi.register;
23 import java.net.UnknownHostException;
24 import java.util.List;
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;
34 import locate.v1_0.MgmtEndpoint;
35 import locate.v1_0.MgmtEndpoint.SpecialPorts;
36 import locate.v1_0.MgmtEndpoints;
38 public class RegistrationCreator {
39 private Access access;
41 public RegistrationCreator(Access access) {
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;
54 String defProtocol="https";
56 RegistrationPropHolder ph = new RegistrationPropHolder(access, port);
58 String firstPrivateHostname = null;
59 // Now, loop through by Container
60 for(String le : Split.splitTrim(',', ph.lcontainer)) {
67 for(String entry : Split.splitTrim(',', ph.lentries)) {
69 defData = locate = new MgmtEndpoint();
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,""))) {
77 locate.setLatitude(ph.latitude);
78 locate.setLongitude(ph.longitude);
81 locate = copy(defData);
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
88 * aaf_locator_entries=oauth,token,introspect
90 * Entries for token and introspect, but they point to oauth service.
92 String locateHostname;
94 locateHostname=ph.getEntryFQDN(entry, dot_le);
95 } else if(firstPrivateHostname==null) {
96 firstPrivateHostname=locateHostname=ph.getEntryFQDN(entry, dot_le);
98 locateHostname=firstPrivateHostname;
101 locate.setHostname(locateHostname);
102 locate.setPort(ph.getEntryPort(dot_le));
104 String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
105 if(specificVersion == null && locate == defData) {
106 specificVersion = version;
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));
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
123 String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
124 if(subprotocols==null) {
125 subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
127 if(subprotocols!=null) {
128 for (String s : Split.split(',', subprotocols)) {
132 access.printf(Level.ERROR, "%s is required for Locator Registration of %s",
133 Config.AAF_LOCATOR_SUBPROTOCOL,Config.AAF_LOCATOR_PROTOCOL);
138 access.printf(Level.ERROR, "%s is required for Locator Registration",Config.AAF_LOCATOR_PROTOCOL);
142 } catch (NumberFormatException | UnknownHostException e) {
143 throw new CadiException("Error extracting Data from Properties for Registrar",e);
146 if(access.willLog(Level.INFO)) {
147 access.log(Level.INFO, print(new StringBuilder(),me.getMgmtEndpoint()));
153 * Find the best version between Actual Interface and Deployed version
155 private int best(String[] split, String[] deploy, int i) {
156 StringBuilder sb = new StringBuilder();
161 for(int j=0;j<s.length();++j) {
162 if(Character.isDigit(c=s.charAt(j))) {
170 if(sb.length()==0 && deploy.length>i) {
172 for(int j=0;j<s.length();++j) {
173 if(Character.isDigit(c=s.charAt(j))) {
181 return sb.length()==0?0:Integer.parseInt(sb.toString());
184 private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
186 for(MgmtEndpoint m : lme) {
192 private void print(StringBuilder out, int cnt, MgmtEndpoint mep) {
193 out.append("\nManagement Endpoint - ");
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());
206 out.append(mep.getMinor());
208 out.append(mep.getPatch());
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()) {
234 boolean first = true;
235 out.append("\n\tSubProtocol: ");
236 for(String s : mep.getSubprotocol()) {
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());