2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2020-2021 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.dcae.restapi;
24 import org.json.JSONObject;
25 import org.onap.dcae.ApplicationSettings;
26 import org.onap.dcae.common.EventSender;
27 import org.onap.dcae.common.EventUpdater;
28 import org.onap.dcae.common.HeaderUtils;
29 import org.onap.dcae.common.model.BackwardsCompatibilityException;
30 import org.onap.dcae.common.model.InternalException;
31 import org.onap.dcae.common.model.PayloadToLargeException;
32 import org.onap.dcae.common.model.StndDefinedNamespaceParameterHasEmptyValueException;
33 import org.onap.dcae.common.model.StndDefinedNamespaceParameterNotDefinedException;
34 import org.onap.dcae.common.model.VesEvent;
35 import org.onap.dcae.common.validator.GeneralEventValidator;
36 import org.onap.dcae.common.validator.StndDefinedDataValidator;
37 import org.onap.dcaegen2.services.sdk.standardization.header.CustomHeaderUtils;
38 import org.slf4j.Logger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Qualifier;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.MediaType;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RestController;
49 import javax.servlet.http.HttpServletRequest;
50 import java.util.List;
51 import java.util.UUID;
53 import static org.onap.dcae.common.validator.BatchEventValidator.executeBatchEventValidation;
54 import static org.springframework.http.ResponseEntity.badRequest;
55 import static org.springframework.http.ResponseEntity.status;
58 public class VesRestController {
60 private static final String VES_EVENT_MESSAGE = "Received a VESEvent '%s', marked with unique identifier '%s', on api version '%s', from host: '%s'";
61 private static final String EVENT_LIST = "eventList";
62 private static final String EVENT = "event";
63 private final ApplicationSettings settings;
64 private final Logger requestLogger;
65 private final Logger logger;
66 private EventSender eventSender;
67 private final HeaderUtils headerUtils;
68 private final GeneralEventValidator generalEventValidator;
69 private final EventUpdater eventUpdater;
70 private final StndDefinedDataValidator stndDefinedValidator;
73 VesRestController(ApplicationSettings settings, @Qualifier("incomingRequestsLogger") Logger incomingRequestsLogger,
74 @Qualifier("errorLog") Logger logger, @Qualifier("eventSender") EventSender eventSender, HeaderUtils headerUtils,
75 StndDefinedDataValidator stndDefinedDataValidator) {
76 this.settings = settings;
77 this.requestLogger = incomingRequestsLogger;
79 this.eventSender = eventSender;
80 this.headerUtils = headerUtils;
81 this.stndDefinedValidator = stndDefinedDataValidator;
82 this.generalEventValidator = new GeneralEventValidator(settings);
83 this.eventUpdater = new EventUpdater(settings);
86 @PostMapping(value = {"/eventListener/{version}"}, consumes = "application/json")
87 ResponseEntity<String> event(@RequestBody String event, @PathVariable String version, HttpServletRequest request) {
88 if (settings.isVersionSupported(version)) {
89 return process(event, version, request, EVENT);
91 return badRequest().contentType(MediaType.APPLICATION_JSON).body(String.format("API version %s is not supported", version));
94 @PostMapping(value = {"/eventListener/{version}/eventBatch"}, consumes = "application/json")
95 ResponseEntity<String> events(@RequestBody String events, @PathVariable String version, HttpServletRequest request) {
96 if (settings.isVersionSupported(version)) {
97 return process(events, version, request, EVENT_LIST);
99 return badRequest().contentType(MediaType.APPLICATION_JSON).body(String.format("API version %s is not supported", version));
102 private ResponseEntity<String> process(String payload, String version, HttpServletRequest request, String type) {
103 CustomHeaderUtils headerUtils = createHeaderUtils(version, request);
104 if (headerUtils.isOkCustomHeaders()) {
105 final VesEvent vesEvent = new VesEvent(new JSONObject(payload));
106 final String requestURI = request.getRequestURI();
107 return handleEvent(vesEvent, version, type, headerUtils, requestURI);
109 return badRequest().body(ApiException.INVALID_CUSTOM_HEADER.toString());
112 private ResponseEntity<String> handleEvent(VesEvent vesEvent, String version, String type, CustomHeaderUtils headerUtils, String requestURI) {
114 generalEventValidator.validate(vesEvent, type, version);
115 List<VesEvent> vesEvents = transformEvent(vesEvent, type, version, requestURI);
116 executeStndDefinedValidation(vesEvents);
117 executeBatchEventValidation(vesEvents);
118 HttpStatus httpStatus = eventSender.send(vesEvents);
119 return status(httpStatus).contentType(MediaType.APPLICATION_JSON).body("Successfully send event");
120 } catch (EventValidatorException e) {
121 logger.error(e.getMessage());
122 return status(e.getApiException().httpStatusCode)
123 .body(e.getApiException().toJSON().toString());
124 } catch (StndDefinedNamespaceParameterNotDefinedException e) {
125 return status(ApiException.MISSING_NAMESPACE_PARAMETER.httpStatusCode)
126 .body(ApiException.MISSING_NAMESPACE_PARAMETER.toJSON().toString());
127 } catch (StndDefinedNamespaceParameterHasEmptyValueException e) {
128 return status(ApiException.MISSING_NAMESPACE_PARAMETER.httpStatusCode)
129 .body(ApiException.EMPTY_NAMESPACE_PARAMETER.toJSON().toString());
130 } catch (InternalException e) {
131 return status(ApiException.SERVICE_UNAVAILABLE.httpStatusCode)
132 .body(e.getApiException().toJSON().toString());
133 } catch (PayloadToLargeException e) {
134 return status(ApiException.PAYLOAD_TO_LARGE.httpStatusCode)
135 .body(ApiException.PAYLOAD_TO_LARGE.toJSON().toString());
136 } catch (BackwardsCompatibilityException e) {
137 return status(ApiException.INTERNAL_SERVER_ERROR.httpStatusCode)
138 .body(ApiException.INTERNAL_SERVER_ERROR.toJSON().toString());
142 private void executeStndDefinedValidation(List<VesEvent> vesEvents) {
143 if (settings.getExternalSchemaValidationCheckflag()) {
144 vesEvents.forEach(stndDefinedValidator::validate);
148 private CustomHeaderUtils createHeaderUtils(String version, HttpServletRequest request) {
149 return new CustomHeaderUtils(version.toLowerCase().replace("v", ""),
150 headerUtils.extractHeaders(request),
151 settings.getApiVersionDescriptionFilepath(),
152 headerUtils.getRestApiIdentify(request.getRequestURI()));
155 private List<VesEvent> transformEvent(VesEvent vesEvent, String type, String version, String requestURI) {
156 return this.eventUpdater.convert(vesEvent, version, generateUUID(vesEvent, version, requestURI), type);
159 private UUID generateUUID(VesEvent vesEvent, String version, String uri) {
160 UUID uuid = UUID.randomUUID();
161 requestLogger.info(String.format(VES_EVENT_MESSAGE, vesEvent.asJsonObject(), uuid, version, uri));