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.CadiException;
31 import org.onap.aaf.cadi.Locator;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.Access.Level;
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 latitude = access.getProperty(Config.CADI_LATITUDE, null);
82 latitude = access.getProperty("AFT_LATITUDE", null);
84 String longitude = access.getProperty(Config.CADI_LONGITUDE, null);
86 longitude = access.getProperty("AFT_LONGITUDE", null);
88 if(latitude==null || longitude==null) {
89 throw new CadiException(Config.CADI_LATITUDE + " and " + Config.CADI_LONGITUDE + " is required");
91 mep.setLatitude(Float.parseFloat(latitude));
92 mep.setLongitude(Float.parseFloat(longitude));
94 String split[] = Split.split('.', version);
95 mep.setPkg(split.length>3?Integer.parseInt(split[3]):0);
96 mep.setPatch(split.length>2?Integer.parseInt(split[2]):0);
97 mep.setMinor(split.length>1?Integer.parseInt(split[1]):0);
98 mep.setMajor(split.length>0?Integer.parseInt(split[0]):0);
100 mep.setHostname(access.getProperty(Config.HOSTNAME, Inet4Address.getLocalHost().getHostName()));
101 String subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
102 if(subprotocols==null) {
103 mep.setProtocol("http");
105 mep.setProtocol("https");
106 for(String s : Split.split(',', subprotocols)) {
107 mep.getSubprotocol().add(s);
110 } catch (NumberFormatException | UnknownHostException e) {
111 throw new CadiException("Error extracting Data from Properties for Registrar",e);
113 meps = new MgmtEndpoints();
114 meps.getMgmtEndpoint().add(mep);
115 for(RemoteRegistrant<ENV> rr : others) {
116 meps.getMgmtEndpoint().add(rr.mep);
121 public Result<Void> update(ENV env) {
123 Rcli<?> client = aafcon.client(locator);
125 Future<MgmtEndpoints> fup = client.update("/registration",mgmtEndpointsDF,meps);
126 if(fup.get(timeout)) {
127 access.log(Level.INFO, "Registration complete to",client.getURI());
128 return Result.ok(fup.code(),null);
130 access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
131 return Result.err(fup.code(),fup.body());
133 } catch (APIException e) {
134 access.log(e, "Error registering service to AAF Locator");
135 return Result.err(503,e.getMessage());
138 } catch (CadiException e) {
139 return Result.err(503,e.getMessage());
144 public Result<Void> cancel(ENV env) {
146 Rcli<?> client = aafcon.client(locator);
148 Future<MgmtEndpoints> fup = client.delete("/registration",mgmtEndpointsDF,meps);
149 if(fup.get(timeout)) {
150 access.log(Level.INFO, "Deregistration complete on",client.getURI());
151 return Result.ok(fup.code(),null);
153 return Result.err(fup.code(),fup.body());
155 } catch (APIException e) {
156 access.log(e, "Error deregistering service on AAF Locator");
157 return Result.err(503,e.getMessage());
160 } catch (CadiException e) {
161 return Result.err(503,e.getMessage());