Collection syntax change because of Sonar
[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<>();
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                 String aafType;
75                 String aafInstance;
76                 String aafAction;
77                 if(p instanceof AAFPermission) {
78                         AAFPermission ap = (AAFPermission)p;
79                         // Note: In AAF > 1.0, Accepting "*" from name would violate multi-tenancy
80                         // Current solution is only allow direct match on Type.
81                         // 8/28/2014 Jonathan - added REGEX ability
82                         aafType = ap.getName();
83                         aafInstance = ap.getInstance();
84                         aafAction = ap.getAction();
85                 } else {
86                         // Permission is concatenated together: separated by |
87                         String[] aaf = p.getKey().split("[\\s]*\\|[\\s]*",3);
88                         aafType = aaf[0];
89                         aafInstance = (aaf.length > 1) ? aaf[1] : "*";
90                         aafAction = (aaf.length > 2) ? aaf[2] : "*";
91                 }
92                 return ((type.equals(aafType)) &&
93                                 (PermEval.evalInstance(instance, aafInstance)) &&
94                                 (PermEval.evalAction(action, aafAction)));
95         }
96
97         public String getName() {
98                 return type;
99         }
100         
101         public String getInstance() {
102                 return instance;
103         }
104         
105         public String getAction() {
106                 return action;
107         }
108         
109         public String getKey() {
110                 return key;
111         }
112
113         /* (non-Javadoc)
114          * @see org.onap.aaf.cadi.Permission#permType()
115          */
116         public String permType() {
117                 return "AAF";
118         }
119
120         public List<String> roles() {
121                 return roles;
122         }
123         public String toString() {
124                 return "AAFPermission:\n\tType: " + type + 
125                                 "\n\tInstance: " + instance +
126                                 "\n\tAction: " + action +
127                                 "\n\tKey: " + key;
128         }
129 }