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