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 // Now, loop through by Container
59 for(String le : Split.splitTrim(',', ph.lcontainer)) {
66 for(String entry : Split.splitTrim(',', ph.lentries)) {
68 defData = locate = new MgmtEndpoint();
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,""))) {
76 locate.setLatitude(ph.latitude);
77 locate.setLongitude(ph.longitude);
80 locate = copy(defData);
83 locate.setName(ph.getEntryName(entry,dot_le));
84 locate.setHostname(ph.getEntryFQDN(entry,dot_le));
85 locate.setPort(ph.getEntryPort(dot_le));
87 String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
88 if(specificVersion == null && locate == defData) {
89 specificVersion = version;
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));
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
106 String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
107 if(subprotocols==null) {
108 subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
110 if(subprotocols!=null) {
111 for (String s : Split.split(',', subprotocols)) {
115 access.printf(Level.ERROR, "%s is required for Locator Registration of %s",
116 Config.AAF_LOCATOR_SUBPROTOCOL,Config.AAF_LOCATOR_PROTOCOL);
121 access.printf(Level.ERROR, "%s is required for Locator Registration",Config.AAF_LOCATOR_PROTOCOL);
125 } catch (NumberFormatException | UnknownHostException e) {
126 throw new CadiException("Error extracting Data from Properties for Registrar",e);
129 if(access.willLog(Level.INFO)) {
130 access.log(Level.INFO, print(new StringBuilder(),me.getMgmtEndpoint()));
136 * Find the best version between Actual Interface and Deployed version
138 private int best(String[] split, String[] deploy, int i) {
139 StringBuilder sb = new StringBuilder();
144 for(int j=0;j<s.length();++j) {
145 if(Character.isDigit(c=s.charAt(j))) {
153 if(sb.length()==0 && deploy.length>i) {
155 for(int j=0;j<s.length();++j) {
156 if(Character.isDigit(c=s.charAt(j))) {
164 return sb.length()==0?0:Integer.parseInt(sb.toString());
167 private StringBuilder print(StringBuilder sb, List<MgmtEndpoint> lme) {
169 for(MgmtEndpoint m : lme) {
175 private void print(StringBuilder out, int cnt, MgmtEndpoint mep) {
176 out.append("\nManagement Endpoint - ");
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());
189 out.append(mep.getMinor());
191 out.append(mep.getPatch());
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()) {
217 boolean first = true;
218 out.append("\n\tSubProtocol: ");
219 for(String s : mep.getSubprotocol()) {
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());