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