Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / actions / URModify.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.List;
26
27 import org.onap.aaf.auth.batch.helpers.UserRole;
28 import org.onap.aaf.auth.dao.cass.Status;
29 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
30 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.layer.Result;
33 import org.onap.aaf.misc.env.APIException;
34
35 import com.datastax.driver.core.Cluster;
36
37 public class URModify extends ActionDAO<UserRole,Void,URModify.Modify> {
38     public URModify(AuthzTrans trans, Cluster cluster, boolean dryRun) throws APIException, IOException {
39         super(trans, cluster,dryRun);
40     }
41     
42     public URModify(AuthzTrans trans, ActionDAO<?,?,?> adao) {
43         super(trans, adao);
44     }
45
46     @Override
47     public Result<Void> exec(AuthzTrans trans, UserRole ur,Modify modify) {
48         if (dryRun) {
49             trans.info().printf("Would Update %s %s", ur.user(), ur.role());
50             return Result.ok();
51         } else {
52             Result<List<Data>> rr = q.userRoleDAO.read(trans, ur.user(),ur.role());
53             if (rr.notOKorIsEmpty()) {
54                 return Result.err(rr);
55             }
56             for (Data d : rr.value) {
57                 modify.change(d);
58                 if (!(ur.expires().equals(d.expires))) {
59                     ur.expires(d.expires);
60                 }
61                 if (ur.user().equals(d.user) && ur.role().equals(d.role)){
62                     Result<Void> rv = q.userRoleDAO.update(trans, d);
63                     if (rv.isOK()) {
64                         trans.info().printf("Updated %s %s to %s", ur.user(), ur.role(), d.toString());
65                     } else {
66                         trans.info().log(rv.errorString());
67                     }
68                 } else {
69                     return Result.err(Status.ERR_Denied, "You cannot change the key of this Data");
70                 }
71             }
72             return Result.err(Status.ERR_UserRoleNotFound,"No User Role with %s %s",ur.user(),ur.role());
73         }
74     }
75     
76     public static interface Modify {
77         void change(UserRoleDAO.Data ur);
78     }
79     
80 }