Approval Batch, prep better JUnit
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / BatchDataView.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 package org.onap.aaf.auth.batch.helpers;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.onap.aaf.auth.batch.actions.ApprovalAdd;
28 import org.onap.aaf.auth.batch.actions.FutureAdd;
29 import org.onap.aaf.auth.batch.approvalsets.DataView;
30 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
31 import org.onap.aaf.auth.dao.cass.FutureDAO;
32 import org.onap.aaf.auth.dao.cass.NsDAO;
33 import org.onap.aaf.auth.dao.cass.RoleDAO;
34 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
35 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.layer.Result;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.Trans;
41
42 import com.datastax.driver.core.Cluster;
43 import com.datastax.driver.core.Session;
44
45 public class BatchDataView implements DataView {
46         private FutureAdd futureAdd;
47         private ApprovalAdd approvalAdd;
48
49         public BatchDataView(final AuthzTrans trans, final Cluster cluster, final boolean dryRun ) throws APIException, IOException {
50                 futureAdd = new FutureAdd(trans, cluster, dryRun);
51                 approvalAdd = new ApprovalAdd(trans, futureAdd);
52         }
53
54         public Session getSession(AuthzTrans trans) throws APIException, IOException {
55                 TimeTaken tt = trans.start("Get Session", Trans.SUB);
56                 try {
57                         return futureAdd.getSession(trans);
58                 } finally {
59                         tt.done();
60                 }
61         }
62         
63         public Result<NsDAO.Data> ns(AuthzTrans trans, String id) {
64                 NS n;
65                 TimeTaken tt = trans.start("Get NS by ID %s", Trans.SUB, id);
66                 try {
67                         n=NS.data.get(id);
68                 } finally {
69                         tt.done();
70                 }
71                 
72                 if(n==null || n.ndd==null) {
73                         return Result.err(Result.ERR_Backend,"Namespace '%s' does not exist", id);
74                 }
75                 return Result.ok(n.ndd);
76         }
77
78         
79         @Override
80         public Result<RoleDAO.Data> roleByName(AuthzTrans trans, String name) {
81                 Role r = Role.byName.get(name);
82                 if(r==null || r.rdd==null) {
83                         return Result.err(Result.ERR_Backend,"Role '%s' does not exist", name);
84                 }
85                 return Result.ok(r.rdd);
86         }
87
88         @Override
89         public Result<List<UserRoleDAO.Data>> ursByRole(AuthzTrans trans, String role) {
90                 List<UserRole> urs = UserRole.getByRole().get(role);
91                 if(urs==null) {
92                         return Result.err(Result.ERR_Backend, "UserRoles for Role '%s' does not exist", role);
93                 }
94                 return toLURDD(urs);
95         }
96
97         private Result<List<Data>> toLURDD(List<UserRole> urs) {
98                 List<UserRoleDAO.Data> rv = new ArrayList<>();
99                 if(urs!=null) {
100                         for(UserRole ur : urs) {
101                                 rv.add(ur.urdd());
102                         }
103                 }
104                 return Result.ok(rv);
105         }
106
107         @Override
108         public Result<List<UserRoleDAO.Data>> ursByUser(AuthzTrans trans, String user) {
109                 List<UserRole> urs = UserRole.getByUser().get(user);
110                 if(urs==null) {
111                         return Result.err(Result.ERR_Backend, "UserRoles for User '%s' does not exist", user);
112                 }
113                 return toLURDD(urs);
114         }
115
116         @Override
117         public Result<FutureDAO.Data> write(AuthzTrans trans, FutureDAO.Data fdd) {
118                 return futureAdd.exec(trans, fdd, null);
119         }
120
121         @Override
122         public Result<ApprovalDAO.Data> write(AuthzTrans trans, ApprovalDAO.Data add) {
123                 return approvalAdd.exec(trans, add, null);
124         }
125
126 }