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.Inet4Address;
24 import java.net.UnknownHostException;
25 import java.util.List;
27 import org.onap.aaf.cadi.Access;
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.util.Split;
33 import locate.v1_0.MgmtEndpoint;
34 import locate.v1_0.MgmtEndpoints;
36 public class RegistrationCreator {
37 private static final String MUST_BE_DEFINED = " must be defined\n";
38 private Access access;
40 public RegistrationCreator(Access access) {
44 public MgmtEndpoints create(final int port) throws CadiException {
45 MgmtEndpoints me = new MgmtEndpoints();
46 List<MgmtEndpoint> lme = me.getMgmtEndpoint();
47 MgmtEndpoint defData = null;
48 MgmtEndpoint locate = null;
51 StringBuilder errs = new StringBuilder();
53 String hostname = access.getProperty(Config.HOSTNAME, null);
55 hostname = Inet4Address.getLocalHost().getHostName();
58 errs.append(Config.HOSTNAME);
59 errs.append(MUST_BE_DEFINED);
63 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
64 if(slatitude == null) {
65 errs.append(Config.CADI_LATITUDE);
66 errs.append(MUST_BE_DEFINED);
68 latitude = Float.parseFloat(slatitude);
72 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
73 if(slongitude == null) {
74 errs.append(Config.CADI_LONGITUDE);
75 errs.append(MUST_BE_DEFINED);
77 longitude = Float.parseFloat(slongitude);
81 throw new CadiException(errs.toString());
87 String lentries = access.getProperty(Config.AAF_LOCATOR_CONTAINER, null);
91 lentries=',' + lentries; // "" makes a blank default Public Entry
94 String defaultName = null;
96 int public_port = port;
97 // Note: only one of the ports can be public... Therefore, only the la
98 for(String le : Split.splitTrim(',', lentries)) {
99 dot_le = le.isEmpty()?"":"."+le;
100 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+dot_le, null);
101 if(str!=null) { // Get Public Port
102 public_port = Integer.decode(str);
106 String public_hostname = hostname;
107 for(String le : Split.splitTrim(',', lentries)) {
108 dot_le = le.isEmpty()?"":"."+le;
109 String ph = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null);
115 String default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, public_hostname);
118 // Now, loop through by Container
119 for(String le : Split.splitTrim(',', lentries)) {
120 // Add variable entries
124 names = access.getProperty(Config.AAF_LOCATOR_NAMES+dot_le,null);
127 names = access.getProperty(Config.AAF_LOCATOR_NAMES,"");
131 names=access.getProperty(Config.AAF_LOCATOR_NAMES,dot_le);
134 for(String name : Split.splitTrim(',', names)) {
136 defData = locate = new MgmtEndpoint();
139 version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION);
140 locate.setProtocol(access.getProperty(Config.AAF_LOCATOR_PROTOCOL,null));
141 List<String> ls = locate.getSubprotocol();
142 for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) {
145 locate.setLatitude(latitude);
146 locate.setLongitude(longitude);
149 locate = copy(defData);
152 str = access.getProperty(Config.HOSTNAME+dot_le, null);
154 str = access.getProperty(Config.HOSTNAME, hostname);
156 locate.setHostname(hostname);
158 ns = access.getProperty(Config.AAF_LOCATOR_NS+dot_le,null);
160 ns = access.getProperty(Config.AAF_LOCATOR_NS,"");
163 case Defaults.AAF_NS:
164 ns = access.getProperty(Config.AAF_ROOT_NS, "");
165 // Fallthrough on purpose.
175 String container_id = access.getProperty(Config.AAF_LOCATOR_CONTAINER_ID+dot_le, "");
176 if(!container_id.isEmpty()) {
177 ns_dot = container_id + '.' + ns_dot;
181 ns_dot = le + '.' + ns_dot;
185 locate.setName(ns_dot + defaultName);
187 locate.setName(ns_dot + name);
190 if(dot_le.isEmpty()) {
191 locate.setHostname(access.getProperty(Config.AAF_LOCATOR_FQDN, default_fqdn));
193 str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, null);
195 locate.setHostname(default_fqdn);
197 String container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, "");
198 str = str.replace("%CNS", container_ns);
199 String container = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le, "");
200 str = str.replace("%C", container);
201 str = str.replace("%NS", ns);
202 str = str.replace("%N", name);
203 str = str.replace("%DF", default_fqdn);
204 str = str.replace("%PH", public_hostname);
205 locate.setHostname(str);
210 locate.setPort(public_port);
212 locate.setPort(port);
215 String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
216 if(specificVersion == null && locate == defData) {
217 specificVersion = version;
219 if(specificVersion!=null) {
220 String split[] = Split.splitTrim('.', specificVersion);
221 locate.setPkg(split.length>3?Integer.parseInt(split[3]):0);
222 locate.setPatch(split.length>2?Integer.parseInt(split[2]):0);
223 locate.setMinor(split.length>1?Integer.parseInt(split[1]):0);
224 locate.setMajor(split.length>0?Integer.parseInt(split[0]):0);
227 String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, null);
228 if (protocol!=null) {
229 locate.setProtocol(protocol);
230 String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
231 if(subprotocols!=null) {
232 List<String> ls = locate.getSubprotocol();
233 for (String s : Split.split(',', subprotocols)) {
241 } catch (NumberFormatException | UnknownHostException e) {
242 throw new CadiException("Error extracting Data from Properties for Registrar",e);
248 private MgmtEndpoint copy(MgmtEndpoint mep) {
249 MgmtEndpoint out = new MgmtEndpoint();
250 out.setName(mep.getName());
251 out.setHostname(mep.getHostname());
252 out.setLatitude(mep.getLatitude());
253 out.setLongitude(mep.getLongitude());
254 out.setMajor(mep.getMajor());
255 out.setMinor(mep.getMinor());
256 out.setPkg(mep.getPkg());
257 out.setPatch(mep.getPatch());
258 out.setPort(mep.getPort());
259 out.setProtocol(mep.getProtocol());
260 out.getSpecialPorts().addAll(mep.getSpecialPorts());
261 out.getSubprotocol().addAll(mep.getSubprotocol());