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