Remove Tabs, per Jococo
[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 import org.onap.aaf.misc.env.util.Split;
29
30 /**
31  * A Class that understands the AAF format of Permission (name/type/action)
32  *  or String "name|type|action"
33  * 
34  * @author Jonathan
35  *
36  */
37 public class AAFPermission implements Permission {
38     private static final List<String> NO_ROLES;
39     protected String ns,type,instance,action,key;
40     private List<String> roles;
41     
42     static {
43         NO_ROLES = new ArrayList<>();
44     }
45
46     protected AAFPermission() {roles=NO_ROLES;}
47
48     public AAFPermission(String ns, String name, String instance, String action) {
49         this.ns = ns;
50         type = name;
51         this.instance = instance;
52         this.action = action;
53         if (ns==null) {
54             key = type + '|' + instance + '|' + action;
55         } else {
56             key = ns + '|' + type + '|' + instance + '|' + action;
57         }
58         this.roles = NO_ROLES;
59
60     }
61
62     public AAFPermission(String ns, String name, String instance, String action, List<String> roles) {
63         this.ns = ns;
64         type = name;
65         this.instance = instance;
66         this.action = action;
67         if (ns==null) {
68             key = type + '|' + instance + '|' + action;
69         } else {
70             key = ns + '|' + type + '|' + instance + '|' + action;
71         }
72         this.roles = roles==null?NO_ROLES:roles;
73     }
74     
75     /**
76      * Match a Permission
77      * if Permission is Fielded type "Permission", we use the fields
78      * otherwise, we split the Permission with '|'
79      * 
80      * when the type or action starts with REGEX indicator character ( ! ),
81      * then it is evaluated as a regular expression.
82      * 
83      * If you want a simple field comparison, it is faster without REGEX
84      */
85     public boolean match(Permission p) {
86         if(p==null) {
87             return false;
88         }
89         String aafNS;
90         String aafType;
91         String aafInstance;
92         String aafAction;
93         if (p instanceof AAFPermission) {
94             AAFPermission ap = (AAFPermission)p;
95             // Note: In AAF > 1.0, Accepting "*" from name would violate multi-tenancy
96             // Current solution is only allow direct match on Type.
97             // 8/28/2014 Jonathan - added REGEX ability
98             aafNS = ap.getNS();
99             aafType = ap.getType();
100             aafInstance = ap.getInstance();
101             aafAction = ap.getAction();
102         } else {
103             // Permission is concatenated together: separated by 
104             String[] aaf = Split.splitTrim('|', p.getKey());
105             switch(aaf.length) {
106                 case 1:
107                     aafNS = aaf[0];
108                     aafType="";
109                     aafInstance = aafAction = "*";
110                     break;
111                 case 2:
112                     aafNS = aaf[0];
113                     aafType = aaf[1];
114                     aafInstance = aafAction = "*";
115                     break;
116                 case 3:
117                     aafNS = aaf[0];
118                     aafType = aaf[1];
119                     aafInstance = aaf[2]; 
120                     aafAction = "*";
121                     break;
122                 default:
123                     aafNS = aaf[0];
124                     aafType = aaf[1];
125                     aafInstance = aaf[2]; 
126                     aafAction = aaf[3];
127                 break;
128             }
129         }
130         boolean typeMatches;
131         if (aafNS==null) {
132             if (ns==null) {
133                 typeMatches = aafType.equals(type);
134             } else {
135                 typeMatches = aafType.equals(ns+'.'+type);
136             }
137         } else if (ns==null) {
138             typeMatches = type.equals(aafNS+'.'+aafType);
139         } else if (aafNS.length() == ns.length()) {
140             typeMatches = aafNS.equals(ns) && aafType.equals(type);
141         } else { // Allow for restructuring of NS/Perm structure
142             typeMatches = (aafNS+'.'+aafType).equals(ns+'.'+type);
143         }
144         return (typeMatches &&
145                 PermEval.evalInstance(instance, aafInstance) &&
146                 PermEval.evalAction(action, aafAction));
147     }
148
149     public String getNS() {
150         return ns;
151     }
152
153     public String getType() {
154         return type;
155     }
156
157     public String getFullType() {
158         return ns + '.' + type;
159     }
160     
161     public String getInstance() {
162         return instance;
163     }
164     
165     public String getAction() {
166         return action;
167     }
168     
169     public String getKey() {
170         return key;
171     }
172
173     /* (non-Javadoc)
174      * @see org.onap.aaf.cadi.Permission#permType()
175      */
176     public String permType() {
177         return "AAF";
178     }
179
180     public List<String> roles() {
181         return roles;
182     }
183     public String toString() {
184         return "AAFPermission:" +
185                 "\n\tNS: " + ns +
186                 "\n\tType: " + type + 
187                 "\n\tInstance: " + instance +
188                 "\n\tAction: " + action +
189                 "\n\tKey: " + key;
190     }
191 }