70bc90afd4ba654695cba4ed899ef92df82ea9e9
[ui/dmaapbc.git] /
1 package org.onap.dcae.dmaapbc.dbcapp.controller;
2
3 import java.util.Date;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
9 import org.openecomp.portalsdk.core.util.SystemProperties;
10 import org.slf4j.MDC;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.PathVariable;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RequestMethod;
15 import org.springframework.web.bind.annotation.ResponseBody;
16
17 /**
18  * Message Router controller: serves Ajax requests made by Angular scripts on
19  * pages that show topics and clients.
20  */
21 @Controller
22 @RequestMapping("/")
23 public class MessageRouterController extends DbcappRestrictedBaseController {
24
25         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MessageRouterController.class);
26
27         private static final String TOPIC_PATH = "/mr_topic";
28         private static final String CLIENT_PATH = "/mr_client";
29
30         public MessageRouterController() {
31         }
32
33         /**
34          * Answers a request for one page of message router topics. See
35          * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
36          * 
37          * @param request
38          *            HttpServletRequest
39          * @return One page of MR topics
40          */
41         @RequestMapping(value = { TOPIC_PATH }, method = RequestMethod.GET, produces = "application/json")
42         @ResponseBody
43         public String getMRTopicsByPage(HttpServletRequest request) {
44                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
45                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
46                 String response = getItemListForPageWrapper(request, DmaapDataItem.MR_TOPIC);
47                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
48                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
49                 return response;
50         }
51
52         /**
53          * Answers a request for one page of message router clients. See
54          * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
55          * 
56          * @param request
57          *            HttpServletRequest
58          * @return One page of MR clients
59          */
60         @RequestMapping(value = { CLIENT_PATH }, method = RequestMethod.GET, produces = "application/json")
61         @ResponseBody
62         public String getMRClientsByPage(HttpServletRequest request) {
63                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
64                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
65                 String response = getItemListForPageWrapper(request, DmaapDataItem.MR_CLIENT);
66                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
67                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
68                 return response;
69         }
70
71         /**
72          * Adds a topic with the specified information. Expects a JSON block in the
73          * request body - a Topic object.
74          * 
75          * @param request
76          *            HttpServletRequest
77          * @return JSON success/failure response
78          */
79         @RequestMapping(value = { TOPIC_PATH }, method = RequestMethod.POST, produces = "application/json")
80         @ResponseBody
81         public String addTopic(HttpServletRequest request) {
82                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
83                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
84                 String response = addItem(request, DmaapDataItem.MR_TOPIC, HttpServletResponse.SC_CREATED);
85                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
86                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
87                 return response;
88         }
89
90         /**
91          * Adds a client with the specified information. Expects a JSON block in the
92          * request body - a MR_Client object.
93          * 
94          * @param request
95          *            HttpServletRequest
96          * @return JSON success/failure response
97          */
98         @RequestMapping(value = { CLIENT_PATH }, method = RequestMethod.POST, produces = "application/json")
99         @ResponseBody
100         public String addMRClient(HttpServletRequest request) {
101                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
102                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
103                 String response = addItem(request, DmaapDataItem.MR_CLIENT, HttpServletResponse.SC_CREATED);
104                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
105                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
106                 return response;
107         }
108
109         /**
110          * Updates a topic with the specified information. Expects a JSON block in
111          * the request body - a Topic object.
112          * 
113          * Writes a JSON object as an HTTP response; on success it has a "status"
114          * and possibly a "data" item; on failure, also has an "error" item.
115          * 
116          * @param id
117          *            ID of the topic to update
118          * @param request
119          *            HttpServletRequest
120          * @return JSON success/failure response
121          */
122         @RequestMapping(value = { TOPIC_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
123         @ResponseBody
124         public String updateTopic(@PathVariable("id") long id, HttpServletRequest request) {
125                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
126                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
127                 String response = updateItem(request, DmaapDataItem.MR_TOPIC, Long.toString(id), null);
128                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
129                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
130                 return response;
131         }
132
133         /**
134          * Updates a client with the specified information. Expects a JSON block in
135          * the request body - a MR_Client object.
136          * 
137          * Writes a JSON object as an HTTP response; on success it has a "status"
138          * and possibly a "data" item; on failure, also has an "error" item.
139          * 
140          * @param id
141          *            ID of the client to update
142          * @param request
143          *            HttpServletRequest
144          * @return JSON success/failure response
145          */
146         @RequestMapping(value = { CLIENT_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
147         @ResponseBody
148         public String updateMRClient(@PathVariable("id") long id, HttpServletRequest request) {
149                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
150                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
151                 String response = updateItem(request, DmaapDataItem.MR_CLIENT, Long.toString(id), null);
152                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
153                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
154                 return response;
155         }
156
157         /**
158          * Deletes a topic with the FQTN ID specified as a path parameter.
159          * 
160          * FQTN is a string of dot-separated names. Spring, in its infinite wisdom,
161          * truncates extensions on dotted path parameters; e.g., "foo.json" becomes
162          * "foo". Avoid truncation here with the extra ":.+" incantation at the end.
163          * 
164          * Writes a JSON object as an HTTP response; on success it only has "status"
165          * item; on failure, also has an "error" item.
166          * 
167          * @param id
168          *            Path parameter with object ID
169          * @param request
170          *            HttpServletRequest
171          * @return JSON success/failure response
172          */
173         @RequestMapping(value = { "/mr_topic/{id:.+}" }, method = RequestMethod.DELETE, produces = "application/json")
174         @ResponseBody
175         public String deleteTopic(@PathVariable("id") String id, HttpServletRequest request) {
176                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
177                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
178                 String response = deleteItem(request, DmaapDataItem.MR_TOPIC, id, 204);
179                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
180                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
181                 return response;
182         }
183
184         /**
185          * Deletes a client with the mrClientId specified as a path parameter.
186          * 
187          * Writes a JSON object as an HTTP response; on success it only has "status"
188          * item; on failure, also has an "error" item.
189          * 
190          * @param id
191          *            Path parameter with object ID
192          * @param request
193          *            HttpServletRequest
194          * @return JSON success/failure response
195          */
196         @RequestMapping(value = { "/mr_client/{id}" }, method = RequestMethod.DELETE, produces = "application/json")
197         @ResponseBody
198         public String deleteMRClient(@PathVariable("id") long id, HttpServletRequest request) {
199                 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
200                 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
201                 String response = deleteItem(request, DmaapDataItem.MR_CLIENT, Long.toString(id), null);
202                 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
203                 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
204                 return response;
205         }
206
207 }