Update AAF Version 1.0.0
[aaf/cadi.git] / core / src / main / java / org / onap / aaf / cadi / lur / EpiLur.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi.lur;\r
24 \r
25 import java.security.Principal;\r
26 import java.util.List;\r
27 \r
28 import org.onap.aaf.cadi.CachingLur;\r
29 import org.onap.aaf.cadi.CadiException;\r
30 import org.onap.aaf.cadi.CredVal;\r
31 import org.onap.aaf.cadi.Lur;\r
32 import org.onap.aaf.cadi.Permission;\r
33 \r
34 /**\r
35  * EpiLUR\r
36  * \r
37  * Short for "Epic LUR". Be able to run through a series of LURs to obtain the validation needed.\r
38  * \r
39  * The pun is better for the other pattern... "TAF" (aka EpiTaf), but it's still the larger picture of \r
40  * LURs that will be accomplished.\r
41  * \r
42  * FYI, the reason we separate LURs, rather than combine, is that Various User Repository Resources have\r
43  * different Caching requirements.  For instance, the Local User Repo (with stand alone names), never expire, but might be\r
44  * refreshed with a change in Configuration File, while the Remote Service based LURs will need to expire at prescribed intervals \r
45  * \r
46  *\r
47  */\r
48 public final class EpiLur implements Lur {\r
49         private final Lur[] lurs;\r
50         \r
51         /**\r
52          * EpiLur constructor\r
53          * \r
54          * Construct the EpiLur from variable TAF parameters\r
55          * @param lurs\r
56          * @throws CadiException\r
57          */\r
58         public EpiLur(Lur ... lurs) throws CadiException{\r
59                 this.lurs = lurs;\r
60                 if(lurs.length==0) throw new CadiException("Need at least one Lur implementation in constructor");\r
61         }\r
62 \r
63         public boolean fish(Principal bait, Permission pond) {\r
64                 if(pond==null) {\r
65                         return false;\r
66                 }\r
67                 boolean rv = false;\r
68                 Lur lur;\r
69                 for(int i=0;!rv && i<lurs.length;++i) {\r
70                         rv = (lur = lurs[i]).fish(bait, pond);\r
71                         if(!rv && lur.handlesExclusively(pond)) break;\r
72                 }\r
73                 return rv;\r
74         }\r
75 \r
76         public void fishAll(Principal bait, List<Permission> permissions) {\r
77                 for(Lur lur : lurs) {\r
78                         lur.fishAll(bait, permissions);\r
79                 }\r
80         }\r
81 \r
82         public void destroy() {\r
83                 for(Lur lur : lurs) {\r
84                         lur.destroy();\r
85                 }\r
86         }\r
87 \r
88         /**\r
89          * Return the first Lur (if any) which also implements UserPass \r
90          * @return\r
91          */\r
92         public CredVal getUserPassImpl() {\r
93                 for(Lur lur : lurs) {\r
94                         if(lur instanceof CredVal) {\r
95                                 return (CredVal)lur;\r
96                         }\r
97                 }\r
98                 return null;\r
99         }\r
100 \r
101         // Never needed... Only EpiLur uses...\r
102         public boolean handlesExclusively(Permission pond) {\r
103                 return false;\r
104         }\r
105         \r
106         /**\r
107          * Get Lur for index.  Returns null if out of range\r
108          * @param idx\r
109          * @return\r
110          */\r
111         public Lur get(int idx) {\r
112                 if(idx>=0 && idx<lurs.length) {\r
113                         return lurs[idx];\r
114                 }\r
115                 return null;\r
116         }\r
117 \r
118         public boolean supports(String userName) {\r
119                 for(Lur l : lurs) {\r
120                         if(l.supports(userName))return true;\r
121                 }\r
122                 return false;\r
123         }\r
124 \r
125         public void remove(String id) {\r
126                 for(Lur l : lurs) {\r
127                         if(l instanceof CachingLur) {\r
128                                 ((CachingLur<?>)l).remove(id);\r
129                         }\r
130                 }\r
131         }\r
132         \r
133         public Lur subLur(Class<? extends Lur> cls ) {\r
134                 for(Lur l : lurs) {\r
135                         if(l.getClass().isAssignableFrom(cls)) {\r
136                                 return l;\r
137                         }\r
138                 }\r
139                 return null;\r
140         }\r
141 \r
142         @Override\r
143         public Permission createPerm(String p) {\r
144                 return new LocalPermission(p);\r
145         }\r
146 \r
147         /* (non-Javadoc)\r
148          * @see com.att.cadi.Lur#clear(java.security.Principal, java.lang.StringBuilder)\r
149          */\r
150         @Override\r
151         public void clear(Principal p, StringBuilder report) {\r
152                 for(Lur lur : lurs) {\r
153                         lur.clear(p, report);\r
154                 }\r
155         }\r
156         \r
157         public String toString() {\r
158                 StringBuilder sb = new StringBuilder();\r
159                 for(Lur lur : lurs) {\r
160                         sb.append(lur.getClass().getSimpleName());\r
161                         sb.append(": Report\n");\r
162                         sb.append(lur.toString());\r
163                         sb.append('\n');\r
164                 }\r
165                 return sb.toString();\r
166         }\r
167 }\r