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