79dd9eeadde4104f876e5d051e53689b55c760a2
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / aaf / AAFPermission.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.aaf;\r
24 \r
25 import com.att.cadi.Permission;\r
26 \r
27 /**\r
28  * A Class that understands the AAF format of Permission (name/type/action)\r
29  *  or String "name|type|action"\r
30  * \r
31  *\r
32  */\r
33 public class AAFPermission implements Permission {\r
34         protected String type,instance,action,key;\r
35 \r
36         protected AAFPermission() {}\r
37 \r
38         public AAFPermission(String type, String instance, String action) {\r
39                 this.type = type;\r
40                 this.instance = instance;\r
41                 this.action = action;\r
42                 key = type + '|' + instance + '|' + action;\r
43         }\r
44         \r
45         /**\r
46          * Match a Permission\r
47          * if Permission is Fielded type "Permission", we use the fields\r
48          * otherwise, we split the Permission with '|'\r
49          * \r
50          * when the type or action starts with REGEX indicator character ( ! ),\r
51          * then it is evaluated as a regular expression.\r
52          * \r
53          * If you want a simple field comparison, it is faster without REGEX\r
54          */\r
55         public boolean match(Permission p) {\r
56                 if(p instanceof AAFPermission) {\r
57                         AAFPermission ap = (AAFPermission)p;\r
58                         // Note: In AAF > 1.0, Accepting "*" from name would violate multi-tenancy\r
59                         // Current solution is only allow direct match on Type.\r
60                         // 8/28/2014 - added REGEX ability\r
61                         if(type.equals(ap.getName()))  \r
62                                 if(PermEval.evalInstance(instance,ap.getInstance()))\r
63                                         if(PermEval.evalAction(action,ap.getAction()))\r
64                                                 return true;\r
65                 } else {\r
66                         // Permission is concatenated together: separated by |\r
67                         String[] aaf = p.getKey().split("[\\s]*\\|[\\s]*",3);\r
68                         if(aaf.length>0 && type.equals(aaf[0]))\r
69                                 if(PermEval.evalInstance(instance,aaf.length>1?aaf[1]:"*"))\r
70                                         if(PermEval.evalAction(action,aaf.length>2?aaf[2]:"*"))\r
71                                                 return true;\r
72                 }                               \r
73                 return false;\r
74         }\r
75 \r
76         public String getName() {\r
77                 return type;\r
78         }\r
79         \r
80         public String getInstance() {\r
81                 return instance;\r
82         }\r
83         \r
84         public String getAction() {\r
85                 return action;\r
86         }\r
87         \r
88         public String getKey() {\r
89                 return key;\r
90         }\r
91 \r
92         /* (non-Javadoc)\r
93          * @see com.att.cadi.Permission#permType()\r
94          */\r
95         public String permType() {\r
96                 return "AAF";\r
97         }\r
98 \r
99         public String toString() {\r
100                 return "AAFPermission:\n\tType: " + type + \r
101                                 "\n\tInstance: " + instance +\r
102                                 "\n\tAction: " + action +\r
103                                 "\n\tKey: " + key;\r
104         }\r
105 }\r