9986c60a493f8fbbc3f248ea4f4474f6ec984940
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / AAF_Locate.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
23 package org.onap.aaf.auth.locate;
24
25 import java.net.URI;
26 import java.util.Map;
27
28 import javax.servlet.Filter;
29
30 import org.onap.aaf.auth.cache.Cache;
31 import org.onap.aaf.auth.cache.Cache.Dated;
32 import org.onap.aaf.auth.dao.CassAccess;
33 import org.onap.aaf.auth.dao.cass.ConfigDAO;
34 import org.onap.aaf.auth.dao.cass.LocateDAO;
35 import org.onap.aaf.auth.direct.DirectLocatorCreator;
36 import org.onap.aaf.auth.direct.DirectRegistrar;
37 import org.onap.aaf.auth.env.AuthzEnv;
38 import org.onap.aaf.auth.env.AuthzTrans;
39 import org.onap.aaf.auth.env.AuthzTransFilter;
40 import org.onap.aaf.auth.locate.api.API_AAFAccess;
41 import org.onap.aaf.auth.locate.api.API_Api;
42 import org.onap.aaf.auth.locate.api.API_Find;
43 import org.onap.aaf.auth.locate.api.API_Proxy;
44 import org.onap.aaf.auth.locate.facade.LocateFacadeFactory;
45 import org.onap.aaf.auth.locate.facade.LocateFacade_1_1;
46 import org.onap.aaf.auth.locate.mapper.Mapper.API;
47 import org.onap.aaf.auth.rserv.HttpMethods;
48 import org.onap.aaf.auth.server.AbsService;
49 import org.onap.aaf.auth.server.JettyServiceStarter;
50 import org.onap.aaf.auth.server.Log4JLogIt;
51 import org.onap.aaf.cadi.CadiException;
52 import org.onap.aaf.cadi.Locator;
53 import org.onap.aaf.cadi.LocatorException;
54 import org.onap.aaf.cadi.PropAccess;
55 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
56 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
57 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
58 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
59 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
60 import org.onap.aaf.cadi.config.Config;
61 import org.onap.aaf.cadi.register.Registrant;
62 import org.onap.aaf.misc.env.APIException;
63 import org.onap.aaf.misc.env.Data;
64 import org.onap.aaf.misc.env.Env;
65
66 import com.datastax.driver.core.Cluster;
67
68 public class AAF_Locate extends AbsService<AuthzEnv, AuthzTrans> {
69         private static final String DOT_LOCATOR = ".locator";
70
71         private static final String USER_PERMS = "userPerms";
72         private LocateFacade_1_1 facade; // this is the default Facade
73         private LocateFacade_1_1 facade_1_1_XML;
74         public Map<String, Dated> cacheUser;
75         public final AAFAuthn<?> aafAuthn;
76         public final AAFLurPerm aafLurPerm;
77         private Locator<URI> gui_locator;
78         public final long expireIn;
79         private final Cluster cluster;
80         public final LocateDAO locateDAO;
81         public final ConfigDAO configDAO;
82         private Locator<URI> dal;
83         private final String aaf_service_name;
84         private final String aaf_gui_name;
85
86         
87         /**
88          * Construct AuthzAPI with all the Context Supporting Routes that Authz needs
89          * 
90          * @param env
91          * @param si 
92          * @param dm 
93          * @param decryptor 
94          * @throws APIException 
95          */
96         public AAF_Locate(final AuthzEnv env) throws Exception {
97                 super(env.access(), env);
98                 aaf_service_name = app_name.replace(DOT_LOCATOR, ".service");
99                 aaf_gui_name = app_name.replace(DOT_LOCATOR, ".gui");
100                 
101                 expireIn = Long.parseLong(env.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF));
102
103                 // Initialize Facade for all uses
104                 AuthzTrans trans = env.newTransNoAvg();
105
106                 cluster = org.onap.aaf.auth.dao.CassAccess.cluster(env,null);
107                 locateDAO = new LocateDAO(trans,cluster,CassAccess.KEYSPACE);
108                 configDAO = new ConfigDAO(trans,locateDAO); // same stuff
109
110                 // Have AAFLocator object Create DirectLocators for Location needs
111                 AbsAAFLocator.setCreator(new DirectLocatorCreator(env, locateDAO));
112
113                 aafLurPerm = aafCon().newLur();
114                 // Note: If you need both Authn and Authz construct the following:
115                 aafAuthn = aafCon().newAuthn(aafLurPerm);
116
117
118                 facade = LocateFacadeFactory.v1_1(env,this,trans,Data.TYPE.JSON);   // Default Facade
119                 facade_1_1_XML = LocateFacadeFactory.v1_1(env,this,trans,Data.TYPE.XML);
120
121                 synchronized(env) {
122                         if(cacheUser == null) {
123                                 cacheUser = Cache.obtain(USER_PERMS);
124                                 Cache.startCleansing(env, USER_PERMS);
125                         }
126                 }
127
128
129                 ////////////////////////////////////////////////////////////////////////////
130                 // Time Critical
131                 //  These will always be evaluated first
132                 ////////////////////////////////////////////////////////////////////////
133                 API_AAFAccess.init(this,facade);
134                 API_Find.init(this, facade);
135                 API_Proxy.init(this, facade);
136                 
137                 ////////////////////////////////////////////////////////////////////////
138                 // Management APIs
139                 ////////////////////////////////////////////////////////////////////////
140                 // There are several APIs around each concept, and it gets a bit too
141                 // long in this class to create.  The initialization of these Management
142                 // APIs have therefore been pushed to StandAlone Classes with static
143                 // init functions
144                 API_Api.init(this, facade);
145
146                 ////////////////////////////////////////////////////////////////////////
147                 // Default Function
148                 ////////////////////////////////////////////////////////////////////////
149                 API_AAFAccess.initDefault(this,facade);
150                 
151         }
152
153         
154         /**
155          * Setup XML and JSON implementations for each supported Version type
156          * 
157          * We do this by taking the Code passed in and creating clones of these with the appropriate Facades and properties
158          * to do Versions and Content switches
159          * 
160          */
161         public void route(HttpMethods meth, String path, API api, LocateCode code) throws Exception {
162                 String version = "1.0";
163                 // Get Correct API Class from Mapper
164                 Class<?> respCls = facade.mapper().getClass(api); 
165                 if(respCls==null) throw new Exception("Unknown class associated with " + api.getClass().getName() + ' ' + api.name());
166                 // setup Application API HTML ContentTypes for JSON and Route
167                 String application = applicationJSON(respCls, version);
168                 route(env,meth,path,code,application,"application/json;version="+version,"*/*","*");
169
170                 // setup Application API HTML ContentTypes for XML and Route
171                 application = applicationXML(respCls, version);
172                 route(env,meth,path,code.clone(facade_1_1_XML,false),application,"text/xml;version="+version);
173                 
174                 // Add other Supported APIs here as created
175         }
176         
177         public void routeAll(HttpMethods meth, String path, API api, LocateCode code) throws Exception {
178                 route(env,meth,path,code,""); // this will always match
179         }
180
181
182         /* (non-Javadoc)
183          * @see org.onap.aaf.auth.server.AbsServer#_newAAFConHttp()
184          */
185         @Override
186         protected AAFConHttp _newAAFConHttp() throws CadiException {
187                 try {
188                         if(dal==null) {
189                                 dal = AbsAAFLocator.create(aaf_service_name,Config.AAF_DEFAULT_VERSION);
190                         }
191                         // utilize pre-constructed DirectAAFLocator
192                         return new AAFConHttp(env.access(),dal);
193                 } catch (APIException | LocatorException e) {
194                         throw new CadiException(e);
195                 }
196
197         }
198
199         public Locator<URI> getGUILocator() throws LocatorException {
200                 if(gui_locator==null) {
201                         gui_locator = AbsAAFLocator.create(aaf_gui_name,Config.AAF_DEFAULT_VERSION);
202                 }
203                 return gui_locator;
204         }
205
206
207         @Override
208         public Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException {
209                 try {
210                         return new Filter[] {
211                                 new AuthzTransFilter(env, aafCon(), 
212                                         new AAFTrustChecker((Env)env)
213                                         ,additionalTafLurs
214                                 )};
215                 } catch (NumberFormatException e) {
216                         throw new CadiException("Invalid Property information", e);
217                 }
218         }
219
220         @SuppressWarnings("unchecked")
221         @Override
222         public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException {
223                 return new Registrant[] {
224                         new DirectRegistrar(access,locateDAO,app_name,app_version,port)
225                 };
226         }
227
228         @Override
229         public void destroy() {
230                 Cache.stopTimer();
231                 if(cluster!=null) {
232                         cluster.close();
233                 }
234                 super.destroy();
235         }
236
237         public static void main(final String[] args) {
238                 try {
239                         Log4JLogIt logIt = new Log4JLogIt(args, "locate");
240                         PropAccess propAccess = new PropAccess(logIt,args);
241
242                         AAF_Locate service = new AAF_Locate(new AuthzEnv(propAccess));
243                         JettyServiceStarter<AuthzEnv,AuthzTrans> jss = new JettyServiceStarter<AuthzEnv,AuthzTrans>(service);
244                         jss.start();
245                 } catch (Exception e) {
246                         e.printStackTrace();
247                 }
248         }
249 }