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