summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/service/EventsRestService.java
blob: d3abd6b1484f469a958e4b0e9d4c19b906b7f388 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  org.onap.dmaap
 *  ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
*  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============LICENSE_END=========================================================
 *  
 *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *  
 *******************************************************************************/
 package org.onap.dmaap.service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.apache.http.HttpStatus;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import org.onap.dmaap.dmf.mr.CambriaApiException;
import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;
import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
import org.onap.dmaap.dmf.mr.service.EventsService;
import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
import org.onap.dmaap.dmf.mr.utils.Utils;
import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
/**
 * This class is a CXF REST service which acts 
 * as gateway for MR Event Service.
 * @author rajashree.khare
 *
 */
@Component
@Path("/")
public class EventsRestService {

	/**
	 * Logger obj
	 */
	//private Logger log = Logger.getLogger(EventsRestService.class.toString());
	private static final EELFLogger log = EELFManager.getInstance().getLogger(EventsRestService.class);
	/**
	 * HttpServletRequest obj
	 */
	@Context
	private HttpServletRequest request;

	/**
	 * HttpServletResponse obj
	 */
	@Context
	private HttpServletResponse response;


	/**
	 * Config Reader
	 */
	@Autowired
	@Qualifier("configurationReader")
	private ConfigurationReader configReader;

	@Autowired
	private EventsService eventsService;

	@Autowired
	private DMaaPErrorMessages errorMessages;
	

	/**
	 * This method is used to consume messages.Taking three parameter
	 * topic,consumerGroup and consumerId .Consumer decide to which topic they
	 * want to consume messages.In on consumer Group there might be many
	 * consumer may be present.
	 * 
	 * @param topic
	 *            specify- the topic name
	 * @param consumergroup
	 *            - specify the consumer group
	 * @param consumerid
	 *            -specify the consumer id
	 * 
	 *            handles CambriaApiException | ConfigDbException |
	 *            TopicExistsException | AccessDeniedException |
	 *            UnavailableException | IOException in try catch block
	 * @throws CambriaApiException
	 * 
	 */
	@GET
	@Path("/{topic}/{consumergroup}/{consumerid}")
	public void getEvents(@PathParam("topic") String topic, @PathParam("consumergroup") 
	String consumergroup,
			@PathParam("consumerid") String consumerid) throws CambriaApiException {
		// log.info("Consuming message from topic " + topic );
		DMaaPContext dMaaPContext = getDmaapContext();
		dMaaPContext.setConsumerRequestTime(Utils.getFormattedDate(new Date()));

		try {

			eventsService.getEvents(dMaaPContext, topic, consumergroup, consumerid);
		} 
		catch (TopicExistsException  e) {
			log.error("Error while reading data from topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_CONFLICT,
					DMaaPResponseCode.CONSUME_MSG_ERROR.getResponseCode(), errorMessages.getConsumeMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic, null, null, 
							consumerid,
					request.getRemoteHost());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);

		}
		catch (DMaaPAccessDeniedException | AccessDeniedException  e) {
			log.error("Error while reading data from topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
					DMaaPResponseCode.CONSUME_MSG_ERROR.getResponseCode(), errorMessages.getConsumeMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic, null, null, 
							consumerid,
					request.getRemoteHost());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);

		}
		
		catch (ConfigDbException | UnavailableException | IOException e) {
			log.error("Error while reading data from topic [" + topic + "].", e);
		
			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,
					DMaaPResponseCode.CONSUME_MSG_ERROR.getResponseCode(), errorMessages.getConsumeMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic, null, null, 
							consumerid,
					request.getRemoteHost());
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);

		}
	}
	
	
	/**
	 * This method is used to throw an exception back to the client app if CG/CID is not passed
	 *  while consuming messages
	 */
	@GET
	@Path("/{topic}")
	public void getEventsToException(@PathParam("topic") String topic) throws CambriaApiException {
		// log.info("Consuming message from topic " + topic );
		DMaaPContext dMaaPContext = getDmaapContext();
		dMaaPContext.setConsumerRequestTime(Utils.getFormattedDate(new Date()));

		try {

			throw new TopicExistsException("Incorrect URL");
		} 
		catch (TopicExistsException  e) {
			log.error("Error while reading data from topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,
					DMaaPResponseCode.CONSUME_MSG_ERROR.getResponseCode(), "Incorrect url - Expects consumer Group and ID in " + request.getRequestURI() + " from "+request.getRemoteHost()
					);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);

		}
		
	}
	
	/**
	 * This method is used to throw an exception back to the client app if CG/CID is not passed
	 *  while consuming messages
	 */
	@GET
	@Path("/{topic}/{consumergroup}")
	public void getEventsToException(@PathParam("topic") String topic, @PathParam("consumergroup") 
	String consumergroup
			) throws CambriaApiException {
		// log.info("Consuming message from topic " + topic );
		DMaaPContext dMaaPContext = getDmaapContext();
		dMaaPContext.setConsumerRequestTime(Utils.getFormattedDate(new Date()));

		try {

			throw new TopicExistsException("Incorrect URL");
		} 
		catch (TopicExistsException  e) {
			log.error("Error while reading data from topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,
					DMaaPResponseCode.CONSUME_MSG_ERROR.getResponseCode(), "Incorrect url - Expects consumer ID in " + request.getRequestURI() + " from "+request.getRemoteHost()
					);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);

		}
		
	}
	
	
	
	
	
	

	/**
	 * This method is used to publish messages.Taking two parameter topic and
	 * partition.Publisher decide to which topic they want to publish message
	 * and kafka decide to which partition of topic message will send,
	 * 
	 * @param topic
	 * @param msg
	 * @param partitionKey
	 * 
	 *            handles CambriaApiException | ConfigDbException |
	 *            TopicExistsException | AccessDeniedException | IOException in
	 *            try catch block
	 * @throws CambriaApiException
	 */

	@POST
	@Produces("application/json")
	@Path("/{topic}")
	public void pushEvents(@PathParam("topic") String topic, InputStream msg,
			@QueryParam("partitionKey") String partitionKey) throws CambriaApiException {
		log.info("Publishing message to topic " + topic);
            
		try {
			eventsService.pushEvents(getDmaapContext(), topic, msg, partitionKey, null);
		} 
		catch ( TopicExistsException  e) {
			log.error("Error while publishing to topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_CONFLICT,
					DMaaPResponseCode.PUBLISH_MSG_ERROR.getResponseCode(), errorMessages.getPublishMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic,
					Utils.getUserApiKey(request), request.getRemoteHost(), null, null);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}
		catch ( DMaaPAccessDeniedException | AccessDeniedException  e) {
			log.error("Error while publishing to topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
					DMaaPResponseCode.PUBLISH_MSG_ERROR.getResponseCode(), errorMessages.getPublishMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic,
					Utils.getUserApiKey(request), request.getRemoteHost(), null, null);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}
		
		
		catch (ConfigDbException |   IOException | missingReqdSetting e) {
			log.error("Error while publishing to topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,
					DMaaPResponseCode.PUBLISH_MSG_ERROR.getResponseCode(), errorMessages.getPublishMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic,
					Utils.getUserApiKey(request), request.getRemoteHost(), null, null);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}
	}

	/**
	 * This method is used to publish messages by passing an optional header
	 * called 'transactionId'. If the 'transactionId' is not provided in the
	 * input then a new transaction object will be created. Else the existing
	 * transaction object will be updated with the counter details.
	 * 
	 * @param topic
	 * @param partitionKey
	 * 
	 *            handles CambriaApiException | ConfigDbException |
	 *            TopicExistsException | AccessDeniedException | IOException in
	 *            try catch block
	 * @throws CambriaApiException
	 */
	@POST
	@Produces("application/json")
	@Path("/transaction/{topic}")
	public void pushEventsWithTransaction(@PathParam("topic") String topic,
			@QueryParam("partitionKey") String partitionKey) throws CambriaApiException {
		// log.info("Publishing message with transaction id for topic " + topic
		// );
		

		try {
			
			eventsService.pushEvents(getDmaapContext(), topic, request.getInputStream(), 
					partitionKey,
					Utils.getFormattedDate(new Date()));
		} 
		
		catch ( TopicExistsException  e) {
			log.error("Error while publishing to topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_CONFLICT,
					DMaaPResponseCode.PUBLISH_MSG_ERROR.getResponseCode(), errorMessages.getPublishMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic,
					Utils.getUserApiKey(request), request.getRemoteHost(), null, null);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}
		catch ( DMaaPAccessDeniedException| AccessDeniedException  e) {
			log.error("Error while publishing to topic [" + topic + "].", e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
					DMaaPResponseCode.PUBLISH_MSG_ERROR.getResponseCode(), errorMessages.getPublishMsgError()
							+ e.getMessage(), null, Utils.getFormattedDate(new Date()), topic,
					Utils.getUserApiKey(request), request.getRemoteHost(), null, null);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);
		}
		
		catch (ConfigDbException  | IOException | missingReqdSetting  e) {
			log.error("Error while publishing to topic : " + topic, e);

			ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND,
					DMaaPResponseCode.PUBLISH_MSG_ERROR.getResponseCode(), "Transaction-"
							+ errorMessages.getPublishMsgError() + e.getMessage(), null,
					Utils.getFormattedDate(new Date()), topic, Utils.getUserApiKey(request), 
					request.getRemoteHost(),
					null, null);
			log.info(errRes.toString());
			throw new CambriaApiException(errRes);

		}
	}

	/**
	 * This method is used for taking Configuration Object,HttpServletRequest
	 * Object,HttpServletRequest HttpServletResponse Object,HttpServletSession
	 * Object.
	 * 
	 * @return DMaaPContext object from where user can get Configuration
	 *         Object,HttpServlet Object
	 * 
	 */
	private DMaaPContext getDmaapContext() {

		DMaaPContext dmaapContext = new DMaaPContext();
		dmaapContext.setRequest(request);
		dmaapContext.setResponse(response);
		dmaapContext.setConfigReader(configReader);

		return dmaapContext;
	}
	

}