AT&T 2.0.19 Code drop, stage 3
[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                         
67                         locate.hostname = access.getProperty(Config.HOSTNAME, Inet4Address.getLocalHost().getHostName());
68                         String subprotocols = access.getProperty(Config.CADI_PROTOCOLS, null);
69                         if(subprotocols==null) {
70                                 locate.protocol="http";
71                         } else {
72                                 locate.protocol="https";
73                                 for(String s : Split.split(',', subprotocols)) {
74                                         locate.subprotocol(true).add(s);
75                                 }
76                         }
77                 } catch (NumberFormatException | UnknownHostException e) {
78                         throw new CadiException("Error extracting Data from Properties for Registrar",e);
79                 }
80         }
81         
82         @Override
83         public Result<Void> update(AuthzEnv env) {
84                 org.onap.aaf.auth.layer.Result<Void> dr = ldao.update(env.newTransNoAvg(), locate);
85                 if(dr.isOK()) {
86                         return Result.ok(200, null);
87                 } else {
88                         return Result.err(503, dr.errorString());
89                 }
90         }
91
92         /* (non-Javadoc)
93          * @see org.onap.aaf.auth.server.Registrant#cancel(org.onap.aaf.auth.env.test.AuthzEnv)
94          */
95         @Override
96         public Result<Void> cancel(AuthzEnv env) {
97                 org.onap.aaf.auth.layer.Result<Void> dr = ldao.delete(env.newTransNoAvg(), locate, false);
98                 if(dr.isOK()) {
99                         return Result.ok(200, null);
100                 } else {
101                         return Result.err(503, dr.errorString());
102                 }
103
104         }
105
106 }