Merge "Add Unit Tests for GroupServlet"
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / InternalServlet.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24
25 package org.onap.dmaap.datarouter.provisioning;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.file.FileStore;
32 import java.nio.file.FileSystem;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.nio.file.StandardCopyOption;
37 import java.util.Properties;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
42 import org.json.JSONArray;
43 import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;
44 import org.onap.dmaap.datarouter.provisioning.beans.LogRecord;
45 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
46 import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;
47 import org.onap.dmaap.datarouter.provisioning.utils.DB;
48 import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader;
49 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
50
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
53
54 /**
55  * <p>
56  * This servlet handles requests to URLs under /internal on the provisioning server. These include:
57  * </p>
58  * <div class="contentContainer">
59  * <table class="packageSummary" border="0" cellpadding="3" cellspacing="0">
60  * <caption><span>URL Path Summary</span><span class="tabEnd">&nbsp;</span></caption>
61  * <tr>
62  * <th class="colFirst" width="15%">URL Path</th>
63  * <th class="colOne">Method</th>
64  * <th class="colLast">Purpose</th>
65  * </tr>
66  * <tr class="altColor">
67  * <td class="colFirst">/internal/prov</td>
68  * <td class="colOne">GET</td>
69  * <td class="colLast">used to GET a full JSON copy of the provisioning data.</td>
70  * </tr>
71  * <tr class="rowColor">
72  * <td class="colFirst">/internal/fetchProv</td>
73  * <td class="colOne">GET</td>
74  * <td class="colLast">used to signal to a standby POD that the provisioning data should be fetched from the active
75  * POD.</td>
76  * </tr>
77  * <tr class="altColor">
78  * <td class="colFirst" rowspan="2">/internal/logs</td>
79  * <td class="colOne">GET</td>
80  * <td class="colLast">used to GET an index of log files and individual logs for this provisioning server.</td>
81  * </tr>
82  * <tr class="altColor">
83  * <td class="colOne">POST</td>
84  * <td class="colLast">used to POST log files from the individual nodes to this provisioning server.</td>
85  * </tr>
86  * <tr class="rowColor">
87  * <td class="colFirst" rowspan="4">/internal/api</td>
88  * <td class="colOne">GET</td>
89  * <td class="colLast">used to GET an individual parameter value. The parameter name is specified by the path after
90  * /api/.</td>
91  * </tr>
92  * <tr class="rowColor">
93  * <td class="colOne">PUT</td>
94  * <td class="colLast">used to set an individual parameter value. The parameter name is specified by the path after
95  * /api/.</td>
96  * </tr>
97  * <tr class="rowColor">
98  * <td class="colOne">DELETE</td>
99  * <td class="colLast">used to remove an individual parameter value. The parameter name is specified by the path after
100  * /api/.</td>
101  * </tr>
102  * <tr class="rowColor">
103  * <td class="colOne">POST</td>
104  * <td class="colLast">used to create a new individual parameter value. The parameter name is specified by the path
105  * after /api/.</td>
106  * </tr>
107  * <tr class="altColor">
108  * <td class="colFirst">/internal/halt</td>
109  * <td class="colOne">GET</td>
110  * <td class="colLast">used to halt the server (must be accessed from 127.0.0.1).</td>
111  * </tr>
112  * <tr class="rowColor">
113  * <td class="colFirst" rowspan="2">/internal/drlogs</td>
114  * <td class="colOne">GET</td>
115  * <td class="colLast">used to get a list of DR log entries available for retrieval.
116  * Note: these are the actual data router log entries sent to the provisioning server by the nodes, not the provisioning
117  * server's internal logs (access via /internal/logs above). The range is returned as a list of record sequence
118  * numbers.</td>
119  * </tr>
120  * <tr class="rowColor">
121  * <td class="colOne">POST</td>
122  * <td class="colLast">used to retrieve specific log entries.
123  * The sequence numbers of the records to fetch are POST-ed; the records matching the sequence numbers are
124  * returned.</td>
125  * </tr>
126  * <tr class="altColor">
127  * <td class="colFirst">/internal/route/*</td>
128  * <td class="colOne">*</td>
129  * <td class="colLast">URLs under this path are handled via the {@link org.onap.dmaap.datarouter.provisioning.RouteServlet}</td>
130  * </tr>
131  * </table>
132  * </div>
133  * <p>
134  * Authorization to use these URLs is a little different than for other URLs on the provisioning server. For the most
135  * part, the IP address that the request comes from should be either:
136  * </p>
137  * <ol>
138  * <li>an IP address of a provisioning server, or</li>
139  * <li>the IP address of a node (to allow access to /internal/prov), or</li>
140  * <li>an IP address from the "<i>special subnet</i>" which is configured with
141  * the PROV_SPECIAL_SUBNET parameter.
142  * </ol>
143  * <p>
144  * In addition, requests to /internal/halt can ONLY come from localhost (127.0.0.1) on the HTTP port.
145  * </p>
146  * <p>
147  * All DELETE/GET/PUT/POST requests made to /internal/api on this servlet on the standby server are proxied to the
148  * active server (using the {@link ProxyServlet}) if it is up and reachable.
149  * </p>
150  *
151  * @author Robert Eby
152  * @version $Id: InternalServlet.java,v 1.23 2014/03/24 18:47:10 eby Exp $
153  */
154 @SuppressWarnings("serial")
155 public class InternalServlet extends ProxyServlet {
156
157     private static Integer logseq = 0; // another piece of info to make log spool file names unique
158     //Adding EELF Logger Rally:US664892
159     private static EELFLogger eelflogger = EELFManager.getInstance()
160         .getLogger("org.onap.dmaap.datarouter.provisioning.InternalServlet");
161
162     /**
163      * Delete a parameter at the address /internal/api/&lt;parameter&gt;. See the <b>Internal API</b> document for
164      * details on how this method should be invoked.
165      */
166     @Override
167     public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
168         setIpAndFqdnForEelf("doDelete");
169         eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
170         EventLogRecord elr = new EventLogRecord(req);
171         if (!isAuthorizedForInternal(req)) {
172             elr.setMessage("Unauthorized.");
173             elr.setResult(HttpServletResponse.SC_FORBIDDEN);
174             eventlogger.info(elr);
175             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
176             return;
177         }
178
179         String path = req.getPathInfo();
180         if (path.startsWith("/api/")) {
181             if (isProxyOK(req) && isProxyServer()) {
182                 super.doDelete(req, resp);
183                 return;
184             }
185             String key = path.substring(5);
186             if (key.length() > 0) {
187                 Parameters param = Parameters.getParameter(key);
188                 if (param != null) {
189                     if (doDelete(param)) {
190                         elr.setResult(HttpServletResponse.SC_OK);
191                         eventlogger.info(elr);
192                         resp.setStatus(HttpServletResponse.SC_OK);
193                         provisioningDataChanged();
194                         provisioningParametersChanged();
195                     } else {
196                         // Something went wrong with the DELETE
197                         elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
198                         eventlogger.info(elr);
199                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
200                     }
201                     return;
202                 }
203             }
204         }
205         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
206     }
207
208     /**
209      * Get some information (such as a parameter) underneath the /internal/ namespace. See the <b>Internal API</b>
210      * document for details on how this method should be invoked.
211      */
212     @Override
213     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
214         setIpAndFqdnForEelf("doGet");
215         eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
216         String path = req.getPathInfo();
217         if (path.equals("/halt") && !req.isSecure()) {
218             // request to halt the server - can ONLY come from localhost
219             String remote = req.getRemoteAddr();
220             if (remote.equals("127.0.0.1")) {
221                 intlogger.info("PROV0009 Request to HALT received.");
222                 resp.setStatus(HttpServletResponse.SC_OK);
223                 Main.shutdown();
224             } else {
225                 intlogger.info("PROV0010 Disallowed request to HALT received from " + remote);
226                 resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
227             }
228             return;
229         }
230
231         EventLogRecord elr = new EventLogRecord(req);
232         if (!isAuthorizedForInternal(req)) {
233             elr.setMessage("Unauthorized.");
234             elr.setResult(HttpServletResponse.SC_FORBIDDEN);
235             eventlogger.info(elr);
236             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
237             return;
238         }
239         if (path.equals("/fetchProv") && !req.isSecure()) {
240             // if request came from active_pod or standby_pod and it is not us, reload prov data
241             SynchronizerTask s = SynchronizerTask.getSynchronizer();
242             s.doFetch();
243             resp.setStatus(HttpServletResponse.SC_OK);
244             return;
245         }
246         if (path.equals("/prov")) {
247             if (isProxyOK(req) && isProxyServer()) {
248                 if (super.doGetWithFallback(req, resp)) {
249                     return;
250                 }
251                 // fall back to returning the local data if the remote is unreachable
252                 intlogger.info("Active server unavailable; falling back to local copy.");
253             }
254             Poker p = Poker.getPoker();
255             resp.setStatus(HttpServletResponse.SC_OK);
256             resp.setContentType(PROVFULL_CONTENT_TYPE2);
257             resp.getOutputStream().print(p.getProvisioningString());
258             return;
259         }
260         if (path.equals("/logs") || path.equals("/logs/")) {
261             resp.setStatus(HttpServletResponse.SC_OK);
262             resp.setContentType("application/json");
263             resp.getOutputStream().print(generateLogfileList().toString());
264             return;
265         }
266         if (path.startsWith("/logs/")) {
267             Properties p = (new DB()).getProperties();
268             String logdir = p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir");
269             String logfile = path.substring(6);
270             if (logdir != null && logfile != null && logfile.indexOf('/') < 0) {
271                 File log = new File(logdir + "/" + logfile);
272                 if (log.exists() && log.isFile()) {
273                     resp.setStatus(HttpServletResponse.SC_OK);
274                     resp.setContentType("text/plain");
275                     Path logpath = Paths.get(log.getAbsolutePath());
276                     Files.copy(logpath, resp.getOutputStream());
277                     return;
278                 }
279             }
280             resp.sendError(HttpServletResponse.SC_NO_CONTENT, "No file.");
281             return;
282         }
283         if (path.startsWith("/api/")) {
284             if (isProxyOK(req) && isProxyServer()) {
285                 super.doGet(req, resp);
286                 return;
287             }
288             String key = path.substring(5);
289             if (key.length() > 0) {
290                 Parameters param = Parameters.getParameter(key);
291                 if (param != null) {
292                     resp.setStatus(HttpServletResponse.SC_OK);
293                     resp.setContentType("text/plain");
294                     resp.getOutputStream().print(param.getValue() + "\n");
295                     return;
296                 }
297             }
298         }
299         if (path.equals("/drlogs") || path.equals("/drlogs/")) {
300             // Special POD <=> POD API to determine what log file records are loaded here
301             LogfileLoader lfl = LogfileLoader.getLoader();
302             resp.setStatus(HttpServletResponse.SC_OK);
303             resp.setContentType("text/plain");
304             resp.getOutputStream().print(lfl.getBitSet().toString());
305             return;
306         }
307         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
308     }
309
310     /**
311      * Modify a parameter at the address /internal/api/&lt;parameter&gt;. See the <b>Internal API</b> document for
312      * details on how this method should be invoked.
313      */
314     @Override
315     public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
316         setIpAndFqdnForEelf("doPut");
317         eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
318         EventLogRecord elr = new EventLogRecord(req);
319         if (!isAuthorizedForInternal(req)) {
320             elr.setMessage("Unauthorized.");
321             elr.setResult(HttpServletResponse.SC_FORBIDDEN);
322             eventlogger.info(elr);
323             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
324             return;
325         }
326         String path = req.getPathInfo();
327         if (path.startsWith("/api/")) {
328             if (isProxyOK(req) && isProxyServer()) {
329                 super.doPut(req, resp);
330                 return;
331             }
332             String key = path.substring(5);
333             if (key.length() > 0) {
334                 Parameters param = Parameters.getParameter(key);
335                 if (param != null) {
336                     String t = catValues(req.getParameterValues("val"));
337                     param.setValue(t);
338                     if (doUpdate(param)) {
339                         elr.setResult(HttpServletResponse.SC_OK);
340                         eventlogger.info(elr);
341                         resp.setStatus(HttpServletResponse.SC_OK);
342                         provisioningDataChanged();
343                         provisioningParametersChanged();
344                     } else {
345                         // Something went wrong with the UPDATE
346                         elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
347                         eventlogger.info(elr);
348                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
349                     }
350                     return;
351                 }
352             }
353         }
354         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
355     }
356
357     /**
358      * Create some new information (such as a parameter or log entries) underneath the /internal/ namespace. See the
359      * <b>Internal API</b> document for details on how this method should be invoked.
360      */
361     @SuppressWarnings("resource")
362     @Override
363     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
364         setIpAndFqdnForEelf("doPost");
365         eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));
366         EventLogRecord elr = new EventLogRecord(req);
367         if (!isAuthorizedForInternal(req)) {
368             elr.setMessage("Unauthorized.");
369             elr.setResult(HttpServletResponse.SC_FORBIDDEN);
370             eventlogger.info(elr);
371             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unauthorized.");
372             return;
373         }
374
375         String path = req.getPathInfo();
376         if (path.startsWith("/api/")) {
377             if (isProxyOK(req) && isProxyServer()) {
378                 super.doPost(req, resp);
379                 return;
380             }
381             String key = path.substring(5);
382             if (key.length() > 0) {
383                 Parameters param = Parameters.getParameter(key);
384                 if (param == null) {
385                     String t = catValues(req.getParameterValues("val"));
386                     param = new Parameters(key, t);
387                     if (doInsert(param)) {
388                         elr.setResult(HttpServletResponse.SC_OK);
389                         eventlogger.info(elr);
390                         resp.setStatus(HttpServletResponse.SC_OK);
391                         provisioningDataChanged();
392                         provisioningParametersChanged();
393                     } else {
394                         // Something went wrong with the INSERT
395                         elr.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
396                         eventlogger.info(elr);
397                         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG);
398                     }
399                     return;
400                 }
401             }
402         }
403
404         if (path.equals("/logs") || path.equals("/logs/")) {
405             String ctype = req.getHeader("Content-Type");
406             if (ctype == null || !ctype.equals("text/plain")) {
407                 elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
408                 elr.setMessage("Bad media type: " + ctype);
409                 resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
410                 eventlogger.info(elr);
411                 return;
412             }
413             String spooldir = (new DB()).getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir");
414             String spoolname = String.format("%d-%d-", System.currentTimeMillis(), Thread.currentThread().getId());
415             synchronized (logseq) {
416                 // perhaps unnecessary, but it helps make the name unique
417                 spoolname += logseq.toString();
418                 logseq++;
419             }
420             String encoding = req.getHeader("Content-Encoding");
421             if (encoding != null) {
422                 if (encoding.trim().equals("gzip")) {
423                     spoolname += ".gz";
424                 } else {
425                     elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
426                     resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
427                     eventlogger.info(elr);
428                     return;
429                 }
430             }
431             // Determine space available -- available space must be at least 5%
432             FileSystem fs = (Paths.get(spooldir)).getFileSystem();
433             long total = 0;
434             long avail = 0;
435             for (FileStore store : fs.getFileStores()) {
436                 total += store.getTotalSpace();
437                 avail += store.getUsableSpace();
438             }
439             try {
440                 fs.close();
441             } catch (Exception e) {
442             }
443             if (((avail * 100) / total) < 5) {
444                 elr.setResult(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
445                 resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
446                 eventlogger.info(elr);
447                 return;
448             }
449             Path tmppath = Paths.get(spooldir, spoolname);
450             Path donepath = Paths.get(spooldir, "IN." + spoolname);
451             Files.copy(req.getInputStream(), Paths.get(spooldir, spoolname), StandardCopyOption.REPLACE_EXISTING);
452             Files.move(tmppath, donepath, StandardCopyOption.REPLACE_EXISTING);
453             elr.setResult(HttpServletResponse.SC_CREATED);
454             resp.setStatus(HttpServletResponse.SC_CREATED);
455             eventlogger.info(elr);
456             LogfileLoader.getLoader();    // This starts the logfile loader "task"
457             return;
458         }
459
460         if (path.equals("/drlogs") || path.equals("/drlogs/")) {
461             // Receive post request and generate log entries
462             String ctype = req.getHeader("Content-Type");
463             if (ctype == null || !ctype.equals("text/plain")) {
464                 elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
465                 elr.setMessage("Bad media type: " + ctype);
466                 resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
467                 eventlogger.info(elr);
468                 return;
469             }
470             InputStream is = req.getInputStream();
471             ByteArrayOutputStream bos = new ByteArrayOutputStream();
472             int ch = 0;
473             while ((ch = is.read()) >= 0) {
474                 bos.write(ch);
475             }
476             RLEBitSet bs = new RLEBitSet(bos.toString());    // The set of records to retrieve
477             elr.setResult(HttpServletResponse.SC_OK);
478             resp.setStatus(HttpServletResponse.SC_OK);
479             resp.setContentType("text/plain");
480             LogRecord.printLogRecords(resp.getOutputStream(), bs);
481             eventlogger.info(elr);
482             return;
483         }
484
485         elr.setResult(HttpServletResponse.SC_NOT_FOUND);
486         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
487         eventlogger.info(elr);
488     }
489
490     private String catValues(String[] v) {
491         StringBuilder sb = new StringBuilder();
492         if (v != null) {
493             String pfx = "";
494             for (String s : v) {
495                 sb.append(pfx);
496                 sb.append(s);
497                 pfx = "|";
498             }
499         }
500         return sb.toString();
501     }
502
503     private JSONArray generateLogfileList() {
504         JSONArray ja = new JSONArray();
505         Properties p = (new DB()).getProperties();
506         String s = p.getProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir");
507         if (s != null) {
508             String[] dirs = s.split(",");
509             for (String dir : dirs) {
510                 File f = new File(dir);
511                 String[] list = f.list();
512                 if (list != null) {
513                     for (String s2 : list) {
514                         if (!s2.startsWith(".")) {
515                             ja.put(s2);
516                         }
517                     }
518                 }
519             }
520         }
521         return ja;
522     }
523 }