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