Remove Tabs, per Jococo
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / register / RemoteRegistrant.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.register;
23
24 import java.net.HttpURLConnection;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.net.UnknownHostException;
28
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;
45
46 import locate.v1_0.MgmtEndpoints;
47
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;
55
56     public RemoteRegistrant(AAFCon<HttpURLConnection> aafcon, int port) throws CadiException, LocatorException {
57         this.aafcon = aafcon;
58         access = aafcon.access;
59         try {
60             mgmtEndpointsDF = aafcon.env.newDataFactory(MgmtEndpoints.class);
61         } catch (APIException e1) {
62             throw new CadiException(e1);
63         }
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.");
68         } else {
69             // Note: want Property Locator or Single, not AAFLocator, because we want the core service, not what it can find
70             try {
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);
75                 } else {
76                     locator = new SingleEndpointLocator(aaf_locate);
77                 }
78             } catch (URISyntaxException | UnknownHostException e) {
79                 throw new CadiException(e);
80             }
81         }
82         
83         RegistrationCreator rcreator = new RegistrationCreator(access);
84         meps = rcreator.create(port);
85     }
86     
87
88
89     @Override
90     public Result<Void> update(ENV env) {
91         try {
92             Rcli<?> client = aafcon.client(locator);
93             try {
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);
98                 } else {
99                     access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
100                     return Result.err(fup.code(),fup.body());
101                 }
102             } catch (APIException e) {
103                 access.log(e, "Error registering service to AAF Locator");
104                 return Result.err(503,e.getMessage());
105             }
106             
107         } catch (CadiException e) {
108             return Result.err(503,e.getMessage());
109         }
110     }
111
112     @Override
113     public Result<Void> cancel(ENV env) {
114         try {
115             Rcli<?> client = aafcon.client(locator);
116             try {
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);
121                 } else {
122                     return Result.err(fup.code(),fup.body());
123                 }
124             } catch (APIException e) {
125                 access.log(e, "Error deregistering service on AAF Locator");
126                 return Result.err(503,e.getMessage());
127             }
128             
129         } catch (CadiException e) {
130             return Result.err(503,e.getMessage());
131         }
132     }
133
134 }