b90c357922d57983107abb0282a3ef0bf6d1f543
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cached / CachedRoleDAO.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.dao.cached;
23
24 import java.util.List;
25
26 import org.onap.aaf.auth.dao.CIDAO;
27 import org.onap.aaf.auth.dao.CachedDAO;
28 import org.onap.aaf.auth.dao.cass.PermDAO;
29 import org.onap.aaf.auth.dao.cass.RoleDAO;
30 import org.onap.aaf.auth.dao.cass.Status;
31 import org.onap.aaf.auth.dao.cass.RoleDAO.Data;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.layer.Result;
34
35 public class CachedRoleDAO extends CachedDAO<AuthzTrans,RoleDAO, RoleDAO.Data> {
36     public CachedRoleDAO(RoleDAO dao, CIDAO<AuthzTrans> info, long expiresIn) {
37         super(dao, info, RoleDAO.CACHE_SEG, expiresIn);
38     }
39
40     public Result<List<Data>> readNS(AuthzTrans trans, final String ns) {
41         DAOGetter getter = new DAOGetter(trans,dao()) {
42             public Result<List<Data>> call() {
43                 return dao.readNS(trans, ns);
44             }
45         };
46
47         Result<List<Data>> lurd = get(trans, ns, getter);
48         if (lurd.isOK() && lurd.isEmpty()) {
49             return Result.err(Status.ERR_RoleNotFound,"No Role found");
50         }
51         return lurd;
52     }
53
54     public Result<List<Data>> readName(AuthzTrans trans, final String name) {
55         DAOGetter getter = new DAOGetter(trans,dao()) {
56             public Result<List<Data>> call() {
57                 return dao().readName(trans, name);
58             }
59         };
60
61         Result<List<Data>> lurd = get(trans, name, getter);
62         if (lurd.isOK() && lurd.isEmpty()) {
63             return Result.err(Status.ERR_RoleNotFound,"No Role found");
64         }
65         return lurd;
66     }
67
68     public Result<List<Data>> readChildren(AuthzTrans trans, final String ns, final String name) {
69         // At this point, I'm thinking it's better not to try to cache "*" results
70         // Data probably won't be accurate, and adding it makes every update invalidate most of the cache
71         // Jonathan 2/4/2014
72         return dao().readChildren(trans,ns,name);
73     }
74
75     public Result<Void> addPerm(AuthzTrans trans, RoleDAO.Data rd, PermDAO.Data perm) {
76         Result<Void> rv = dao().addPerm(trans,rd,perm);
77         if (trans.debug().isLoggable())
78             trans.debug().log("Adding",perm,"to", rd, "with CachedRoleDAO.addPerm");
79         invalidate(trans, rd);
80         return rv;
81     }
82
83     public Result<Void> delPerm(AuthzTrans trans, RoleDAO.Data rd, PermDAO.Data perm) {
84         Result<Void> rv = dao().delPerm(trans,rd,perm);
85         if (trans.debug().isLoggable())
86             trans.debug().log("Removing",perm,"from", rd, "with CachedRoleDAO.addPerm");
87         invalidate(trans, rd);
88         return rv;
89     }
90
91     /**
92      * Add description to this role
93      *
94      * @param trans
95      * @param ns
96      * @param name
97      * @param description
98      * @return
99      */
100     public Result<Void> addDescription(AuthzTrans trans, String ns, String name, String description) {
101         //TODO Invalidate?
102         return dao().addDescription(trans, ns, name, description);
103
104     }
105
106 }