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====================================================
21 package org.onap.aaf.auth.batch.helpers;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.onap.aaf.auth.batch.actions.ApprovalAdd;
28 import org.onap.aaf.auth.batch.actions.FutureAdd;
29 import org.onap.aaf.auth.batch.approvalsets.DataView;
30 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
31 import org.onap.aaf.auth.dao.cass.FutureDAO;
32 import org.onap.aaf.auth.dao.cass.NsDAO;
33 import org.onap.aaf.auth.dao.cass.RoleDAO;
34 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
35 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.layer.Result;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.Trans;
42 import com.datastax.driver.core.Cluster;
43 import com.datastax.driver.core.Session;
45 public class BatchDataView implements DataView {
46 private FutureAdd futureAdd;
47 private ApprovalAdd approvalAdd;
49 public BatchDataView(final AuthzTrans trans, final Cluster cluster, final boolean dryRun ) throws APIException, IOException {
50 futureAdd = new FutureAdd(trans, cluster, dryRun);
51 approvalAdd = new ApprovalAdd(trans, futureAdd);
54 public Session getSession(AuthzTrans trans) throws APIException, IOException {
55 TimeTaken tt = trans.start("Get Session", Trans.SUB);
57 return futureAdd.getSession(trans);
63 public Result<NsDAO.Data> ns(AuthzTrans trans, String id) {
65 TimeTaken tt = trans.start("Get NS by ID %s", Trans.SUB, id);
72 if(n==null || n.ndd==null) {
73 return Result.err(Result.ERR_Backend,"Namespace '%s' does not exist", id);
75 return Result.ok(n.ndd);
80 public Result<RoleDAO.Data> roleByName(AuthzTrans trans, String name) {
81 Role r = Role.byName.get(name);
82 if(r==null || r.rdd==null) {
83 return Result.err(Result.ERR_Backend,"Role '%s' does not exist", name);
85 return Result.ok(r.rdd);
89 public Result<List<UserRoleDAO.Data>> ursByRole(AuthzTrans trans, String role) {
90 List<UserRole> urs = UserRole.getByRole().get(role);
92 return Result.err(Result.ERR_Backend, "UserRoles for Role '%s' does not exist", role);
97 private Result<List<Data>> toLURDD(List<UserRole> urs) {
98 List<UserRoleDAO.Data> rv = new ArrayList<>();
100 for(UserRole ur : urs) {
104 return Result.ok(rv);
108 public Result<List<UserRoleDAO.Data>> ursByUser(AuthzTrans trans, String user) {
109 List<UserRole> urs = UserRole.getByUser().get(user);
111 return Result.err(Result.ERR_Backend, "UserRoles for User '%s' does not exist", user);
117 public Result<FutureDAO.Data> write(AuthzTrans trans, FutureDAO.Data fdd) {
118 return futureAdd.exec(trans, fdd, null);
122 public Result<ApprovalDAO.Data> write(AuthzTrans trans, ApprovalDAO.Data add) {
123 return approvalAdd.exec(trans, add, null);