Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / actions / RoleModify.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.Perm;
28 import org.onap.aaf.auth.batch.helpers.Role;
29 import org.onap.aaf.auth.dao.cass.PermDAO;
30 import org.onap.aaf.auth.dao.cass.RoleDAO;
31 import org.onap.aaf.auth.dao.cass.Status;
32 import org.onap.aaf.auth.dao.cass.RoleDAO.Data;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.misc.env.APIException;
36
37 import com.datastax.driver.core.Cluster;
38
39 public class RoleModify extends ActionDAO<Role,RoleDAO.Data,RoleModify.Modify> {
40     public RoleModify(AuthzTrans trans, Cluster cluster, boolean dryRun) throws APIException, IOException {
41         super(trans, cluster, dryRun);
42     }
43     
44     public RoleModify(AuthzTrans trans, ActionDAO<?,?,?> adao) {
45         super(trans, adao);
46     }
47
48     @Override
49     public Result<RoleDAO.Data> exec(final AuthzTrans trans, final Role r,final RoleModify.Modify modify) {
50         Result<List<Data>> rr = q.roleDAO.read(trans, r.rdd.ns,r.rdd.name);
51         if (dryRun) {
52             if (rr.isOKhasData()) {
53                 return Result.ok(rr.value.get(0));
54             } else {
55                 return Result.err(Result.ERR_NotFound, "Data not Found " + r.toString());
56             }
57         } else {
58             Result<Data> rv = null;
59             if (rr.isOKhasData()) {
60                 for (final Data d : rr.value) {
61                     modify.change(d);
62                     if (d.ns.equals(r.rdd.ns) && d.name.equals(r.rdd.name)) {
63                         // update for fields
64                         // In either case, adjust Roles
65                         for (String p : d.perms) {
66                             if (!r.rdd.perms.contains(p)) {
67                                 Result<PermDAO.Data> rpdd = PermDAO.Data.decode(trans, q, p);
68                                 if (rpdd.isOKhasData()) {
69                                     q.roleDAO.dao().addPerm(trans, d, rpdd.value);
70                                 }
71                             }
72                         }
73                         for (String p : r.rdd.perms) {
74                             if (!d.perms.contains(p)) {
75                                 Result<PermDAO.Data> rpdd = PermDAO.Data.decode(trans, q, p);
76                                 if (rpdd.isOKhasData()) {
77                                     q.roleDAO.dao().delPerm(trans, d, rpdd.value);
78                                 }
79                             }
80                         }
81                         rv = Result.ok(d);
82                     } else {                
83                         for (String p : d.perms) {
84                             Perm perm = Perm.keys.get(p);
85                             if (perm!=null) {
86                                 if (perm.roles.contains(r.encode())) {
87                                     modify.permModify().exec(trans, perm, new PermModify.Modify() {
88                                         @Override
89                                         public RoleModify roleModify() {
90                                             return RoleModify.this;
91                                         }
92                                         
93                                         @Override
94                                         public void change(PermDAO.Data pdd) {
95                                             pdd.roles.remove(r.encode());
96                                             pdd.roles.add(d.encode());
97                                         }
98                                     });
99                                 }
100                             }
101                         }
102                         Result<List<Data>> preexist = q.roleDAO.read(trans, d);
103                         if (preexist.isOKhasData()) {
104                             Data rdd = preexist.value.get(0);
105                             for (String p : d.perms) {
106                                 Result<PermDAO.Data> perm = PermDAO.Data.decode(trans, q, p);
107                                 if (perm.isOKhasData()) {
108                                     q.roleDAO.dao().addPerm(trans,rdd, perm.value);
109                                 }
110                             }
111                             rv = Result.ok(rdd);
112                         } else {
113                             rv = q.roleDAO.create(trans, d);
114                         }
115                         if (rv.isOK()) {
116                             trans.info().printf("Updating %s|%s to %s|%s", r.rdd.ns, r.rdd.name, d.ns, d.name);
117                             q.roleDAO.delete(trans, r.rdd, false);
118                             
119                         } else {
120                             trans.info().log(rv.errorString());
121                         }
122                     }
123                 }
124             } else {
125                 rv = Result.err(rr);
126             }
127             if (rv==null) {
128                 rv = Result.err(Status.ERR_General,"Never get to this code");
129             }
130             return rv;
131         }
132     }
133     
134     public static interface Modify {
135         void change(RoleDAO.Data ur);
136         PermModify permModify();
137     }
138     
139     public Result<Void> delete(AuthzTrans trans, Role r) {
140         if (dryRun) {
141             return Result.ok();
142         } else {
143             return q.roleDAO.delete(trans,r.rdd,false);
144         }
145     }
146 }