b21f8975bf4ca49acafef6dab5283cb45f1d908c
[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.Inet4Address;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.net.UnknownHostException;
29
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.Locator;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
36 import org.onap.aaf.cadi.client.Future;
37 import org.onap.aaf.cadi.client.Rcli;
38 import org.onap.aaf.cadi.client.Result;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.locator.PropertyLocator;
41 import org.onap.aaf.cadi.locator.SingleEndpointLocator;
42 import org.onap.aaf.cadi.util.Split;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.impl.BasicEnv;
45 import org.onap.aaf.misc.rosetta.env.RosettaDF;
46
47 import locate.v1_0.MgmtEndpoint;
48 import locate.v1_0.MgmtEndpoints;
49
50 public class RemoteRegistrant<ENV extends BasicEnv> implements Registrant<ENV> {
51         private final MgmtEndpoint mep;
52         private final MgmtEndpoints meps;
53         private final AAFCon<HttpURLConnection> aafcon;
54         private final RosettaDF<MgmtEndpoints> mgmtEndpointsDF;
55         private final Locator<URI> locator;
56         private final Access access;
57         private final int timeout;
58
59         @SafeVarargs
60         public RemoteRegistrant(AAFCon<HttpURLConnection> aafcon, String name, String version, int port, RemoteRegistrant<ENV> ... others) throws CadiException, LocatorException {
61                 this.aafcon = aafcon;
62                 access = aafcon.access;
63                 try {
64                         mgmtEndpointsDF = aafcon.env.newDataFactory(MgmtEndpoints.class);
65                 } catch (APIException e1) {
66                         throw new CadiException(e1);
67                 }
68                 timeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
69                 String aaf_locate = access.getProperty(Config.AAF_LOCATE_URL,null);
70                 if(aaf_locate==null) {
71                         throw new CadiException(Config.AAF_LOCATE_URL + " is required.");
72                 } else {
73                         // Note: want Property Locator or Single, not AAFLocator, because we want the core service, not what it can find
74                         try {
75                                 if(aaf_locate.indexOf(',')>=0) {
76                                         locator = new PropertyLocator(aaf_locate);
77                                 } else {
78                                         locator = new SingleEndpointLocator(aaf_locate);
79                                 }
80                         } catch (URISyntaxException e) {
81                                 throw new CadiException(e);
82                         }
83                 }
84                 
85                 mep = new MgmtEndpoint();
86                 mep.setName(name);
87                 mep.setPort(port);
88
89                 try {
90                         String hostnameToRegister = access.getProperty(Config.AAF_REGISTER_AS, null);
91                         if(hostnameToRegister==null) {
92                                 hostnameToRegister = access.getProperty(Config.HOSTNAME, null);
93                         }
94                         if(hostnameToRegister==null) {
95                                 hostnameToRegister = Inet4Address.getLocalHost().getHostName();
96                         }
97                         mep.setHostname(hostnameToRegister);
98                         
99                         String latitude = access.getProperty(Config.CADI_LATITUDE, null);
100                         if(latitude==null) {
101                                 latitude = access.getProperty("AFT_LATITUDE", null);
102                         }
103                         String longitude = access.getProperty(Config.CADI_LONGITUDE, null);
104                         if(longitude==null) {
105                                 longitude = access.getProperty("AFT_LONGITUDE", null);
106                         }
107                         if(latitude==null || longitude==null) {
108                                 throw new CadiException(Config.CADI_LATITUDE + " and " + Config.CADI_LONGITUDE + " is required");
109                         } else {
110                                 mep.setLatitude(Float.parseFloat(latitude));
111                                 mep.setLongitude(Float.parseFloat(longitude));
112                         }
113                         String split[] = Split.split('.', version);
114                         mep.setPkg(split.length>3?Integer.parseInt(split[3]):0);
115                         mep.setPatch(split.length>2?Integer.parseInt(split[2]):0);
116                         mep.setMinor(split.length>1?Integer.parseInt(split[1]):0);
117                         mep.setMajor(split.length>0?Integer.parseInt(split[0]):0);
118                         
119                         String subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
120                         if(subprotocols==null) {
121                                 mep.setProtocol("http");
122                         } else {
123                                 mep.setProtocol("https");
124                                 for(String s : Split.split(',', subprotocols)) {
125                                         mep.getSubprotocol().add(s);
126                                 }
127                         }
128                 } catch (NumberFormatException | UnknownHostException e) {
129                         throw new CadiException("Error extracting Data from Properties for Registrar",e);
130                 }
131                 meps = new MgmtEndpoints();
132                 meps.getMgmtEndpoint().add(mep);
133                 for(RemoteRegistrant<ENV> rr : others) {
134                         meps.getMgmtEndpoint().add(rr.mep);
135                 }
136         }
137         
138         @Override
139         public Result<Void> update(ENV env) {
140                 try {
141                         Rcli<?> client = aafcon.client(locator);
142                         try {
143                                 Future<MgmtEndpoints> fup = client.update("/registration",mgmtEndpointsDF,meps);
144                                 if(fup.get(timeout)) {
145                                         access.log(Level.INFO, "Registration complete to",client.getURI());
146                                         return Result.ok(fup.code(),null);
147                                 } else {
148                                         access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
149                                         return Result.err(fup.code(),fup.body());
150                                 }
151                         } catch (APIException e) {
152                                 access.log(e, "Error registering service to AAF Locator");
153                                 return Result.err(503,e.getMessage());
154                         }
155                         
156                 } catch (CadiException e) {
157                         return Result.err(503,e.getMessage());
158                 }
159         }
160
161         @Override
162         public Result<Void> cancel(ENV env) {
163                 try {
164                         Rcli<?> client = aafcon.client(locator);
165                         try {
166                                 Future<MgmtEndpoints> fup = client.delete("/registration",mgmtEndpointsDF,meps);
167                                 if(fup.get(timeout)) {
168                                         access.log(Level.INFO, "Deregistration complete on",client.getURI());
169                                         return Result.ok(fup.code(),null);
170                                 } else {
171                                         return Result.err(fup.code(),fup.body());
172                                 }
173                         } catch (APIException e) {
174                                 access.log(e, "Error deregistering service on AAF Locator");
175                                 return Result.err(503,e.getMessage());
176                         }
177                         
178                 } catch (CadiException e) {
179                         return Result.err(503,e.getMessage());
180                 }
181         }
182
183 }