code syncup with ECOMP updates
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / restapi / RestfulCollectorServlet.java
1
2 /*
3  * ============LICENSE_START=======================================================
4  * PROJECT
5  * ================================================================================
6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcae.restapi;
23
24 import java.io.IOException;
25 import java.net.URL;
26
27 import javax.servlet.ServletException;
28
29 import org.apache.tomcat.util.codec.binary.Base64;
30 import org.onap.dcae.commonFunction.CommonStartup;
31 import org.onap.dcae.commonFunction.VESLogger;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.att.nsa.apiServer.CommonServlet;
36 import com.att.nsa.configs.ConfigDbException;
37 import com.att.nsa.drumlin.service.framework.DrumlinErrorHandler;
38 import com.att.nsa.drumlin.service.framework.context.DrumlinRequestContext;
39 import com.att.nsa.drumlin.service.framework.routing.DrumlinRequestRouter;
40 import com.att.nsa.drumlin.service.framework.routing.playish.DrumlinPlayishRoutingFileSource;
41 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
42 import com.att.nsa.drumlin.till.nv.rrNvReadable;
43 import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
44 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
45 import com.att.nsa.security.NsaAuthenticator;
46
47 import com.att.nsa.security.authenticators.SimpleAuthenticator;
48 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
49
50
51 public class RestfulCollectorServlet extends CommonServlet
52 {
53         String authid=null;
54         String authpwd=null;
55         public String authlist;
56         
57         public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting
58         {
59                 super ( settings, "collector", false );
60                 authlist = settings.getString(CommonStartup.kSetting_authlist,null);
61         }
62
63
64
65
66         /**
67          * This is called once at server start. Use it to init any shared objects and setup the route mapping.
68          */
69         @Override
70         protected void servletSetup () throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException
71         {
72                 super.servletSetup ();
73
74                 try
75                 {
76                         // the base class provides a bunch of things like API authentication and ECOMP compliant
77                         // logging. The Restful Collector likely doesn't need API authentication, so for now,
78                         // we init the base class services with an in-memory (and empty!) config DB.
79                         commonServletSetup ( ConfigDbType.MEMORY );
80                         
81                         VESLogger.setUpEcompLogging();
82
83                         // setup the servlet routing and error handling
84                         final DrumlinRequestRouter drr = getRequestRouter ();
85
86                         // you can tell the request router what to do when a particular kind of exception is thrown.
87                         drr.setHandlerForException( IllegalArgumentException.class, new DrumlinErrorHandler()
88                         {
89                                 @Override
90                                 public void handle ( DrumlinRequestContext ctx, Throwable cause )
91                                 {
92                                         sendJsonReply ( ctx, HttpStatusCodes.k400_badRequest, cause.getMessage() );
93                                 }
94                         });
95
96                         // load the routes from the config file
97                         final URL routes = findStream ( "routes.conf" );
98                         if ( routes == null ) throw new rrNvReadable.missingReqdSetting ( "No routing configuration." );
99                         final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource ( routes );
100                         drr.addRouteSource ( drs );
101
102                         if (CommonStartup.authflag > 0) {
103                                 NsaAuthenticator<NsaSimpleApiKey> NsaAuth;
104                                 NsaAuth = AuthlistHandler(authlist);
105                                 
106                                 this.getSecurityManager().addAuthenticator(NsaAuth);
107                         }
108                         
109                         log.info ( "Restful Collector Servlet is up." );
110                 }
111                 catch ( SecurityException e )
112                 {
113                         throw new ServletException ( e );
114                 }
115                 catch ( IOException e )
116                 {
117                         throw new ServletException ( e );
118                 }
119                 catch ( ConfigDbException e )
120                 {
121                         throw new ServletException ( e );
122                 }
123         }
124
125         public NsaAuthenticator<NsaSimpleApiKey> AuthlistHandler (String authlist)
126         {
127                 NsaAuthenticator<NsaSimpleApiKey> NsaAuth = new SimpleAuthenticator ();
128                 if (authlist != null)
129                 {
130                         String authpair[] = authlist.split("\\|");
131                         for (String pair: authpair) {           
132                                         String lineid[] = pair.split(",");
133                                         String listauthid =  lineid[0]; 
134                                         String listauthpwd =  new String(Base64.decodeBase64(lineid[1]));
135                                         ((SimpleAuthenticator) NsaAuth).add(listauthid,listauthpwd);
136                     }
137                         
138                 }
139                 else if (authid != null)
140                 {
141                         ((SimpleAuthenticator) NsaAuth).add(authid,authpwd);
142                 }
143                 else
144                 {
145                         //add a default test account
146                         ((SimpleAuthenticator) NsaAuth).add("admin","collectorpasscode");
147                 }
148                 return NsaAuth;
149
150         }
151         
152
153         private static final long serialVersionUID = 1L;
154         private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class );
155 }