AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / 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.actions;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.onap.aaf.auth.dao.cass.PermDAO;
28 import org.onap.aaf.auth.dao.cass.RoleDAO;
29 import org.onap.aaf.auth.dao.cass.Status;
30 import org.onap.aaf.auth.dao.cass.RoleDAO.Data;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.helpers.Perm;
33 import org.onap.aaf.auth.helpers.Role;
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.ns,r.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.ns) && d.name.equals(r.name)) {
63                                                 // update for fields
64                                                 // In either case, adjust Roles
65                                                 for(String p : d.perms) {
66                                                         if(!r.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.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.ns, r.name, d.ns, d.name);
117                                                         RoleDAO.Data rmme = new RoleDAO.Data();
118                                                         rmme.ns=r.ns;
119                                                         rmme.name=r.name;
120                                                         q.roleDAO.delete(trans, rmme, false);
121                                                         
122                                                 } else {
123                                                         trans.info().log(rv.errorString());
124                                                 }
125                                         }
126                                 }
127                         } else {
128                                 rv = Result.err(rr);
129                         }
130                         if(rv==null) {
131                                 rv = Result.err(Status.ERR_General,"Never get to this code");
132                         }
133                         return rv;
134                 }
135         }
136         
137         public static interface Modify {
138                 void change(RoleDAO.Data ur);
139                 PermModify permModify();
140         }
141         
142         public Result<Void> delete(AuthzTrans trans, Role r) {
143                 if(dryRun) {
144                         return Result.ok();
145                 } else {
146                         RoleDAO.Data data = new RoleDAO.Data();
147                         data.ns=r.ns;
148                         data.name = r.name;
149                         return q.roleDAO.delete(trans,data,false);
150                 }
151         }
152 }