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