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====================================================
22 package org.onap.aaf.cadi.register;
24 import java.net.HttpURLConnection;
25 import java.net.Inet4Address;
27 import java.net.UnknownHostException;
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.Access.Level;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.Locator;
33 import org.onap.aaf.cadi.LocatorException;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Result;
38 import org.onap.aaf.cadi.config.Config;
39 import org.onap.aaf.cadi.locator.PropertyLocator;
40 import org.onap.aaf.cadi.util.Split;
41 import org.onap.aaf.misc.env.APIException;
42 import org.onap.aaf.misc.env.impl.BasicEnv;
43 import org.onap.aaf.misc.rosetta.env.RosettaDF;
45 import locate.v1_0.MgmtEndpoint;
46 import locate.v1_0.MgmtEndpoints;
48 public class RemoteRegistrant<ENV extends BasicEnv> implements Registrant<ENV> {
49 private final MgmtEndpoint mep;
50 private final MgmtEndpoints meps;
51 private final AAFCon<HttpURLConnection> aafcon;
52 private final RosettaDF<MgmtEndpoints> mgmtEndpointsDF;
53 private final Locator<URI> locator;
54 private final Access access;
55 private final int timeout;
58 public RemoteRegistrant(AAFCon<HttpURLConnection> aafcon, String name, String version, int port, RemoteRegistrant<ENV> ... others) throws CadiException, LocatorException {
60 access = aafcon.access;
62 mgmtEndpointsDF = aafcon.env.newDataFactory(MgmtEndpoints.class);
63 } catch (APIException e1) {
64 throw new CadiException(e1);
66 timeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
67 String aaf_locate = access.getProperty(Config.AAF_LOCATE_URL,null);
68 if(aaf_locate==null) {
69 throw new CadiException(Config.AAF_LOCATE_URL + " is required.");
71 // Note: want Property Locator, not AAFLocator, because we want the core service, not what it can find
72 locator = new PropertyLocator(aaf_locate);
75 mep = new MgmtEndpoint();
80 String hostnameToRegister = access.getProperty(Config.CADI_REGISTRATION_HOSTNAME, null);
81 if(hostnameToRegister==null) {
82 hostnameToRegister = access.getProperty(Config.HOSTNAME, null);
84 if(hostnameToRegister==null) {
85 hostnameToRegister = Inet4Address.getLocalHost().getHostName();
87 mep.setHostname(hostnameToRegister);
89 String latitude = access.getProperty(Config.CADI_LATITUDE, null);
91 latitude = access.getProperty("AFT_LATITUDE", null);
93 String longitude = access.getProperty(Config.CADI_LONGITUDE, null);
95 longitude = access.getProperty("AFT_LONGITUDE", null);
97 if(latitude==null || longitude==null) {
98 throw new CadiException(Config.CADI_LATITUDE + " and " + Config.CADI_LONGITUDE + " is required");
100 mep.setLatitude(Float.parseFloat(latitude));
101 mep.setLongitude(Float.parseFloat(longitude));
103 String split[] = Split.split('.', version);
104 mep.setPkg(split.length>3?Integer.parseInt(split[3]):0);
105 mep.setPatch(split.length>2?Integer.parseInt(split[2]):0);
106 mep.setMinor(split.length>1?Integer.parseInt(split[1]):0);
107 mep.setMajor(split.length>0?Integer.parseInt(split[0]):0);
109 String subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
110 if(subprotocols==null) {
111 mep.setProtocol("http");
113 mep.setProtocol("https");
114 for(String s : Split.split(',', subprotocols)) {
115 mep.getSubprotocol().add(s);
118 } catch (NumberFormatException | UnknownHostException e) {
119 throw new CadiException("Error extracting Data from Properties for Registrar",e);
121 meps = new MgmtEndpoints();
122 meps.getMgmtEndpoint().add(mep);
123 for(RemoteRegistrant<ENV> rr : others) {
124 meps.getMgmtEndpoint().add(rr.mep);
129 public Result<Void> update(ENV env) {
131 Rcli<?> client = aafcon.client(locator);
133 Future<MgmtEndpoints> fup = client.update("/registration",mgmtEndpointsDF,meps);
134 if(fup.get(timeout)) {
135 access.log(Level.INFO, "Registration complete to",client.getURI());
136 return Result.ok(fup.code(),null);
138 access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
139 return Result.err(fup.code(),fup.body());
141 } catch (APIException e) {
142 access.log(e, "Error registering service to AAF Locator");
143 return Result.err(503,e.getMessage());
146 } catch (CadiException e) {
147 return Result.err(503,e.getMessage());
152 public Result<Void> cancel(ENV env) {
154 Rcli<?> client = aafcon.client(locator);
156 Future<MgmtEndpoints> fup = client.delete("/registration",mgmtEndpointsDF,meps);
157 if(fup.get(timeout)) {
158 access.log(Level.INFO, "Deregistration complete on",client.getURI());
159 return Result.ok(fup.code(),null);
161 return Result.err(fup.code(),fup.body());
163 } catch (APIException e) {
164 access.log(e, "Error deregistering service on AAF Locator");
165 return Result.err(503,e.getMessage());
168 } catch (CadiException e) {
169 return Result.err(503,e.getMessage());