Public and Private Locate entries
[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.util.ArrayList;
25 import java.util.List;
26
27 import org.onap.aaf.auth.dao.cass.LocateDAO;
28 import org.onap.aaf.auth.env.AuthzEnv;
29 import org.onap.aaf.auth.env.AuthzTrans;
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.register.Registrant;
34 import org.onap.aaf.cadi.register.RegistrationCreator;
35
36 import locate.v1_0.MgmtEndpoint;
37 import locate.v1_0.MgmtEndpoints;
38
39 public class DirectRegistrar implements Registrant<AuthzEnv> {
40
41     private LocateDAO ldao;
42     private List<LocateDAO.Data> ldd; 
43     public DirectRegistrar(Access access, LocateDAO ldao, int port) throws CadiException {
44         this.ldao = ldao;
45         ldd = new ArrayList<>();
46         RegistrationCreator rc = new RegistrationCreator(access);
47         MgmtEndpoints mes = rc.create(port);
48         for(MgmtEndpoint me : mes.getMgmtEndpoint()) {
49                 ldd.add(convert(me));
50         }
51     }
52     
53     private LocateDAO.Data convert(MgmtEndpoint me) {
54         LocateDAO.Data out = new LocateDAO.Data();
55         out.name=me.getName();
56                 out.hostname=me.getHostname();
57                 out.latitude=me.getLatitude();
58                 out.longitude=me.getLongitude();
59                 out.major=me.getMajor();
60                 out.minor=me.getMinor();
61                 out.pkg=me.getPkg();
62                 out.patch=me.getPatch();
63                 out.port=me.getPort();
64                 out.protocol=me.getProtocol();
65                 out.subprotocol(true).addAll(me.getSubprotocol());
66 //              out.port_key = UUID.randomUUID();
67                 return out;
68         }
69
70         @Override
71
72     public Result<Void> update(AuthzEnv env) {
73         AuthzTrans trans = env.newTransNoAvg(); 
74         StringBuilder sb = null;
75         for(LocateDAO.Data ld : ldd) {
76                 org.onap.aaf.auth.layer.Result<Void> dr = ldao.update(trans, ld);
77                 if (dr.notOK()) {
78                         if(sb == null) {
79                                 sb = new StringBuilder(dr.errorString());
80                         } else {
81                                 sb.append(';');
82                                 sb.append(dr.errorString());
83                         }
84                 }
85         }
86         
87         if(sb==null) {
88                 return Result.ok(200, null);
89         } else {
90                 return Result.err(503, sb.toString());
91         }
92     }
93
94     /* (non-Javadoc)
95      * @see org.onap.aaf.auth.server.Registrant#cancel(org.onap.aaf.auth.env.test.AuthzEnv)
96      */
97     @Override
98     public Result<Void> cancel(AuthzEnv env) {
99         AuthzTrans trans = env.newTransNoAvg(); 
100         StringBuilder sb = null;
101         for(LocateDAO.Data ld : ldd) {
102             org.onap.aaf.auth.layer.Result<Void> dr = ldao.delete(trans, ld, false);
103                 if (dr.notOK()) {
104                         if(sb == null) {
105                                 sb = new StringBuilder(dr.errorString());
106                         } else {
107                                 sb.append(';');
108                                 sb.append(dr.errorString());
109                         }
110                 }
111         }
112         
113         if(sb==null) {
114                 return Result.ok(200, null);
115         } else {
116                 return Result.err(503, sb.toString());
117         }
118     }
119
120 }