Public and Private Locate entries
[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
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;
43
44 import locate.v1_0.MgmtEndpoints;
45
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;
53
54     public RemoteRegistrant(AAFCon<HttpURLConnection> aafcon, int port) throws CadiException, LocatorException {
55         this.aafcon = aafcon;
56         access = aafcon.access;
57         try {
58             mgmtEndpointsDF = aafcon.env.newDataFactory(MgmtEndpoints.class);
59         } catch (APIException e1) {
60             throw new CadiException(e1);
61         }
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.");
66         } else {
67             // Note: want Property Locator or Single, not AAFLocator, because we want the core service, not what it can find
68             try {
69                 if (aaf_locate.indexOf(',')>=0) {
70                     locator = new PropertyLocator(aaf_locate);
71                 } else {
72                     locator = new SingleEndpointLocator(aaf_locate);
73                 }
74             } catch (URISyntaxException e) {
75                 throw new CadiException(e);
76             }
77         }
78         
79         RegistrationCreator rcreator = new RegistrationCreator(access);
80         meps = rcreator.create(port);
81     }
82     
83
84
85         @Override
86     public Result<Void> update(ENV env) {
87         try {
88             Rcli<?> client = aafcon.client(locator);
89             try {
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);
94                 } else {
95                     access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
96                     return Result.err(fup.code(),fup.body());
97                 }
98             } catch (APIException e) {
99                 access.log(e, "Error registering service to AAF Locator");
100                 return Result.err(503,e.getMessage());
101             }
102             
103         } catch (CadiException e) {
104             return Result.err(503,e.getMessage());
105         }
106     }
107
108     @Override
109     public Result<Void> cancel(ENV env) {
110         try {
111             Rcli<?> client = aafcon.client(locator);
112             try {
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);
117                 } else {
118                     return Result.err(fup.code(),fup.body());
119                 }
120             } catch (APIException e) {
121                 access.log(e, "Error deregistering service on AAF Locator");
122                 return Result.err(503,e.getMessage());
123             }
124             
125         } catch (CadiException e) {
126             return Result.err(503,e.getMessage());
127         }
128     }
129
130 }