Mass whitespace changes (Style Warnings)
[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         String aafNS;
87         String aafType;
88         String aafInstance;
89         String aafAction;
90         if (p instanceof AAFPermission) {
91             AAFPermission ap = (AAFPermission)p;
92             // Note: In AAF > 1.0, Accepting "*" from name would violate multi-tenancy
93             // Current solution is only allow direct match on Type.
94             // 8/28/2014 Jonathan - added REGEX ability
95             aafNS = ap.getNS();
96             aafType = ap.getType();
97             aafInstance = ap.getInstance();
98             aafAction = ap.getAction();
99         } else {
100             // Permission is concatenated together: separated by 
101             String[] aaf = Split.splitTrim('|', p.getKey());
102             switch(aaf.length) {
103                 case 1:
104                     aafNS = aaf[0];
105                     aafType="";
106                     aafInstance = aafAction = "*";
107                     break;
108                 case 2:
109                     aafNS = aaf[0];
110                     aafType = aaf[1];
111                     aafInstance = aafAction = "*";
112                     break;
113                 case 3:
114                     aafNS = aaf[0];
115                     aafType = aaf[1];
116                     aafInstance = aaf[2]; 
117                     aafAction = "*";
118                     break;
119                 default:
120                     aafNS = aaf[0];
121                     aafType = aaf[1];
122                     aafInstance = aaf[2]; 
123                     aafAction = aaf[3];
124                 break;
125             }
126         }
127         boolean typeMatches;
128         if (aafNS==null) {
129             if (ns==null) {
130                 typeMatches = aafType.equals(type);
131             } else {
132                 typeMatches = aafType.equals(ns+'.'+type);
133             }
134         } else if (ns==null) {
135             typeMatches = type.equals(aafNS+'.'+aafType);
136         } else if (aafNS.length() == ns.length()) {
137             typeMatches = aafNS.equals(ns) && aafType.equals(type);
138         } else { // Allow for restructuring of NS/Perm structure
139             typeMatches = (aafNS+'.'+aafType).equals(ns+'.'+type);
140         }
141         return (typeMatches &&
142                 PermEval.evalInstance(instance, aafInstance) &&
143                 PermEval.evalAction(action, aafAction));
144     }
145
146     public String getNS() {
147         return ns;
148     }
149
150     public String getType() {
151         return type;
152     }
153
154     public String getFullType() {
155         return ns + '.' + type;
156     }
157     
158     public String getInstance() {
159         return instance;
160     }
161     
162     public String getAction() {
163         return action;
164     }
165     
166     public String getKey() {
167         return key;
168     }
169
170     /* (non-Javadoc)
171      * @see org.onap.aaf.cadi.Permission#permType()
172      */
173     public String permType() {
174         return "AAF";
175     }
176
177     public List<String> roles() {
178         return roles;
179     }
180     public String toString() {
181         return "AAFPermission:" +
182                 "\n\tNS: " + ns +
183                 "\n\tType: " + type + 
184                 "\n\tInstance: " + instance +
185                 "\n\tAction: " + action +
186                 "\n\tKey: " + key;
187     }
188 }