AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cached / CachedPermDAO.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.PermDAO.Data;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.layer.Result;
34
35 public class CachedPermDAO extends CachedDAO<AuthzTrans,PermDAO, PermDAO.Data> {
36
37         public CachedPermDAO(PermDAO dao, CIDAO<AuthzTrans> info, long expiresIn) {
38                 super(dao, info, PermDAO.CACHE_SEG, expiresIn);
39         }
40
41         public Result<List<Data>> readNS(AuthzTrans trans, final String ns) {
42                 DAOGetter getter = new DAOGetter(trans,dao()) {
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.isOKhasData()) {
50                         return lurd;
51                 } else {
52                         
53                 }
54 //              if(getter.result==null) {
55 //                      if(lurd==null) {
56                                 return Result.err(Status.ERR_PermissionNotFound,"No Permission found - " + lurd.details);
57 //                      } else {
58 //                              return Result.ok(lurd);
59 //                      }
60 //              }
61 //              return getter.result;
62         }
63
64         public Result<List<Data>> readChildren(AuthzTrans trans, final String ns, final String type) {
65                 return dao().readChildren(trans,ns,type);
66         }
67
68         /**
69          * 
70          * @param trans
71          * @param ns
72          * @param type
73          * @return
74          */
75         public Result<List<Data>> readByType(AuthzTrans trans, final String ns, final String type) {
76                 DAOGetter getter = new DAOGetter(trans,dao()) {
77                         public Result<List<Data>> call() {
78                                 return dao.readByType(trans, ns, type);
79                         }
80                 };
81                 
82                 // Note: Can reuse index1 here, because there is no name collision versus response
83                 Result<List<Data>> lurd = get(trans, ns+'|'+type, getter);
84                 if(lurd.isOK() && lurd.isEmpty()) {
85                         return Result.err(Status.ERR_PermissionNotFound,"No Permission found");
86                 }
87                 return lurd;
88         }
89         
90         /**
91          * Add desciption to this permission
92          * 
93          * @param trans
94          * @param ns
95          * @param type
96          * @param instance
97          * @param action
98          * @param description
99          * @return
100          */
101         public Result<Void> addDescription(AuthzTrans trans, String ns, String type, 
102                         String instance, String action, String description) {
103                 //TODO Invalidate?
104                 return dao().addDescription(trans, ns, type, instance, action, description);
105         }
106         
107         public Result<Void> addRole(AuthzTrans trans, PermDAO.Data perm, RoleDAO.Data role) {
108                 Result<Void> rv = dao().addRole(trans,perm,role.encode());
109                 if(trans.debug().isLoggable())
110                         trans.debug().log("Adding",role.encode(),"to", perm, "with CachedPermDAO.addRole");
111                 invalidate(trans,perm);
112                 return rv;
113         }
114
115         public Result<Void> delRole(AuthzTrans trans, Data perm, RoleDAO.Data role) {
116                 Result<Void> rv = dao().delRole(trans,perm,role.encode());
117                 if(trans.debug().isLoggable())
118                         trans.debug().log("Removing",role.encode(),"from", perm, "with CachedPermDAO.delRole");
119                 invalidate(trans,perm);
120                 return rv;
121         }
122
123
124 }