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