aboutsummaryrefslogtreecommitdiffstats
path: root/dcae_dmaapbc_webapp/dbca-common/src/main/java/org/openecomp/dcae/dmaapbc/dbcapp/controller/MessageRouterController.java
blob: 9d889ddecdb4156963d5fb6dcd7a4cfce6023a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package org.openecomp.dcae.dmaapbc.dbcapp.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.slf4j.MDC;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Message Router controller: serves Ajax requests made by Angular scripts on
 * pages that show topics and clients.
 */
@Controller
@RequestMapping("/")
public class MessageRouterController extends DbcappRestrictedBaseController {

	private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MessageRouterController.class);

	private static final String TOPIC_PATH = "/mr_topic";
	private static final String CLIENT_PATH = "/mr_client";

	public MessageRouterController() {
	}

	/**
	 * Answers a request for one page of message router topics. See
	 * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return One page of MR topics
	 */
	@RequestMapping(value = { TOPIC_PATH }, method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public String getMRTopicsByPage(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = getItemListForPageWrapper(request, DmaapDataItem.MR_TOPIC);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Answers a request for one page of message router clients. See
	 * {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return One page of MR clients
	 */
	@RequestMapping(value = { CLIENT_PATH }, method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public String getMRClientsByPage(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = getItemListForPageWrapper(request, DmaapDataItem.MR_CLIENT);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Adds a topic with the specified information. Expects a JSON block in the
	 * request body - a Topic object.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return JSON success/failure response
	 */
	@RequestMapping(value = { TOPIC_PATH }, method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public String addTopic(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = addItem(request, DmaapDataItem.MR_TOPIC, HttpServletResponse.SC_CREATED);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Adds a client with the specified information. Expects a JSON block in the
	 * request body - a MR_Client object.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return JSON success/failure response
	 */
	@RequestMapping(value = { CLIENT_PATH }, method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public String addMRClient(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = addItem(request, DmaapDataItem.MR_CLIENT, HttpServletResponse.SC_CREATED);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Updates a topic with the specified information. Expects a JSON block in
	 * the request body - a Topic object.
	 * 
	 * Writes a JSON object as an HTTP response; on success it has a "status"
	 * and possibly a "data" item; on failure, also has an "error" item.
	 * 
	 * @param id
	 *            ID of the topic to update
	 * @param request
	 *            HttpServletRequest
	 * @return JSON success/failure response
	 */
	@RequestMapping(value = { TOPIC_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
	@ResponseBody
	public String updateTopic(@PathVariable("id") long id, HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = updateItem(request, DmaapDataItem.MR_TOPIC, Long.toString(id), null);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Updates a client with the specified information. Expects a JSON block in
	 * the request body - a MR_Client object.
	 * 
	 * Writes a JSON object as an HTTP response; on success it has a "status"
	 * and possibly a "data" item; on failure, also has an "error" item.
	 * 
	 * @param id
	 *            ID of the client to update
	 * @param request
	 *            HttpServletRequest
	 * @return JSON success/failure response
	 */
	@RequestMapping(value = { CLIENT_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
	@ResponseBody
	public String updateMRClient(@PathVariable("id") long id, HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = updateItem(request, DmaapDataItem.MR_CLIENT, Long.toString(id), null);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Deletes a topic with the FQTN ID specified as a path parameter.
	 * 
	 * FQTN is a string of dot-separated names. Spring, in its infinite wisdom,
	 * truncates extensions on dotted path parameters; e.g., "foo.json" becomes
	 * "foo". Avoid truncation here with the extra ":.+" incantation at the end.
	 * 
	 * Writes a JSON object as an HTTP response; on success it only has "status"
	 * item; on failure, also has an "error" item.
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return JSON success/failure response
	 */
	@RequestMapping(value = { "/mr_topic/{id:.+}" }, method = RequestMethod.DELETE, produces = "application/json")
	@ResponseBody
	public String deleteTopic(@PathVariable("id") String id, HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = deleteItem(request, DmaapDataItem.MR_TOPIC, id, 204);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Deletes a client with the mrClientId specified as a path parameter.
	 * 
	 * Writes a JSON object as an HTTP response; on success it only has "status"
	 * item; on failure, also has an "error" item.
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return JSON success/failure response
	 */
	@RequestMapping(value = { "/mr_client/{id}" }, method = RequestMethod.DELETE, produces = "application/json")
	@ResponseBody
	public String deleteMRClient(@PathVariable("id") long id, HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = deleteItem(request, DmaapDataItem.MR_CLIENT, Long.toString(id), null);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

}