Support Multiple Realms for DefaultOrg
[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.cadi.util.Split;
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.impl.BasicEnv;
35
36 /**
37  * Organization Plugin Mechanism
38  * 
39  * Define a NameSpace for the company (i.e. com.att), and put in Properties as 
40  * "Organization.[your NS" and assign the supporting Class.  
41  * 
42  * Example:
43  * Organization.com.att=org.onap.aaf.auth.org.test.att.ATT
44  *
45  * @author Pavani, Jonathan
46  *
47  */
48 public class OrganizationFactory {
49         private static final String ORGANIZATION_DOT = "Organization.";
50         private static Organization defaultOrg = null;
51         private static Map<String,Organization> orgs = new ConcurrentHashMap<String,Organization>();
52         public static Organization init(BasicEnv env) throws OrganizationException {
53                 int idx = ORGANIZATION_DOT.length();
54                 Organization org,firstOrg = null;
55                 
56                 for(Entry<Object, Object> es : env.getProperties().entrySet()) {
57                         String key = es.getKey().toString();
58                         if(key.startsWith(ORGANIZATION_DOT)) {
59                                 org = obtain(env,key.substring(idx));
60                                 if(firstOrg==null) {
61                                         firstOrg = org;
62                                 }
63                         }
64                 }
65                 if(defaultOrg == null) {
66                         defaultOrg = firstOrg;
67                 }
68                 return defaultOrg;
69         }
70         public static Organization obtain(Env env,final String theNS) throws OrganizationException {
71                 String orgNS;
72                 if(theNS.indexOf('@')>=0) {
73                         orgNS=FQI.reverseDomain(theNS);
74                 } else {
75                         orgNS=theNS;
76                 }
77                 Organization org = orgs.get(orgNS);
78                 if(org == null) {
79                         env.debug().printf("Attempting to instantiate Organization %s\n",orgNS);
80
81                         String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
82                         if(orgClass == null) {
83                                 env.warn().log("There is no Organization." + orgNS + " property");
84                         } else {
85                                 try {
86                                         Class<?> orgCls = Class.forName(orgClass);
87                                         for(Organization o : orgs.values()) {
88                                                 if(o.getClass().isAssignableFrom(orgCls)) {
89                                                         org = o;
90                                                 }
91                                         }
92                                 } catch (ClassNotFoundException e1) {
93                                         env.error().log(e1, orgClass + " is not on the Classpath.");
94                                         throw new OrganizationException(e1);
95                                 }
96                                 if(org==null) {
97                                         try {
98                                                 @SuppressWarnings("unchecked")
99                                                 Class<Organization> cls = (Class<Organization>) Class.forName(orgClass);
100                                                 Constructor<Organization> cnst = cls.getConstructor(Env.class,String.class);
101                                                 org = cnst.newInstance(env,orgNS);
102                                                 String other_realms = env.getProperty(orgNS+".also_supports");
103                                                 if(other_realms!=null) {
104                                                         for(String r : Split.splitTrim(',', other_realms)) {
105                                                                 org.addSupportedRealm(r);
106                                                         }
107                                                 }
108                                                 
109                                         } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | 
110                                                         InstantiationException | IllegalAccessException | IllegalArgumentException | 
111                                                         InvocationTargetException e) {
112                                                 env.error().log(e, "Error on Organization Construction");
113                                                 throw new OrganizationException(e);
114                                         }
115                                 }
116                                 orgs.put(orgNS, org);
117                                 boolean isDefault;
118                                 if((isDefault="true".equalsIgnoreCase(env.getProperty(orgNS+".default")))) {
119                                         defaultOrg = org;
120                                 }
121                                 env.init().printf("Instantiated %s with %s%s\n",orgNS,orgClass,(isDefault?" as default":""));
122                         }
123                         if(org==null) {
124                                 if(defaultOrg!=null) {
125                                         org=defaultOrg;
126                                         orgs.put(orgNS, org);
127                                 }
128                         }
129                 }
130                 
131                 return org;
132         }
133
134         public static Organization get(AuthzTrans trans) throws OrganizationException {
135                 String domain = FQI.reverseDomain(trans.user());
136                 Organization org = orgs.get(domain);
137                 if(org==null) {
138                         org = defaultOrg; // can be null, btw, unless set.
139                 }
140                 return org;
141         }
142 }