[AAF-21] Initial code import
[aaf/cadi.git] / aaf / src / src / main / java / com / att / cadi / aaf / AAFTransmutate.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 java.security.Principal;\r
27 import java.util.regex.Pattern;\r
28 \r
29 import com.att.cadi.Transmutate;\r
30 import com.att.cadi.lur.ConfigPrincipal;\r
31 import com.att.cadi.principal.BasicPrincipal;\r
32 import com.att.cadi.principal.CSPPrincipal_T;\r
33 \r
34 /**\r
35  * AAFTransmutate\r
36  * \r
37  * Each System determines the mechanisms for which one Principal is transmutated to another, such as whether it is created\r
38  * independently, etc.\r
39  * \r
40  * For AAF, the only important thing is that these are valid ATTUID/mechIDs, to avoid unnecessary user hits\r
41  * \r
42  * attUIDs look like ab1234 or AB1234 or AZ123a\r
43  * mechids look like m12345\r
44  * \r
45  *\r
46  */\r
47 public final class AAFTransmutate implements Transmutate<Principal> {\r
48         private Pattern pattern = Pattern.compile("[a-zA-Z]\\w\\d\\d\\d\\w");\r
49 \r
50         public Principal mutate(Principal p) {\r
51                 // Accept these three internal kinds of Principals\r
52                 if(p instanceof CSPPrincipal_T \r
53                         || p instanceof BasicPrincipal\r
54                         || p instanceof ConfigPrincipal) { \r
55                         return p;\r
56                 } else { \r
57                         String name = p.getName();\r
58                         final int idx = name.indexOf('@');\r
59                         if(idx>0) { // strip off any domain\r
60                                 name = name.substring(0,idx); \r
61                         }\r
62 \r
63                         // Check for ATTUID specs before creating CSP_T\r
64                         return pattern.matcher(name).matches()?\r
65                                 new CSP_T(name):\r
66                                 null;\r
67                 }\r
68         }\r
69 \r
70         /**\r
71          * Essential Principal reflecting CSP Principal\r
72          * \r
73          *\r
74          */\r
75         private final class CSP_T implements CSPPrincipal_T {\r
76                 private String name;\r
77                 public CSP_T(String name) {\r
78                         this.name = name;\r
79                 }\r
80                 public String getName() {\r
81                         return name;\r
82                 }\r
83         }\r
84 }\r