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