AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / AAFPermission.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.aaf;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.onap.aaf.cadi.Permission;
28
29 /**
30  * A Class that understands the AAF format of Permission (name/type/action)
31  *  or String "name|type|action"
32  * 
33  * @author Jonathan
34  *
35  */
36 public class AAFPermission implements Permission {
37         private static final List<String> NO_ROLES;
38         protected String type,instance,action,key;
39         private List<String> roles;
40         
41         static {
42                 NO_ROLES = new ArrayList<String>();
43         }
44
45         protected AAFPermission() {roles=NO_ROLES;}
46
47         public AAFPermission(String type, String instance, String action) {
48                 this.type = type;
49                 this.instance = instance;
50                 this.action = action;
51                 key = type + '|' + instance + '|' + action;
52                 this.roles = NO_ROLES;
53
54         }
55         public AAFPermission(String type, String instance, String action, List<String> roles) {
56                 this.type = type;
57                 this.instance = instance;
58                 this.action = action;
59                 key = type + '|' + instance + '|' + action;
60                 this.roles = roles==null?NO_ROLES:roles;
61         }
62         
63         /**
64          * Match a Permission
65          * if Permission is Fielded type "Permission", we use the fields
66          * otherwise, we split the Permission with '|'
67          * 
68          * when the type or action starts with REGEX indicator character ( ! ),
69          * then it is evaluated as a regular expression.
70          * 
71          * If you want a simple field comparison, it is faster without REGEX
72          */
73         public boolean match(Permission p) {
74                 if(p instanceof AAFPermission) {
75                         AAFPermission ap = (AAFPermission)p;
76                         // Note: In AAF > 1.0, Accepting "*" from name would violate multi-tenancy
77                         // Current solution is only allow direct match on Type.
78                         // 8/28/2014 Jonathan - added REGEX ability
79                         if(type.equals(ap.getName()))  
80                                 if(PermEval.evalInstance(instance,ap.getInstance()))
81                                         if(PermEval.evalAction(action,ap.getAction()))
82                                                 return true;
83                 } else {
84                         // Permission is concatenated together: separated by |
85                         String[] aaf = p.getKey().split("[\\s]*\\|[\\s]*",3);
86                         if(aaf.length>0 && type.equals(aaf[0]))
87                                 if(PermEval.evalInstance(instance,aaf.length>1?aaf[1]:"*"))
88                                         if(PermEval.evalAction(action,aaf.length>2?aaf[2]:"*"))
89                                                 return true;
90                 }                               
91                 return false;
92         }
93
94         public String getName() {
95                 return type;
96         }
97         
98         public String getInstance() {
99                 return instance;
100         }
101         
102         public String getAction() {
103                 return action;
104         }
105         
106         public String getKey() {
107                 return key;
108         }
109
110         /* (non-Javadoc)
111          * @see org.onap.aaf.cadi.Permission#permType()
112          */
113         public String permType() {
114                 return "AAF";
115         }
116
117         public List<String> roles() {
118                 return roles;
119         }
120         public String toString() {
121                 return "AAFPermission:\n\tType: " + type + 
122                                 "\n\tInstance: " + instance +
123                                 "\n\tAction: " + action +
124                                 "\n\tKey: " + key;
125         }
126 }