5443dec27d622b8325c488f8f57b29280e08c119
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / lur / EpiLur.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.cadi.lur;
23
24 import java.security.Principal;
25 import java.util.List;
26
27 import org.onap.aaf.cadi.CachingLur;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.CredVal;
30 import org.onap.aaf.cadi.Lur;
31 import org.onap.aaf.cadi.Permission;
32
33 /**
34  * EpiLUR
35  * 
36  * Short for "Epic LUR". Be able to run through a series of LURs to obtain the validation needed.
37  * 
38  * The pun is better for the other pattern... "TAF" (aka EpiTaf), but it's still the larger picture of 
39  * LURs that will be accomplished.
40  * 
41  * FYI, the reason we separate LURs, rather than combine, is that Various User Repository Resources have
42  * different Caching requirements.  For instance, the Local User Repo (with stand alone names), never expire, but might be
43  * refreshed with a change in Configuration File, while the Remote Service based LURs will need to expire at prescribed intervals 
44  * 
45  * @author Jonathan
46  *
47  */
48 public final class EpiLur implements Lur {
49     private final Lur[] lurs;
50     
51     /**
52      * EpiLur constructor
53      * 
54      * Construct the EpiLur from variable TAF parameters
55      * @param lurs
56      * @throws CadiException
57      */
58     public EpiLur(Lur ... lurs) throws CadiException{
59         this.lurs = lurs;
60         if (lurs.length==0) throw new CadiException("Need at least one Lur implementation in constructor");
61     }
62
63     public boolean fish(Principal bait, Permission ... pond) {
64         if (pond==null) {
65             return false;
66         }
67         boolean rv = false;
68         Lur lur;
69         for (int i=0;!rv && i<lurs.length;++i) {
70             rv = (lur = lurs[i]).fish(bait, pond);
71             if (!rv && lur.handlesExclusively(pond)) break;
72         }
73         return rv;
74     }
75
76     public void fishAll(Principal bait, List<Permission> permissions) {
77         for (Lur lur : lurs) {
78             lur.fishAll(bait, permissions);
79         }
80     }
81
82     public void destroy() {
83         for (Lur lur : lurs) {
84             lur.destroy();
85         }
86     }
87
88     /**
89      * Return the first Lur (if any) which also implements UserPass 
90      * @return
91      */
92     public CredVal getUserPassImpl() {
93         for (Lur lur : lurs) {
94             if (lur instanceof CredVal) {
95                 return (CredVal)lur;
96             }
97         }
98         return null;
99     }
100
101     // Never needed... Only EpiLur uses...
102     public boolean handlesExclusively(Permission ... pond) {
103         return false;
104     }
105     
106     /**
107      * Get Lur for index.  Returns null if out of range
108      * @param idx
109      * @return
110      */
111     public Lur get(int idx) {
112         if (idx>=0 && idx<lurs.length) {
113             return lurs[idx];
114         }
115         return null;
116     }
117
118     public boolean handles(Principal p) {
119         for (Lur l : lurs) {
120             if (l.handles(p)) {
121                 return true;
122             }
123         }
124         return false;
125     }
126
127     public void remove(String id) {
128         for (Lur l : lurs) {
129             if (l instanceof CachingLur) {
130                 ((CachingLur<?>)l).remove(id);
131             }
132         }
133     }
134     
135     public Lur subLur(Class<? extends Lur> cls ) {
136         for (Lur l : lurs) {
137             if (l.getClass().isAssignableFrom(cls)) {
138                 return l;
139             }
140         }
141         return null;
142     }
143
144     @Override
145     public Permission createPerm(String p) {
146         return new LocalPermission(p);
147     }
148
149     /* (non-Javadoc)
150      * @see org.onap.aaf.cadi.Lur#clear(java.security.Principal, java.lang.StringBuilder)
151      */
152     @Override
153     public void clear(Principal p, StringBuilder report) {
154         for (Lur lur : lurs) {
155             lur.clear(p, report);
156         }
157     }
158     
159     public String toString() {
160         StringBuilder sb = new StringBuilder();
161         for (Lur lur : lurs) {
162             sb.append(lur.getClass().getSimpleName());
163             sb.append(": Report\n");
164             sb.append(lur.toString());
165             sb.append('\n');
166         }
167         return sb.toString();
168     }
169 }