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