9eea77e3f7436ae5d5969f88be8fa37ab07f328d
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / direct / DirectRegistrar.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.auth.direct;
23
24 import java.net.Inet4Address;
25 import java.net.UnknownHostException;
26
27 import org.onap.aaf.auth.dao.cass.LocateDAO;
28 import org.onap.aaf.auth.dao.cass.LocateDAO.Data;
29 import org.onap.aaf.auth.env.AuthzEnv;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.client.Result;
33 import org.onap.aaf.cadi.config.Config;
34 import org.onap.aaf.cadi.register.Registrant;
35 import org.onap.aaf.cadi.util.Split;
36
37 public class DirectRegistrar implements Registrant<AuthzEnv> {
38     private Data locate;
39     private LocateDAO ldao;
40     public DirectRegistrar(Access access, LocateDAO ldao, String name, String version, int port) throws CadiException {
41         this.ldao = ldao;
42         locate = new LocateDAO.Data();
43         locate.name = name;
44         locate.port = port;
45         
46         try {
47             String latitude = access.getProperty(Config.CADI_LATITUDE, null);
48             if (latitude==null) {
49                 latitude = access.getProperty("AFT_LATITUDE", null);
50             }
51             String longitude = access.getProperty(Config.CADI_LONGITUDE, null);
52             if (longitude==null) {
53                 longitude = access.getProperty("AFT_LONGITUDE", null);
54             }
55             if (latitude==null || longitude==null) {
56                 throw new CadiException(Config.CADI_LATITUDE + " and " + Config.CADI_LONGITUDE + " is required");
57             } else {
58                 locate.latitude = Float.parseFloat(latitude);
59                 locate.longitude = Float.parseFloat(longitude);
60             }
61             String split[] = Split.splitTrim('.', version);
62             locate.pkg = split.length>3?Integer.parseInt(split[3]):0;
63             locate.patch = split.length>2?Integer.parseInt(split[2]):0;
64             locate.minor = split.length>1?Integer.parseInt(split[1]):0;
65             locate.major = split.length>0?Integer.parseInt(split[0]):0;
66             locate.hostname = access.getProperty(Config.AAF_REGISTER_AS, null);
67             if (locate.hostname==null) {
68                 locate.hostname = access.getProperty(Config.HOSTNAME, null);
69             }
70             if (locate.hostname==null) {
71                 locate.hostname = Inet4Address.getLocalHost().getHostName();
72             }
73             String subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
74             if (subprotocols==null) {
75                 locate.protocol="http";
76             } else {
77                 locate.protocol="https";
78                 for (String s : Split.split(',', subprotocols)) {
79                     locate.subprotocol(true).add(s);
80                 }
81             }
82         } catch (NumberFormatException | UnknownHostException e) {
83             throw new CadiException("Error extracting Data from Properties for Registrar",e);
84         }
85     }
86     
87     @Override
88     public Result<Void> update(AuthzEnv env) {
89         org.onap.aaf.auth.layer.Result<Void> dr = ldao.update(env.newTransNoAvg(), locate);
90         if (dr.isOK()) {
91             return Result.ok(200, null);
92         } else {
93             return Result.err(503, dr.errorString());
94         }
95     }
96
97     /* (non-Javadoc)
98      * @see org.onap.aaf.auth.server.Registrant#cancel(org.onap.aaf.auth.env.test.AuthzEnv)
99      */
100     @Override
101     public Result<Void> cancel(AuthzEnv env) {
102         org.onap.aaf.auth.layer.Result<Void> dr = ldao.delete(env.newTransNoAvg(), locate, false);
103         if (dr.isOK()) {
104             return Result.ok(200, null);
105         } else {
106             return Result.err(503, dr.errorString());
107         }
108
109     }
110
111 }