1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.provisioning;
27 import java.io.ByteArrayOutputStream;
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;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
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;
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
56 * This servlet handles requests to URLs under /internal on the provisioning server. These include:
58 * <div class="contentContainer">
59 * <table class="packageSummary" border="0" cellpadding="3" cellspacing="0">
60 * <caption><span>URL Path Summary</span><span class="tabEnd"> </span></caption>
62 * <th class="colFirst" width="15%">URL Path</th>
63 * <th class="colOne">Method</th>
64 * <th class="colLast">Purpose</th>
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>
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
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>
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>
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
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
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
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
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>
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
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
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>
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:
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.
144 * In addition, requests to /internal/halt can ONLY come from localhost (127.0.0.1) on the HTTP port.
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.
152 * @version $Id: InternalServlet.java,v 1.23 2014/03/24 18:47:10 eby Exp $
154 @SuppressWarnings("serial")
155 public class InternalServlet extends ProxyServlet {
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");
163 * Delete a parameter at the address /internal/api/<parameter>. See the <b>Internal API</b> document for
164 * details on how this method should be invoked.
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.");
179 String path = req.getPathInfo();
180 if (path.startsWith("/api/")) {
181 if (isProxyOK(req) && isProxyServer()) {
182 super.doDelete(req, resp);
185 String key = path.substring(5);
186 if (key.length() > 0) {
187 Parameters param = Parameters.getParameter(key);
189 if (doDelete(param)) {
190 elr.setResult(HttpServletResponse.SC_OK);
191 eventlogger.info(elr);
192 resp.setStatus(HttpServletResponse.SC_OK);
193 provisioningDataChanged();
194 provisioningParametersChanged();
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);
205 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
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.
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);
225 intlogger.info("PROV0010 Disallowed request to HALT received from " + remote);
226 resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
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.");
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();
243 resp.setStatus(HttpServletResponse.SC_OK);
246 if (path.equals("/prov")) {
247 if (isProxyOK(req) && isProxyServer()) {
248 if (super.doGetWithFallback(req, resp)) {
251 // fall back to returning the local data if the remote is unreachable
252 intlogger.info("Active server unavailable; falling back to local copy.");
254 Poker p = Poker.getPoker();
255 resp.setStatus(HttpServletResponse.SC_OK);
256 resp.setContentType(PROVFULL_CONTENT_TYPE2);
257 resp.getOutputStream().print(p.getProvisioningString());
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());
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());
280 resp.sendError(HttpServletResponse.SC_NO_CONTENT, "No file.");
283 if (path.startsWith("/api/")) {
284 if (isProxyOK(req) && isProxyServer()) {
285 super.doGet(req, resp);
288 String key = path.substring(5);
289 if (key.length() > 0) {
290 Parameters param = Parameters.getParameter(key);
292 resp.setStatus(HttpServletResponse.SC_OK);
293 resp.setContentType("text/plain");
294 resp.getOutputStream().print(param.getValue() + "\n");
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());
307 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
311 * Modify a parameter at the address /internal/api/<parameter>. See the <b>Internal API</b> document for
312 * details on how this method should be invoked.
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.");
326 String path = req.getPathInfo();
327 if (path.startsWith("/api/")) {
328 if (isProxyOK(req) && isProxyServer()) {
329 super.doPut(req, resp);
332 String key = path.substring(5);
333 if (key.length() > 0) {
334 Parameters param = Parameters.getParameter(key);
336 String t = catValues(req.getParameterValues("val"));
338 if (doUpdate(param)) {
339 elr.setResult(HttpServletResponse.SC_OK);
340 eventlogger.info(elr);
341 resp.setStatus(HttpServletResponse.SC_OK);
342 provisioningDataChanged();
343 provisioningParametersChanged();
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);
354 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
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.
361 @SuppressWarnings("resource")
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.");
375 String path = req.getPathInfo();
376 if (path.startsWith("/api/")) {
377 if (isProxyOK(req) && isProxyServer()) {
378 super.doPost(req, resp);
381 String key = path.substring(5);
382 if (key.length() > 0) {
383 Parameters param = Parameters.getParameter(key);
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();
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);
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);
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();
420 String encoding = req.getHeader("Content-Encoding");
421 if (encoding != null) {
422 if (encoding.trim().equals("gzip")) {
425 elr.setResult(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
426 resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
427 eventlogger.info(elr);
431 // Determine space available -- available space must be at least 5%
432 FileSystem fs = (Paths.get(spooldir)).getFileSystem();
435 for (FileStore store : fs.getFileStores()) {
436 total += store.getTotalSpace();
437 avail += store.getUsableSpace();
441 } catch (Exception e) {
443 if (((avail * 100) / total) < 5) {
444 elr.setResult(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
445 resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
446 eventlogger.info(elr);
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"
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);
470 InputStream is = req.getInputStream();
471 ByteArrayOutputStream bos = new ByteArrayOutputStream();
473 while ((ch = is.read()) >= 0) {
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);
485 elr.setResult(HttpServletResponse.SC_NOT_FOUND);
486 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Bad URL.");
487 eventlogger.info(elr);
490 private String catValues(String[] v) {
491 StringBuilder sb = new StringBuilder();
500 return sb.toString();
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");
508 String[] dirs = s.split(",");
509 for (String dir : dirs) {
510 File f = new File(dir);
511 String[] list = f.list();
513 for (String s2 : list) {
514 if (!s2.startsWith(".")) {