changed to unmaintained
[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.net.UnknownHostException;
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.ConfigDAO;
35 import org.onap.aaf.auth.dao.cass.LocateDAO;
36 import org.onap.aaf.auth.dao.hl.Question;
37 import org.onap.aaf.auth.direct.DirectLocatorCreator;
38 import org.onap.aaf.auth.direct.DirectRegistrar;
39 import org.onap.aaf.auth.env.AuthzEnv;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.auth.env.AuthzTransFilter;
42 import org.onap.aaf.auth.locate.api.API_AAFAccess;
43 import org.onap.aaf.auth.locate.api.API_Api;
44 import org.onap.aaf.auth.locate.api.API_Find;
45 import org.onap.aaf.auth.locate.api.API_Proxy;
46 import org.onap.aaf.auth.locate.facade.LocateFacadeFactory;
47 import org.onap.aaf.auth.locate.facade.LocateFacade_1_1;
48 import org.onap.aaf.auth.locate.mapper.Mapper.API;
49 import org.onap.aaf.auth.rserv.HttpMethods;
50 import org.onap.aaf.auth.server.AbsService;
51 import org.onap.aaf.auth.server.JettyServiceStarter;
52 import org.onap.aaf.auth.server.Log4JLogIt;
53 import org.onap.aaf.cadi.CadiException;
54 import org.onap.aaf.cadi.Locator;
55 import org.onap.aaf.cadi.LocatorException;
56 import org.onap.aaf.cadi.PropAccess;
57 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
58 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
59 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
60 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
61 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
62 import org.onap.aaf.cadi.config.Config;
63 import org.onap.aaf.cadi.config.RegistrationPropHolder;
64 import org.onap.aaf.cadi.register.Registrant;
65 import org.onap.aaf.misc.env.APIException;
66 import org.onap.aaf.misc.env.Data;
67 import org.onap.aaf.misc.env.Env;
68
69 import com.datastax.driver.core.Cluster;
70
71 public class AAF_Locate extends AbsService<AuthzEnv, AuthzTrans> {
72     private static final String USER_PERMS = "userPerms";
73     private LocateFacade_1_1 facade; // this is the default Facade
74     private LocateFacade_1_1 facade_1_1_XML;
75     public Map<String, Dated> cacheUser;
76     public final AAFAuthn<?> aafAuthn;
77     public final AAFLurPerm aafLurPerm;
78     private Locator<URI> gui_locator;
79     public final long expireIn;
80     private final Cluster cluster;
81     public final LocateDAO locateDAO;
82     public final ConfigDAO configDAO;
83     private Locator<URI> dal;
84
85     public final Question question;
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
98         expireIn = Long.parseLong(env.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF));
99
100         // Initialize Facade for all uses
101         AuthzTrans trans = env.newTransNoAvg();
102
103         cluster = org.onap.aaf.auth.dao.CassAccess.cluster(env,null);
104         locateDAO = new LocateDAO(trans,cluster,CassAccess.KEYSPACE);
105         configDAO = new ConfigDAO(trans,locateDAO); // same stuff
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_1(env,this,trans,Data.TYPE.JSON);   // Default Facade
116         facade_1_1_XML = LocateFacadeFactory.v1_1(env,this,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         question = new Question(trans, cluster, CassAccess.KEYSPACE);
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, question);
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_1_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){
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("%CNS.%NS.service",Config.AAF_DEFAULT_API_VERSION);
188             }
189             // utilize pre-constructed DirectAAFLocator
190             return new AAFConHttp(env.access(),dal);
191         } catch (LocatorException e) {
192             throw new CadiException(e);
193         }
194     }
195
196     public Locator<URI> getGUILocator() throws LocatorException {
197         if (gui_locator==null) {
198             RegistrationPropHolder rph;
199             try {
200                  rph = new RegistrationPropHolder(access, 0);
201             } catch (UnknownHostException | CadiException e) {
202                 throw new LocatorException(e);
203             }
204             String url = rph.getPublicEntryName("gui", rph.default_container);
205             gui_locator = AbsAAFLocator.create(url,Config.AAF_DEFAULT_API_VERSION);
206         }
207         return gui_locator;
208     }
209
210
211     @Override
212     public Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException {
213         try {
214             return new Filter[] {
215                 new AuthzTransFilter(env, aafCon(),
216                     new AAFTrustChecker((Env)env)
217                     ,additionalTafLurs
218                 )};
219         } catch (NumberFormatException e) {
220             throw new CadiException("Invalid Property information", e);
221         }
222     }
223
224     @SuppressWarnings("unchecked")
225     @Override
226     public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException {
227         return new Registrant[] {
228             new DirectRegistrar(access,locateDAO,port)
229         };
230     }
231
232     @Override
233     public void destroy() {
234         Cache.stopTimer();
235         if (cluster!=null) {
236             cluster.close();
237         }
238         super.destroy();
239     }
240
241     public static void main(final String[] args) {
242         try {
243             Log4JLogIt logIt = new Log4JLogIt(args, "locate");
244             PropAccess propAccess = new PropAccess(logIt,args);
245
246             try {
247                 new JettyServiceStarter<AuthzEnv,AuthzTrans>(
248                     new AAF_Locate(new AuthzEnv(propAccess)),true)
249                         .start();
250             } catch (Exception e) {
251                 propAccess.log(e);
252             }
253         } catch (Exception e) {
254             e.printStackTrace();
255         }
256     }
257 }