AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / org / OrganizationFactory.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.auth.org;
23
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.cadi.util.FQI;
32 import org.onap.aaf.misc.env.Env;
33 import org.onap.aaf.misc.env.impl.BasicEnv;
34
35 /**
36  * Organization Plugin Mechanism
37  * 
38  * Define a NameSpace for the company (i.e. com.att), and put in Properties as 
39  * "Organization.[your NS" and assign the supporting Class.  
40  * 
41  * Example:
42  * Organization.com.att=org.onap.aaf.auth.org.test.att.ATT
43  *
44  * @author Pavani, Jonathan
45  *
46  */
47 public class OrganizationFactory {
48         private static final String ORGANIZATION_DOT = "Organization.";
49         private static Organization defaultOrg = null;
50         private static Map<String,Organization> orgs = new ConcurrentHashMap<String,Organization>();
51         public static Organization init(BasicEnv env) throws OrganizationException {
52                 int idx = ORGANIZATION_DOT.length();
53                 Organization org,firstOrg = null;
54                 
55                 for(Entry<Object, Object> es : env.getProperties().entrySet()) {
56                         String key = es.getKey().toString();
57                         if(key.startsWith(ORGANIZATION_DOT)) {
58                                 org = obtain(env,key.substring(idx));
59                                 if(firstOrg==null) {
60                                         firstOrg = org;
61                                 }
62                         }
63                 }
64                 if(defaultOrg == null) {
65                         defaultOrg = firstOrg;
66                 }
67                 return defaultOrg;
68         }
69         public static Organization obtain(Env env,final String theNS) throws OrganizationException {
70                 String orgNS;
71                 if(theNS.indexOf('@')>=0) {
72                         orgNS=FQI.reverseDomain(theNS);
73                 } else {
74                         orgNS=theNS;
75                 }
76                 Organization org = orgs.get(orgNS);
77                 if(org == null) {
78                         String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
79                         if(orgClass == null) {
80                                 env.warn().log("There is no Organization." + orgNS + " property");
81                         } else {
82                                 for(Organization o : orgs.values()) {
83                                         if(orgClass.equals(o.getClass().getName())) {
84                                                 org = o;
85                                         }
86                                 }
87                                 if(org==null) {
88                                         try {
89                                                 @SuppressWarnings("unchecked")
90                                                 Class<Organization> cls = (Class<Organization>) Class.forName(orgClass);
91                                                 Constructor<Organization> cnst = cls.getConstructor(Env.class,String.class);
92                                                 org = cnst.newInstance(env,orgNS);
93                                         } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | 
94                                                         InstantiationException | IllegalAccessException | IllegalArgumentException | 
95                                                         InvocationTargetException e) {
96                                                 env.error().log(e, "Error on Organization Construction");
97                                                 throw new OrganizationException(e);
98                                         }
99                                 }
100                                 orgs.put(orgNS, org);
101                                 if("true".equalsIgnoreCase(env.getProperty(orgNS+".default"))) {
102                                         defaultOrg = org;
103                                 }
104
105                         }
106                         if(org==null) {
107                                 if(defaultOrg!=null) {
108                                         org=defaultOrg;
109                                         orgs.put(orgNS, org);
110                                 }
111                         }
112                 }
113                 
114                 return org;
115         }
116
117         public static Organization get(AuthzTrans trans) throws OrganizationException {
118                 String domain = FQI.reverseDomain(trans.user());
119                 Organization org = orgs.get(domain);
120                 if(org==null) {
121                         org = defaultOrg; // can be null, btw, unless set.
122                 }
123                 return org;
124         }
125 }