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;
26 import java.net.URISyntaxException;
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.config.RegistrationPropHolder;
40 import org.onap.aaf.cadi.locator.PropertyLocator;
41 import org.onap.aaf.cadi.locator.SingleEndpointLocator;
42 import org.onap.aaf.misc.env.APIException;
43 import org.onap.aaf.misc.env.impl.BasicEnv;
44 import org.onap.aaf.misc.rosetta.env.RosettaDF;
46 import locate.v1_0.MgmtEndpoints;
48 public class RemoteRegistrant<ENV extends BasicEnv> implements Registrant<ENV> {
49 private final MgmtEndpoints meps;
50 private final AAFCon<HttpURLConnection> aafcon;
51 private final RosettaDF<MgmtEndpoints> mgmtEndpointsDF;
52 private final Locator<URI> locator;
53 private final Access access;
54 private final int timeout;
56 public RemoteRegistrant(AAFCon<HttpURLConnection> aafcon, int port) throws CadiException, LocatorException {
58 access = aafcon.access;
60 mgmtEndpointsDF = aafcon.env.newDataFactory(MgmtEndpoints.class);
61 } catch (APIException e1) {
62 throw new CadiException(e1);
64 timeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
65 String aaf_locate = access.getProperty(Config.AAF_LOCATE_URL,null);
66 if (aaf_locate==null) {
67 throw new CadiException(Config.AAF_LOCATE_URL + " is required.");
69 // Note: want Property Locator or Single, not AAFLocator, because we want the core service, not what it can find
71 RegistrationPropHolder rph = new RegistrationPropHolder(access, 0);
72 aaf_locate = rph.replacements(getClass().getSimpleName(),aaf_locate, null,null);
73 if (aaf_locate.indexOf(',')>=0) {
74 locator = new PropertyLocator(aaf_locate);
76 locator = new SingleEndpointLocator(aaf_locate);
78 } catch (UnknownHostException e) {
79 throw new CadiException(e);
83 RegistrationCreator rcreator = new RegistrationCreator(access);
84 meps = rcreator.create(port);
90 public Result<Void> update(ENV env) {
92 Rcli<?> client = aafcon.client(locator);
94 Future<MgmtEndpoints> fup = client.update("/registration",mgmtEndpointsDF,meps);
95 if (fup.get(timeout)) {
96 access.log(Level.INFO, "Registration complete to",client.getURI());
97 return Result.ok(fup.code(),null);
99 access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
100 return Result.err(fup.code(),fup.body());
102 } catch (APIException e) {
103 access.log(e, "Error registering service to AAF Locator");
104 return Result.err(503,e.getMessage());
107 } catch (CadiException e) {
108 return Result.err(503,e.getMessage());
113 public Result<Void> cancel(ENV env) {
115 Rcli<?> client = aafcon.client(locator);
117 Future<MgmtEndpoints> fup = client.delete("/registration",mgmtEndpointsDF,meps);
118 if (fup.get(timeout)) {
119 access.log(Level.INFO, "Deregistration complete on",client.getURI());
120 return Result.ok(fup.code(),null);
122 return Result.err(fup.code(),fup.body());
124 } catch (APIException e) {
125 access.log(e, "Error deregistering service on AAF Locator");
126 return Result.err(503,e.getMessage());
129 } catch (CadiException e) {
130 return Result.err(503,e.getMessage());