AT&T 2.0.19 Code drop, stage 2
[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.CadiException;
31 import org.onap.aaf.cadi.Locator;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.Access.Level;
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 latitude = access.getProperty(Config.CADI_LATITUDE, null);
81                         if(latitude==null) {
82                                 latitude = access.getProperty("AFT_LATITUDE", null);
83                         }
84                         String longitude = access.getProperty(Config.CADI_LONGITUDE, null);
85                         if(longitude==null) {
86                                 longitude = access.getProperty("AFT_LONGITUDE", null);
87                         }
88                         if(latitude==null || longitude==null) {
89                                 throw new CadiException(Config.CADI_LATITUDE + " and " + Config.CADI_LONGITUDE + " is required");
90                         } else {
91                                 mep.setLatitude(Float.parseFloat(latitude));
92                                 mep.setLongitude(Float.parseFloat(longitude));
93                         }
94                         String split[] = Split.split('.', version);
95                         mep.setPkg(split.length>3?Integer.parseInt(split[3]):0);
96                         mep.setPatch(split.length>2?Integer.parseInt(split[2]):0);
97                         mep.setMinor(split.length>1?Integer.parseInt(split[1]):0);
98                         mep.setMajor(split.length>0?Integer.parseInt(split[0]):0);
99                         
100                         mep.setHostname(access.getProperty(Config.HOSTNAME, Inet4Address.getLocalHost().getHostName()));
101                         String subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
102                         if(subprotocols==null) {
103                                 mep.setProtocol("http");
104                         } else {
105                                 mep.setProtocol("https");
106                                 for(String s : Split.split(',', subprotocols)) {
107                                         mep.getSubprotocol().add(s);
108                                 }
109                         }
110                 } catch (NumberFormatException | UnknownHostException e) {
111                         throw new CadiException("Error extracting Data from Properties for Registrar",e);
112                 }
113                 meps = new MgmtEndpoints();
114                 meps.getMgmtEndpoint().add(mep);
115                 for(RemoteRegistrant<ENV> rr : others) {
116                         meps.getMgmtEndpoint().add(rr.mep);
117                 }
118         }
119         
120         @Override
121         public Result<Void> update(ENV env) {
122                 try {
123                         Rcli<?> client = aafcon.client(locator);
124                         try {
125                                 Future<MgmtEndpoints> fup = client.update("/registration",mgmtEndpointsDF,meps);
126                                 if(fup.get(timeout)) {
127                                         access.log(Level.INFO, "Registration complete to",client.getURI());
128                                         return Result.ok(fup.code(),null);
129                                 } else {
130                                         access.log(Level.ERROR,"Error registering to AAF Locator on ", client.getURI());
131                                         return Result.err(fup.code(),fup.body());
132                                 }
133                         } catch (APIException e) {
134                                 access.log(e, "Error registering service to AAF Locator");
135                                 return Result.err(503,e.getMessage());
136                         }
137                         
138                 } catch (CadiException e) {
139                         return Result.err(503,e.getMessage());
140                 }
141         }
142
143         @Override
144         public Result<Void> cancel(ENV env) {
145                 try {
146                         Rcli<?> client = aafcon.client(locator);
147                         try {
148                                 Future<MgmtEndpoints> fup = client.delete("/registration",mgmtEndpointsDF,meps);
149                                 if(fup.get(timeout)) {
150                                         access.log(Level.INFO, "Deregistration complete on",client.getURI());
151                                         return Result.ok(fup.code(),null);
152                                 } else {
153                                         return Result.err(fup.code(),fup.body());
154                                 }
155                         } catch (APIException e) {
156                                 access.log(e, "Error deregistering service on AAF Locator");
157                                 return Result.err(503,e.getMessage());
158                         }
159                         
160                 } catch (CadiException e) {
161                         return Result.err(503,e.getMessage());
162                 }
163         }
164
165 }