Merge "Fix sonar issues in Mapper1_0"
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / actions / URFutureApproveExec.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.actions;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
29 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
30 import org.onap.aaf.auth.dao.cass.ApprovalDAO.Data;
31 import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP;
32 import org.onap.aaf.auth.dao.hl.Function.Lookup;
33 import org.onap.aaf.auth.dao.hl.Function.OP_STATUS;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.helpers.Approval;
36 import org.onap.aaf.auth.helpers.Future;
37 import org.onap.aaf.auth.helpers.UserRole;
38 import org.onap.aaf.auth.layer.Result;
39 import org.onap.aaf.misc.env.APIException;
40
41 import com.datastax.driver.core.Cluster;
42
43 public class URFutureApproveExec extends ActionDAO<List<Approval>, OP_STATUS, Future> {
44
45         public URFutureApproveExec(AuthzTrans trans, Cluster cluster, boolean dryRun) throws APIException, IOException {
46                 super(trans,cluster, dryRun);
47         }
48         
49         public URFutureApproveExec(AuthzTrans trans, ActionDAO<?,?,?> adao) {
50                 super(trans, adao);
51         }
52
53         @Override
54         public Result<OP_STATUS> exec(AuthzTrans trans, List<Approval> app, Future future) {
55                 if(dryRun) {
56                         return Result.err(Result.ERR_ActionNotCompleted,"Not Executed");
57                 } else {
58                         // Save on Lookups
59                         final List<ApprovalDAO.Data> apprs = new ArrayList<>();
60                         final List<UserRoleDAO.Data> urs = new ArrayList<>();
61                         for(Approval a : app) {
62                                 apprs.add(a.add);
63                                 UserRole ur = UserRole.get(a.add.user, future.role);
64                                 if(ur!=null) {
65                                         urs.add(ur.urdd());
66                                 }
67                         }
68                         Result<OP_STATUS> rv = f.performFutureOp(trans, FUTURE_OP.A, future.fdd,
69                                 new Lookup<List<ApprovalDAO.Data>>() {
70                                         @Override
71                                         public List<Data> get(AuthzTrans trans, Object ... noop) {
72                                                 return apprs;
73                                         }
74                                 },
75                                 new Lookup<UserRoleDAO.Data>() {
76                                         @Override
77                                         public UserRoleDAO.Data get(AuthzTrans trans, Object ... keys) {
78                                                 List<UserRole> lur = UserRole.byUser.get(keys[0]);
79                                                 if(lur!=null) {
80                                                         for(UserRole ur : lur) {
81                                                                 if(ur.role().equals(keys[1])) {
82                                                                         return ur.urdd();
83                                                                 }
84                                                         }
85                                                 }
86                                                 return null;
87                                         }
88                                 });
89                         if(rv.isOK()) {
90                                 switch(rv.value) {
91                                         case D:
92                                                 trans.info().printf("Denied %s on %s", future.memo(),future.fdd.target);
93                                                 break;
94                                         case E:
95                                                 trans.info().printf("Completed %s on %s", future.memo(),future.fdd.target);
96                                                 break;
97                                         case L:
98                                                 trans.info().printf("Future %s on %s has lapsed", future.memo(),future.fdd.target);
99                                                 break;
100                                         default:
101                                 }
102                         } else {
103                                 trans.error().log("Error completing",future.memo(),rv.errorString());
104                         }
105                         return rv;
106                 }
107         }
108 }