Update Fixes from testing
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cached / CachedCredDAO.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.CredDAO;
29 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
30 import org.onap.aaf.auth.dao.cass.Status;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.layer.Result;
33
34 public class CachedCredDAO extends CachedDAO<AuthzTrans, CredDAO, CredDAO.Data> {
35     private final ReadID readID;
36     private final ReadID readIDBath;
37     
38     public CachedCredDAO(CredDAO dao, CIDAO<AuthzTrans> info, long expiresIn) {
39         super(dao, info, CredDAO.CACHE_SEG, expiresIn);
40         if(FileGetter.isLoaded) {
41             readID = new ReadID() {
42                 @Override
43                 public Result<List<Data>> read(AuthzTrans trans, final String id) {
44                     return FileGetter.singleton(null).getter(id).get();
45                 }
46             };
47             // Both are the same... File read in only does BAth
48             readIDBath = readID;
49         } else {
50             readID = new ReadID() {
51                 @Override
52                 public Result<List<Data>> read(AuthzTrans trans, final String id) {
53                     DAOGetter getter = new DAOGetter(trans,dao()) {
54                         public Result<List<CredDAO.Data>> call() {
55                             return dao().readID(trans, id);
56                         }
57                     };
58                     
59                     Result<List<CredDAO.Data>> lurd = get(trans, id, getter);
60                     if (lurd.isOK() && lurd.isEmpty()) {
61                         return Result.err(Status.ERR_UserNotFound,"No User Cred found");
62                     }
63                     return lurd;
64                 }
65             };
66             
67             readIDBath = new ReadID() {
68                 @Override
69                 public Result<List<Data>> read(AuthzTrans trans, final String id) {
70                      DAOGetter getter = new DAOGetter(trans,dao()) {
71                          public Result<List<CredDAO.Data>> call() {
72                              return dao().readIDBAth(trans, id);
73                          }
74                      };
75                      
76                      Result<List<CredDAO.Data>> lurd = get(trans, id, getter);
77                      if (lurd.isOK() && lurd.isEmpty()) {
78                          return Result.err(Status.ERR_UserNotFound,"No User Cred found");
79                      }
80                      return lurd;
81                 }
82             };
83         }
84     }
85     
86     /**
87      * Pass through Cred Lookup
88      * 
89      * Unlike Role and Perm, we don't need or want to cache these elements... Only used for NS Delete.
90      * 
91      * @param trans
92      * @param ns
93      * @return
94      */
95     public Result<List<CredDAO.Data>> readNS(AuthzTrans trans, final String ns) {
96         
97         return dao().readNS(trans, ns);
98     }
99
100     public Result<List<CredDAO.Data>> readID(AuthzTrans trans, final String id) {
101         return readID.read(trans, id);
102     }
103
104     public Result<List<Data>> readIDBAth(AuthzTrans trans, String id) {
105         return readIDBath.read(trans,id);
106     }
107
108     private interface ReadID {
109         public Result<List<CredDAO.Data>> read(final AuthzTrans trans, final String id);
110     }
111 }