Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / actions / URFutureApprove.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.Date;
26 import java.util.GregorianCalendar;
27
28 import org.onap.aaf.auth.batch.helpers.Approval;
29 import org.onap.aaf.auth.batch.helpers.UserRole;
30 import org.onap.aaf.auth.dao.cass.FutureDAO;
31 import org.onap.aaf.auth.dao.cass.NsDAO;
32 import org.onap.aaf.auth.dao.hl.Function;
33 import org.onap.aaf.auth.dao.hl.Question;
34 import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.auth.org.Organization.Expiration;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.aaf.misc.env.util.Chrono;
40
41 import com.datastax.driver.core.Cluster;
42
43 public class URFutureApprove extends ActionDAO<UserRole, String,String> implements Action<UserRole,String,String>, Key<UserRole> {
44     private final Date start;
45     private final Date expires;
46
47     public URFutureApprove(AuthzTrans trans, Cluster cluster, boolean dryRun) throws APIException, IOException {
48         super(trans,cluster, dryRun);
49         GregorianCalendar gc = new GregorianCalendar();
50         start = gc.getTime();
51         expires = trans.org().expiration(gc, Expiration.Future).getTime();
52     }
53     
54     public URFutureApprove(AuthzTrans trans, ActionDAO<?,?,?> adao) {
55         super(trans, adao);
56         GregorianCalendar gc = new GregorianCalendar();
57         start = gc.getTime();
58         expires = trans.org().expiration(gc, Expiration.Future).getTime();
59     }
60
61     @Override
62     public Result<String> exec(AuthzTrans trans, UserRole ur,String text) {
63         if (dryRun) {
64             return Result.ok(text);
65         } else {
66             Result<NsDAO.Data> rns = q.deriveNs(trans, ur.ns());
67             if (rns.isOK()) {
68                 
69                 FutureDAO.Data data = new FutureDAO.Data();
70                 data.id=null; // let Create function assign UUID
71                 data.target=Function.FOP_USER_ROLE;
72                 
73                 data.memo = key(ur);
74                 data.start = start;
75                 data.expires = ur.expires();
76                 try {
77                     data.construct = ur.urdd().bytify();
78                 } catch (IOException e) {
79                     return Result.err(e);
80                 }
81                 Result<String> rfuture = f.createFuture(trans, data, Function.FOP_USER_ROLE, ur.user(), rns.value, FUTURE_OP.A);
82                 if (rfuture.isOK()) {
83                     trans.info().log(rfuture.value, text, ur.user(), data.memo);
84                 } else {
85                     trans.error().log(rfuture.details, text);
86                 }
87                 return rfuture;
88             } else {
89                 return Result.err(rns);
90             }
91         }
92     }
93     
94     @Override
95     public String key(UserRole ur) {
96         String expire;
97         if (expires.before(start)) {
98             expire = "' - EXPIRED ";
99         } else {
100             expire = "' - expiring ";
101         }
102         
103         if (Question.OWNER.equals(ur.rname())) {
104             return Approval.RE_VALIDATE_OWNER + ur.ns() + expire + Chrono.dateOnlyStamp(ur.expires());
105         } else if (Question.ADMIN.equals(ur.rname())) {
106             return Approval.RE_VALIDATE_ADMIN + ur.ns() + expire + Chrono.dateOnlyStamp(ur.expires());
107         } else {
108             return Approval.RE_APPROVAL_IN_ROLE + ur.role() + expire + Chrono.dateOnlyStamp(ur.expires());
109         }
110     }
111
112 }