c2fbb26c45f5df79c1733a273a08657dbe31e646
[ui/dmaapbc.git] /
1 package org.onap.dcae.dmaapbc.dbcapp.controller;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.List;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.onap.dcae.dmaapbc.dbcapp.util.DbcappProperties;
11 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
12 import org.openecomp.portalsdk.core.util.SystemProperties;
13 import org.slf4j.MDC;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Controller;
16 import org.springframework.web.bind.annotation.PathVariable;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.ResponseBody;
20
21 /**
22  * Data Router controller: serves Ajax requests made by Angular scripts on pages
23  * that show feeds, publishers and subscribers.
24  */
25 @Controller
26 @RequestMapping("/")
27 public class DataRouterController extends DbcappRestrictedBaseController {
28
29         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DataRouterController.class);
30
31         /**
32          * Application properties - NOT available to constructor.
33          */
34         @Autowired
35         private DbcappProperties appProperties;
36
37         private static final String FEED_PATH = "/dr_feed";
38         private static final String PUB_PATH = "/dr_pub";
39         private static final String SUB_PATH = "/dr_sub";
40
41         public DataRouterController() {
42         }
43
44         /**
45          * Answers a request for one page of data router feeds.
46          * 
47          * @param request
48          *            HttpServletRequest
49          * @return Result of
50          *         {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
51          */
52         @RequestMapping(value = { FEED_PATH }, method = RequestMethod.GET, produces = "application/json")
53         @ResponseBody
54         public String getDRFeedsByPage(HttpServletRequest request) {
55                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
56                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
57                 String response = getItemListForPageWrapper(request, DmaapDataItem.DR_FEED);
58                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
59                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
60                 return response;
61         }
62
63         /**
64          * Answers a request for one page of data router publishers.
65          * 
66          * @param request
67          *            HttpServletRequest
68          * @return Result of
69          *         {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
70          */
71         @RequestMapping(value = { PUB_PATH }, method = RequestMethod.GET, produces = "application/json")
72         @ResponseBody
73         public String getDRPubsByPage(HttpServletRequest request) {
74                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
75                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
76                 String response = getItemListForPageWrapper(request, DmaapDataItem.DR_PUB);
77                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
78                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
79                 return response;
80         }
81
82         /**
83          * Answers a request for one page of data router subscribers.
84          * 
85          * @param request
86          *            HttpServletRequest
87          * @return Result of
88          *         {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
89          */
90         @RequestMapping(value = { SUB_PATH }, method = RequestMethod.GET, produces = "application/json")
91         @ResponseBody
92         public String getDRSubsByPage(HttpServletRequest request) {
93                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
94                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
95                 String response = getItemListForPageWrapper(request, DmaapDataItem.DR_SUB);
96                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
97                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
98                 return response;
99         }
100
101         /**
102          * Adds a feed with the specified information. Expects a JSON block in the
103          * request body - a Feed object.
104          * 
105          * @param request
106          *            HttpServletRequest
107          * @return a JSON object; on success it has a "status" and possibly a "data"
108          *         item; on failure, also has an "error" item.
109          */
110         @RequestMapping(value = { FEED_PATH }, method = RequestMethod.POST, produces = "application/json")
111         @ResponseBody
112         public String addDRFeed(HttpServletRequest request) {
113                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
114                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
115                 String response = addItem(request, DmaapDataItem.DR_FEED, null);
116                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
117                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
118                 return response;
119         }
120
121         /**
122          * Adds a publisher with the specified information. Expects a JSON block in
123          * the request body - a DR_Pub object.
124          * 
125          * @param request
126          *            HttpServletRequest
127          * @return a JSON object; on success it has a "status" and possibly a "data"
128          *         item; on failure, also has an "error" item.
129          */
130         @RequestMapping(value = { PUB_PATH }, method = RequestMethod.POST, produces = "application/json")
131         @ResponseBody
132         public String addDRPub(HttpServletRequest request) {
133                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
134                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
135                 String response = addItem(request, DmaapDataItem.DR_PUB, HttpServletResponse.SC_CREATED);
136                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
137                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
138                 return response;
139         }
140
141         /**
142          * Adds a subscriber with the specified information. Expects a JSON block in
143          * the request body - a DR_Sub object.
144          * 
145          * @param request
146          *            HttpServletRequest
147          * @return a JSON object; on success it has a "status" and possibly a "data"
148          *         item; on failure, also has an "error" item.
149          */
150         @RequestMapping(value = { SUB_PATH }, method = RequestMethod.POST, produces = "application/json")
151         @ResponseBody
152         public String addDRSub(HttpServletRequest request) {
153                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
154                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
155                 String response = addItem(request, DmaapDataItem.DR_SUB, HttpServletResponse.SC_CREATED);
156                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
157                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
158                 return response;
159         }
160
161         /**
162          * Updates a feed with the specified information. Expects a JSON block in
163          * the request body - a Feed object.
164          * 
165          * @param id
166          *            Path parameter with object ID
167          * @param request
168          *            HttpServletRequest
169          * @return a JSON object; on success it has a "status" and possibly a "data"
170          *         item; on failure, also has an "error" item.
171          */
172         @RequestMapping(value = { FEED_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
173         @ResponseBody
174         public String updateFeed(@PathVariable("id") long id, HttpServletRequest request) {
175                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
176                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
177                 String response = updateItem(request, DmaapDataItem.DR_FEED, Long.toString(id), null);
178                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
179                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
180                 return response;
181         }
182
183         /**
184          * Updates a publisher with the specified information. Expects a JSON block
185          * in the request body - a DR_Pub object.
186          * 
187          * The pubId may have a dot in it. Spring, in its infinite wisdom, truncates
188          * extensions on dotted path parameters; e.g., "foo.json" becomes "foo".
189          * Avoid truncation here with the extra ":.+" incantation at the end.
190          * 
191          * @param id
192          *            Path parameter with object ID
193          * @param request
194          *            HttpServletRequest
195          * @return a JSON object; on success it has a "status" and possibly a "data"
196          *         item; on failure, also has an "error" item.
197          */
198         @RequestMapping(value = { PUB_PATH + "/{id:.+}" }, method = RequestMethod.PUT, produces = "application/json")
199         @ResponseBody
200         public String updateDRPub(@PathVariable("id") String id, HttpServletRequest request) {
201                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
202                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
203                 String response = updateItem(request, DmaapDataItem.DR_PUB, id, null);
204                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
205                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
206                 return response;
207         }
208
209         /**
210          * Updates a subscriber with the specified information. Expects a JSON block
211          * in the request body - a DR_Sub object.
212          * 
213          * @param id
214          *            Path parameter with object ID
215          * @param request
216          *            HttpServletRequest
217          * @return a JSON object; on success it has a "status" and possibly a "data"
218          *         item; on failure, also has an "error" item.
219          */
220         @RequestMapping(value = { SUB_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
221         @ResponseBody
222         public String updateDRSub(@PathVariable("id") long id, HttpServletRequest request) {
223                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
224                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
225                 String response = updateItem(request, DmaapDataItem.DR_SUB, Long.toString(id), null);
226                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
227                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
228                 return response;
229         }
230
231         /**
232          * Deletes a feed with the ID specified as a path parameter. On successful
233          * delete the endpoint returns 204 (confusingly).
234          * 
235          * @param id
236          *            Path parameter with object ID
237          * @param request
238          *            HttpServletRequest
239          * @return A JSON object as an HTTP response; on success it only has
240          *         "status" item; on failure, also has an "error" item.
241          */
242         @RequestMapping(value = { FEED_PATH + "/{id}" }, method = RequestMethod.DELETE, produces = "application/json")
243         @ResponseBody
244         public String deleteDRFeed(@PathVariable("id") long id, HttpServletRequest request) {
245                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
246                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
247                 String response = deleteItem(request, DmaapDataItem.DR_FEED, Long.toString(id), 204);
248                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
249                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
250                 return response;
251         }
252
253         /**
254          * Deletes the publisher with the ID specified as a path parameter.
255          * 
256          * The pubId may have a dot in it. Spring, in its infinite wisdom, truncates
257          * extensions on dotted path parameters; e.g., "foo.json" becomes "foo".
258          * Avoid truncation here with the extra ":.+" incantation at the end.
259          * 
260          * @param id
261          *            Path parameter with object ID
262          * @param request
263          *            HttpServletRequest
264          * @return a JSON object; on success it only has "status" item; on failure,
265          *         also has an "error" item.
266          */
267         @RequestMapping(value = { PUB_PATH + "/{id:.+}" }, method = RequestMethod.DELETE, produces = "application/json")
268         @ResponseBody
269         public String deleteDRPub(@PathVariable("id") String id, HttpServletRequest request) {
270                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
271                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
272                 String response = deleteItem(request, DmaapDataItem.DR_PUB, id, null);
273                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
274                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
275                 return response;
276         }
277
278         /**
279          * Deletes the subscriber with the ID specified as a path parameter.
280          * 
281          * @param id
282          *            Path parameter with object ID
283          * @param request
284          *            HttpServletRequest
285          * @return A JSON object; on success it only has "status" item; on failure,
286          *         also has an "error" item.
287          */
288         @RequestMapping(value = { SUB_PATH + "/{id}" }, method = RequestMethod.DELETE, produces = "application/json")
289         @ResponseBody
290         public String deleteDRSub(@PathVariable("id") long id, HttpServletRequest request) {
291                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
292                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
293                 String response = deleteItem(request, DmaapDataItem.DR_SUB, Long.toString(id), 204);
294                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
295                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
296                 return response;
297         }
298
299         /**
300          * Answers a request for data router feed classification labels, such as
301          * "Customer Proprietary", which are read from properties.
302          * 
303          * @param request
304          *            HttpServletRequest
305          * @return List of string values
306          */
307         @RequestMapping(value = { FEED_PATH + "_pii_types" }, method = RequestMethod.GET, produces = "application/json")
308         @ResponseBody
309         public List<String> getDRFeedPiiType(HttpServletRequest request) {
310                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
311                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
312                 ArrayList<String> response = new ArrayList<>();
313                 String[] propKeys = appProperties.getCsvListProperty(DbcappProperties.DMAAP_PII_TYPE_LIST);
314                 for (String key : propKeys) 
315                         response.add(appProperties.getProperty(key));
316                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
317                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
318                 return response;
319         }
320
321 }