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.URISyntaxException;
28 import java.net.UnknownHostException;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.Locator;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
36 import org.onap.aaf.cadi.client.Future;
37 import org.onap.aaf.cadi.client.Rcli;
38 import org.onap.aaf.cadi.client.Result;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.locator.PropertyLocator;
41 import org.onap.aaf.cadi.locator.SingleEndpointLocator;
42 import org.onap.aaf.cadi.util.Split;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.impl.BasicEnv;
45 import org.onap.aaf.misc.rosetta.env.RosettaDF;
47 import locate.v1_0.MgmtEndpoint;
48 import locate.v1_0.MgmtEndpoints;
50 public class RemoteRegistrant<ENV extends BasicEnv> implements Registrant<ENV> {
51 private final MgmtEndpoint mep;
52 private final MgmtEndpoints meps;
53 private final AAFCon<HttpURLConnection> aafcon;
54 private final RosettaDF<MgmtEndpoints> mgmtEndpointsDF;
55 private final Locator<URI> locator;
56 private final Access access;
57 private final int timeout;
60 public RemoteRegistrant(AAFCon<HttpURLConnection> aafcon, String name, String version, int port, RemoteRegistrant<ENV> ... others) throws CadiException, LocatorException {
62 access = aafcon.access;
64 mgmtEndpointsDF = aafcon.env.newDataFactory(MgmtEndpoints.class);
65 } catch (APIException e1) {
66 throw new CadiException(e1);
68 timeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
69 String aaf_locate = access.getProperty(Config.AAF_LOCATE_URL,null);
70 if(aaf_locate==null) {
71 throw new CadiException(Config.AAF_LOCATE_URL + " is required.");
73 // Note: want Property Locator or Single, not AAFLocator, because we want the core service, not what it can find
75 if(aaf_locate.indexOf(',')>=0) {
76 locator = new PropertyLocator(aaf_locate);
78 locator = new SingleEndpointLocator(aaf_locate);
80 } catch (URISyntaxException e) {
81 throw new CadiException(e);
85 mep = new MgmtEndpoint();
90 String hostnameToRegister = access.getProperty(Config.AAF_REGISTER_AS, null);
91 if(hostnameToRegister==null) {
92 hostnameToRegister = access.getProperty(Config.HOSTNAME, null);
94 if(hostnameToRegister==null) {
95 hostnameToRegister = Inet4Address.getLocalHost().getHostName();
97 mep.setHostname(hostnameToRegister);
99 String latitude = access.getProperty(Config.CADI_LATITUDE, null);
101 latitude = access.getProperty("AFT_LATITUDE", null);
103 String longitude = access.getProperty(Config.CADI_LONGITUDE, null);
104 if(longitude==null) {
105 longitude = access.getProperty("AFT_LONGITUDE", null);
107 if(latitude==null || longitude==null) {
108 throw new CadiException(Config.CADI_LATITUDE + " and " + Config.CADI_LONGITUDE + " is required");
110 mep.setLatitude(Float.parseFloat(latitude));
111 mep.setLongitude(Float.parseFloat(longitude));
113 String split[] = Split.split('.', version);
114 mep.setPkg(split.length>3?Integer.parseInt(split[3]):0);
115 mep.setPatch(split.length>2?Integer.parseInt(split[2]):0);
116 mep.setMinor(split.length>1?Integer.parseInt(split[1]):0);
117 mep.setMajor(split.length>0?Integer.parseInt(split[0]):0);
119 String subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
120 if(subprotocols==null) {
121 mep.setProtocol("http");
123 mep.setProtocol("https");
124 for(String s : Split.split(',', subprotocols)) {
125 mep.getSubprotocol().add(s);
128 } catch (NumberFormatException | UnknownHostException e) {
129 throw new CadiException("Error extracting Data from Properties for Registrar",e);
131 meps = new MgmtEndpoints();
132 meps.getMgmtEndpoint().add(mep);
133 for(RemoteRegistrant<ENV> rr : others) {
134 meps.getMgmtEndpoint().add(rr.mep);
139 public Result<Void> update(ENV env) {
141 Rcli<?> client = aafcon.client(locator);
143 Future<MgmtEndpoints> fup = client.update("/registration",mgmtEndpointsDF,meps);
144 if(fup.get(timeout)) {
145 access.log(Level.INFO, "Registration complete to",client.getURI());
146 return Result.ok(fup.code(),null);
148 access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
149 return Result.err(fup.code(),fup.body());
151 } catch (APIException e) {
152 access.log(e, "Error registering service to AAF Locator");
153 return Result.err(503,e.getMessage());
156 } catch (CadiException e) {
157 return Result.err(503,e.getMessage());
162 public Result<Void> cancel(ENV env) {
164 Rcli<?> client = aafcon.client(locator);
166 Future<MgmtEndpoints> fup = client.delete("/registration",mgmtEndpointsDF,meps);
167 if(fup.get(timeout)) {
168 access.log(Level.INFO, "Deregistration complete on",client.getURI());
169 return Result.ok(fup.code(),null);
171 return Result.err(fup.code(),fup.body());
173 } catch (APIException e) {
174 access.log(e, "Error deregistering service on AAF Locator");
175 return Result.err(503,e.getMessage());
178 } catch (CadiException e) {
179 return Result.err(503,e.getMessage());