843e26820cb7feb281916dc6c151723443686dc3
[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                         env.debug().printf("Attempting to instantiate Organization %s\n",orgNS);
79
80                         String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
81                         if(orgClass == null) {
82                                 env.warn().log("There is no Organization." + orgNS + " property");
83                         } else {
84                                 try {
85                                         Class<?> orgCls = Class.forName(orgClass);
86                                         for(Organization o : orgs.values()) {
87                                                 if(o.getClass().isAssignableFrom(orgCls)) {
88                                                         org = o;
89                                                 }
90                                         }
91                                 } catch (ClassNotFoundException e1) {
92                                         env.error().log(e1, orgClass + " is not on the Classpath.");
93                                         throw new OrganizationException(e1);
94                                 }
95                                 if(org==null) {
96                                         try {
97                                                 @SuppressWarnings("unchecked")
98                                                 Class<Organization> cls = (Class<Organization>) Class.forName(orgClass);
99                                                 Constructor<Organization> cnst = cls.getConstructor(Env.class,String.class);
100                                                 org = cnst.newInstance(env,orgNS);
101                                         } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | 
102                                                         InstantiationException | IllegalAccessException | IllegalArgumentException | 
103                                                         InvocationTargetException e) {
104                                                 env.error().log(e, "Error on Organization Construction");
105                                                 throw new OrganizationException(e);
106                                         }
107                                 }
108                                 orgs.put(orgNS, org);
109                                 boolean isDefault;
110                                 if((isDefault="true".equalsIgnoreCase(env.getProperty(orgNS+".default")))) {
111                                         defaultOrg = org;
112                                 }
113                                 env.init().printf("Instantiated %s with %s%s\n",orgNS,orgClass,(isDefault?" as default":""));
114                         }
115                         if(org==null) {
116                                 if(defaultOrg!=null) {
117                                         org=defaultOrg;
118                                         orgs.put(orgNS, org);
119                                 }
120                         }
121                 }
122                 
123                 return org;
124         }
125
126         public static Organization get(AuthzTrans trans) throws OrganizationException {
127                 String domain = FQI.reverseDomain(trans.user());
128                 Organization org = orgs.get(domain);
129                 if(org==null) {
130                         org = defaultOrg; // can be null, btw, unless set.
131                 }
132                 return org;
133         }
134 }