aboutsummaryrefslogtreecommitdiffstats
path: root/dcae_dmaapbc_webapp/dbca-common/src/main/java/org/openecomp/dcae/dmaapbc/dbcapp/controller/DataRouterController.java
blob: cb6aa9385f1b0e8eb742eef064b7d22ac6b07aa8 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package org.openecomp.dcae.dmaapbc.dbcapp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import org.openecomp.dcae.dmaapbc.dbcapp.util.DbcappProperties;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * Data Router controller: serves Ajax requests made by Angular scripts on pages
 * that show feeds, publishers and subscribers.
 */
@Controller
@RequestMapping("/")
public class DataRouterController extends DbcappRestrictedBaseController {

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

	/**
	 * Application properties - NOT available to constructor.
	 */
	@Autowired
	private DbcappProperties appProperties;

	private static final String FEED_PATH = "/dr_feed";
	private static final String PUB_PATH = "/dr_pub";
	private static final String SUB_PATH = "/dr_sub";

	public DataRouterController() {
	}

	/**
	 * Answers a request for one page of data router feeds.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return Result of
	 *         {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
	 */
	@RequestMapping(value = { FEED_PATH }, method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public String getDRFeedsByPage(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = getItemListForPageWrapper(request, DmaapDataItem.DR_FEED);
		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 data router publishers.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return Result of
	 *         {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
	 */
	@RequestMapping(value = { PUB_PATH }, method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public String getDRPubsByPage(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = getItemListForPageWrapper(request, DmaapDataItem.DR_PUB);
		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 data router subscribers.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return Result of
	 *         {@link #getItemListForPageWrapper(HttpServletRequest, DmaapDataItem)}
	 */
	@RequestMapping(value = { SUB_PATH }, method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public String getDRSubsByPage(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = getItemListForPageWrapper(request, DmaapDataItem.DR_SUB);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Adds a feed with the specified information. Expects a JSON block in the
	 * request body - a Feed object.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return a JSON object; on success it has a "status" and possibly a "data"
	 *         item; on failure, also has an "error" item.
	 */
	@RequestMapping(value = { FEED_PATH }, method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public String addDRFeed(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = addItem(request, DmaapDataItem.DR_FEED, null);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Adds a publisher with the specified information. Expects a JSON block in
	 * the request body - a DR_Pub object.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return a JSON object; on success it has a "status" and possibly a "data"
	 *         item; on failure, also has an "error" item.
	 */
	@RequestMapping(value = { PUB_PATH }, method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public String addDRPub(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = addItem(request, DmaapDataItem.DR_PUB, 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 subscriber with the specified information. Expects a JSON block in
	 * the request body - a DR_Sub object.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return a JSON object; on success it has a "status" and possibly a "data"
	 *         item; on failure, also has an "error" item.
	 */
	@RequestMapping(value = { SUB_PATH }, method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public String addDRSub(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		String response = addItem(request, DmaapDataItem.DR_SUB, 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 feed with the specified information. Expects a JSON block in
	 * the request body - a Feed object.
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return a JSON object; on success it has a "status" and possibly a "data"
	 *         item; on failure, also has an "error" item.
	 */
	@RequestMapping(value = { FEED_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
	@ResponseBody
	public String updateFeed(@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.DR_FEED, 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 publisher with the specified information. Expects a JSON block
	 * in the request body - a DR_Pub object.
	 * 
	 * The pubId may have a dot in it. 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.
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return a JSON object; on success it has a "status" and possibly a "data"
	 *         item; on failure, also has an "error" item.
	 */
	@RequestMapping(value = { PUB_PATH + "/{id:.+}" }, method = RequestMethod.PUT, produces = "application/json")
	@ResponseBody
	public String updateDRPub(@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 = updateItem(request, DmaapDataItem.DR_PUB, 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 subscriber with the specified information. Expects a JSON block
	 * in the request body - a DR_Sub object.
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return a JSON object; on success it has a "status" and possibly a "data"
	 *         item; on failure, also has an "error" item.
	 */
	@RequestMapping(value = { SUB_PATH + "/{id}" }, method = RequestMethod.PUT, produces = "application/json")
	@ResponseBody
	public String updateDRSub(@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.DR_SUB, 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 feed with the ID specified as a path parameter. On successful
	 * delete the endpoint returns 204 (confusingly).
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return A JSON object as an HTTP response; on success it only has
	 *         "status" item; on failure, also has an "error" item.
	 */
	@RequestMapping(value = { FEED_PATH + "/{id}" }, method = RequestMethod.DELETE, produces = "application/json")
	@ResponseBody
	public String deleteDRFeed(@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.DR_FEED, Long.toString(id), 204);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Deletes the publisher with the ID specified as a path parameter.
	 * 
	 * The pubId may have a dot in it. 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.
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return a JSON object; on success it only has "status" item; on failure,
	 *         also has an "error" item.
	 */
	@RequestMapping(value = { PUB_PATH + "/{id:.+}" }, method = RequestMethod.DELETE, produces = "application/json")
	@ResponseBody
	public String deleteDRPub(@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.DR_PUB, id, null);
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

	/**
	 * Deletes the subscriber with the ID specified as a path parameter.
	 * 
	 * @param id
	 *            Path parameter with object ID
	 * @param request
	 *            HttpServletRequest
	 * @return A JSON object; on success it only has "status" item; on failure,
	 *         also has an "error" item.
	 */
	@RequestMapping(value = { SUB_PATH + "/{id}" }, method = RequestMethod.DELETE, produces = "application/json")
	@ResponseBody
	public String deleteDRSub(@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.DR_SUB, Long.toString(id), 204);
		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 data router feed classification labels, such as
	 * "Customer Proprietary", which are read from properties.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return List of string values
	 */
	@RequestMapping(value = { FEED_PATH + "_pii_types" }, method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public List<String> getDRFeedPiiType(HttpServletRequest request) {
		MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
		ArrayList<String> response = new ArrayList<>();
		String[] propKeys = appProperties.getCsvListProperty(DbcappProperties.DMAAP_PII_TYPE_LIST);
		for (String key : propKeys) 
			response.add(appProperties.getProperty(key));
		MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
		logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
		return response;
	}

}