2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.actions;
24 import java.io.IOException;
25 import java.util.List;
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;
37 import com.datastax.driver.core.Cluster;
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);
44 public RoleModify(AuthzTrans trans, ActionDAO<?,?,?> adao) {
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);
52 if (rr.isOKhasData()) {
53 return Result.ok(rr.value.get(0));
55 return Result.err(Result.ERR_NotFound, "Data not Found " + r.toString());
58 Result<Data> rv = null;
59 if (rr.isOKhasData()) {
60 for (final Data d : rr.value) {
62 if (d.ns.equals(r.ns) && d.name.equals(r.name)) {
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);
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);
83 for (String p : d.perms) {
84 Perm perm = Perm.keys.get(p);
86 if (perm.roles.contains(r.encode())) {
87 modify.permModify().exec(trans, perm, new PermModify.Modify() {
89 public RoleModify roleModify() {
90 return RoleModify.this;
94 public void change(PermDAO.Data pdd) {
95 pdd.roles.remove(r.encode());
96 pdd.roles.add(d.encode());
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);
113 rv = q.roleDAO.create(trans, d);
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();
120 q.roleDAO.delete(trans, rmme, false);
123 trans.info().log(rv.errorString());
131 rv = Result.err(Status.ERR_General,"Never get to this code");
137 public static interface Modify {
138 void change(RoleDAO.Data ur);
139 PermModify permModify();
142 public Result<Void> delete(AuthzTrans trans, Role r) {
146 RoleDAO.Data data = new RoleDAO.Data();
149 return q.roleDAO.delete(trans,data,false);