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;
28 import org.onap.aaf.cadi.Access;
29 import org.onap.aaf.cadi.Access.Level;
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.aaf.v2_0.AAFCon;
34 import org.onap.aaf.cadi.client.Future;
35 import org.onap.aaf.cadi.client.Rcli;
36 import org.onap.aaf.cadi.client.Result;
37 import org.onap.aaf.cadi.config.Config;
38 import org.onap.aaf.cadi.locator.PropertyLocator;
39 import org.onap.aaf.cadi.locator.SingleEndpointLocator;
40 import org.onap.aaf.misc.env.APIException;
41 import org.onap.aaf.misc.env.impl.BasicEnv;
42 import org.onap.aaf.misc.rosetta.env.RosettaDF;
44 import locate.v1_0.MgmtEndpoints;
46 public class RemoteRegistrant<ENV extends BasicEnv> implements Registrant<ENV> {
47 private final MgmtEndpoints meps;
48 private final AAFCon<HttpURLConnection> aafcon;
49 private final RosettaDF<MgmtEndpoints> mgmtEndpointsDF;
50 private final Locator<URI> locator;
51 private final Access access;
52 private final int timeout;
54 public RemoteRegistrant(AAFCon<HttpURLConnection> aafcon, int port) throws CadiException, LocatorException {
56 access = aafcon.access;
58 mgmtEndpointsDF = aafcon.env.newDataFactory(MgmtEndpoints.class);
59 } catch (APIException e1) {
60 throw new CadiException(e1);
62 timeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
63 String aaf_locate = access.getProperty(Config.AAF_LOCATE_URL,null);
64 if (aaf_locate==null) {
65 throw new CadiException(Config.AAF_LOCATE_URL + " is required.");
67 // Note: want Property Locator or Single, not AAFLocator, because we want the core service, not what it can find
69 if (aaf_locate.indexOf(',')>=0) {
70 locator = new PropertyLocator(aaf_locate);
72 locator = new SingleEndpointLocator(aaf_locate);
74 } catch (URISyntaxException e) {
75 throw new CadiException(e);
79 RegistrationCreator rcreator = new RegistrationCreator(access);
80 meps = rcreator.create(port);
86 public Result<Void> update(ENV env) {
88 Rcli<?> client = aafcon.client(locator);
90 Future<MgmtEndpoints> fup = client.update("/registration",mgmtEndpointsDF,meps);
91 if (fup.get(timeout)) {
92 access.log(Level.INFO, "Registration complete to",client.getURI());
93 return Result.ok(fup.code(),null);
95 access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
96 return Result.err(fup.code(),fup.body());
98 } catch (APIException e) {
99 access.log(e, "Error registering service to AAF Locator");
100 return Result.err(503,e.getMessage());
103 } catch (CadiException e) {
104 return Result.err(503,e.getMessage());
109 public Result<Void> cancel(ENV env) {
111 Rcli<?> client = aafcon.client(locator);
113 Future<MgmtEndpoints> fup = client.delete("/registration",mgmtEndpointsDF,meps);
114 if (fup.get(timeout)) {
115 access.log(Level.INFO, "Deregistration complete on",client.getURI());
116 return Result.ok(fup.code(),null);
118 return Result.err(fup.code(),fup.body());
120 } catch (APIException e) {
121 access.log(e, "Error deregistering service on AAF Locator");
122 return Result.err(503,e.getMessage());
125 } catch (CadiException e) {
126 return Result.err(503,e.getMessage());