SonarFix-CachedRoleDAO.java
[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             @Override
43             public Result<List<Data>> call() {
44                 return dao.readNS(trans, ns);
45             }
46         };
47
48         Result<List<Data>> lurd = get(trans, ns, getter);
49         if (lurd.isOK() && lurd.isEmpty()) {
50             return Result.err(Status.ERR_RoleNotFound,"No Role found");
51         }
52         return lurd;
53     }
54
55     public Result<List<Data>> readName(AuthzTrans trans, final String name) {
56         DAOGetter getter = new DAOGetter(trans,dao()) {
57             @Override
58             public Result<List<Data>> call() {
59                 return dao().readName(trans, name);
60             }
61         };
62
63         Result<List<Data>> lurd = get(trans, name, getter);
64         if (lurd.isOK() && lurd.isEmpty()) {
65             return Result.err(Status.ERR_RoleNotFound,"No Role found");
66         }
67         return lurd;
68     }
69
70     public Result<List<Data>> readChildren(AuthzTrans trans, final String ns, final String name) {
71         // At this point, I'm thinking it's better not to try to cache "*" results
72         // Data probably won't be accurate, and adding it makes every update invalidate most of the cache
73         // Jonathan 2/4/2014
74         return dao().readChildren(trans,ns,name);
75     }
76
77     public Result<Void> addPerm(AuthzTrans trans, RoleDAO.Data rd, PermDAO.Data perm) {
78         Result<Void> rv = dao().addPerm(trans,rd,perm);
79         if (trans.debug().isLoggable())
80             trans.debug().log("Adding",perm,"to", rd, "with CachedRoleDAO.addPerm");
81         invalidate(trans, rd);
82         return rv;
83     }
84
85     public Result<Void> delPerm(AuthzTrans trans, RoleDAO.Data rd, PermDAO.Data perm) {
86         Result<Void> rv = dao().delPerm(trans,rd,perm);
87         if (trans.debug().isLoggable())
88             trans.debug().log("Removing",perm,"from", rd, "with CachedRoleDAO.addPerm");
89         invalidate(trans, rd);
90         return rv;
91     }
92
93     /**
94      * Add description to this role
95      *
96      * @param trans
97      * @param ns
98      * @param name
99      * @param description
100      * @return
101      */
102     public Result<Void> addDescription(AuthzTrans trans, String ns, String name, String description) {
103         //TODO Invalidate?
104         return dao().addDescription(trans, ns, name, description);
105
106     }
107
108 }