Sonar Fixes, Formatting
[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  * Modifications Copyright (C) 2019 IBM.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  *
21  */
22
23 package org.onap.aaf.auth.dao.cached;
24
25 import java.util.List;
26
27 import org.onap.aaf.auth.dao.CIDAO;
28 import org.onap.aaf.auth.dao.CachedDAO;
29 import org.onap.aaf.auth.dao.cass.CredDAO;
30 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
31 import org.onap.aaf.auth.dao.cass.Status;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.layer.Result;
34
35 public class CachedCredDAO extends CachedDAO<AuthzTrans, CredDAO, CredDAO.Data> {
36     private final ReadID readID;
37     private final ReadID readIDBath;
38
39     public CachedCredDAO(CredDAO dao, CIDAO<AuthzTrans> info, long expiresIn) {
40         super(dao, info, CredDAO.CACHE_SEG, expiresIn);
41         if(FileGetter.isLoaded) {
42             readID = new ReadID() {
43                 @Override
44                 public Result<List<Data>> read(AuthzTrans trans, final String id) {
45                     return FileGetter.singleton(null).getter(id).get();
46                 }
47             };
48             // Both are the same... File read in only does BAth
49             readIDBath = readID;
50         } else {
51             readID = new ReadID() {
52                 @Override
53                 public Result<List<Data>> read(AuthzTrans trans, final String id) {
54                     DAOGetter getter = new DAOGetter(trans,dao()) {
55                         @Override
56                         public Result<List<CredDAO.Data>> call() {
57                             return dao().readID(trans, id);
58                         }
59                     };
60
61                     Result<List<CredDAO.Data>> lurd = get(trans, id, getter);
62                     if (lurd.isOK() && lurd.isEmpty()) {
63                         return Result.err(Status.ERR_UserNotFound,"No User Cred found");
64                     }
65                     return lurd;
66                 }
67             };
68
69             readIDBath = new ReadID() {
70                 @Override
71                 public Result<List<Data>> read(AuthzTrans trans, final String id) {
72                      DAOGetter getter = new DAOGetter(trans,dao()) {
73                          @Override
74                          public Result<List<CredDAO.Data>> call() {
75                              return dao().readIDBAth(trans, id);
76                          }
77                      };
78
79                      Result<List<CredDAO.Data>> lurd = get(trans, id, getter);
80                      if (lurd.isOK() && lurd.isEmpty()) {
81                          return Result.err(Status.ERR_UserNotFound,"No User Cred found");
82                      }
83                      return lurd;
84                 }
85             };
86         }
87     }
88
89     /**
90      * Pass through Cred Lookup
91      *
92      * Unlike Role and Perm, we don't need or want to cache these elements... Only used for NS Delete.
93      *
94      * @param trans
95      * @param ns
96      * @return
97      */
98     public Result<List<CredDAO.Data>> readNS(AuthzTrans trans, final String ns) {
99
100         return dao().readNS(trans, ns);
101     }
102
103     public Result<List<CredDAO.Data>> readID(AuthzTrans trans, final String id) {
104         return readID.read(trans, id);
105     }
106
107     public Result<List<Data>> readIDBAth(AuthzTrans trans, String id) {
108         return readIDBath.read(trans,id);
109     }
110
111     @FunctionalInterface
112     private interface ReadID {
113         public Result<List<CredDAO.Data>> read(final AuthzTrans trans, final String id);
114     }
115 }