2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2020 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 com.att.nsa.clock.SaClock;
25 import com.att.nsa.logging.LoggingContext;
26 import com.att.nsa.logging.log4j.EcompFields;
27 import org.json.JSONObject;
28 import org.onap.dcae.ApplicationSettings;
29 import org.onap.dcae.common.EventSender;
30 import org.onap.dcae.common.EventUpdater;
31 import org.onap.dcae.common.HeaderUtils;
32 import org.onap.dcae.common.validator.GeneralEventValidator;
33 import org.onap.dcae.common.validator.StndDefinedDataValidator;
34 import org.onap.dcae.common.VESLogger;
35 import org.onap.dcae.common.model.StndDefinedNamespaceParameterHasEmptyValueException;
36 import org.onap.dcae.common.model.StndDefinedNamespaceParameterNotDefinedException;
37 import org.onap.dcae.common.model.VesEvent;
38 import org.onap.dcaegen2.services.sdk.standardization.header.CustomHeaderUtils;
39 import org.slf4j.Logger;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.beans.factory.annotation.Qualifier;
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.springframework.http.ResponseEntity.accepted;
54 import static org.springframework.http.ResponseEntity.badRequest;
57 public class VesRestController {
59 private static final String VES_EVENT_MESSAGE = "Received a VESEvent '%s', marked with unique identifier '%s', on api version '%s', from host: '%s'";
60 private static final String EVENT_LIST = "eventList";
61 private static final String EVENT = "event";
62 private final ApplicationSettings settings;
63 private final Logger requestLogger;
64 private EventSender eventSender;
65 private final HeaderUtils headerUtils;
66 private final GeneralEventValidator generalEventValidator;
67 private final EventUpdater eventUpdater;
68 private final StndDefinedDataValidator stndDefinedValidator;
71 VesRestController(ApplicationSettings settings, @Qualifier("incomingRequestsLogger") Logger incomingRequestsLogger,
72 @Qualifier("eventSender") EventSender eventSender, HeaderUtils headerUtils,
73 StndDefinedDataValidator stndDefinedDataValidator) {
74 this.settings = settings;
75 this.requestLogger = incomingRequestsLogger;
76 this.eventSender = eventSender;
77 this.headerUtils = headerUtils;
78 this.stndDefinedValidator = stndDefinedDataValidator;
79 this.generalEventValidator = new GeneralEventValidator(settings);
80 this.eventUpdater = new EventUpdater(settings);
83 @PostMapping(value = {"/eventListener/{version}"}, consumes = "application/json")
84 ResponseEntity<String> event(@RequestBody String event, @PathVariable String version, HttpServletRequest request) {
85 if (settings.isVersionSupported(version)) {
86 return process(event, version, request, EVENT);
88 return badRequest().contentType(MediaType.APPLICATION_JSON).body(String.format("API version %s is not supported", version));
91 @PostMapping(value = {"/eventListener/{version}/eventBatch"}, consumes = "application/json")
92 ResponseEntity<String> events(@RequestBody String events, @PathVariable String version, HttpServletRequest request) {
93 if (settings.isVersionSupported(version)) {
94 return process(events, version, request, EVENT_LIST);
96 return badRequest().contentType(MediaType.APPLICATION_JSON).body(String.format("API version %s is not supported", version));
99 private ResponseEntity<String> process(String payload, String version, HttpServletRequest request, String type) {
100 CustomHeaderUtils headerUtils = createHeaderUtils(version, request);
101 if (headerUtils.isOkCustomHeaders()) {
102 final VesEvent vesEvent = new VesEvent(new JSONObject(payload));
103 final String requestURI = request.getRequestURI();
104 return handleEvent(vesEvent, version, type, headerUtils, requestURI);
106 return badRequest().body(ApiException.INVALID_CUSTOM_HEADER.toString());
109 private ResponseEntity<String> handleEvent(VesEvent vesEvent, String version, String type, CustomHeaderUtils headerUtils, String requestURI) {
111 generalEventValidator.validate(vesEvent, type, version);
112 List<VesEvent> vesEvents = transformEvent(vesEvent, type, version, requestURI);
113 executeStndDefinedValidation(vesEvents);
114 eventSender.send(vesEvents);
115 } catch (EventValidatorException e) {
116 return ResponseEntity.status(e.getApiException().httpStatusCode)
117 .body(e.getApiException().toJSON().toString());
118 } catch (StndDefinedNamespaceParameterNotDefinedException e) {
119 return ResponseEntity.status(ApiException.MISSING_NAMESPACE_PARAMETER.httpStatusCode)
120 .body(ApiException.MISSING_NAMESPACE_PARAMETER.toJSON().toString());
121 } catch (StndDefinedNamespaceParameterHasEmptyValueException e) {
122 return ResponseEntity.status(ApiException.MISSING_NAMESPACE_PARAMETER.httpStatusCode)
123 .body(ApiException.EMPTY_NAMESPACE_PARAMETER.toJSON().toString());
126 // TODO call service and return status, replace CambriaClient, split event to single object and list of them
127 return accepted().headers(this.headerUtils.fillHeaders(headerUtils.getRspCustomHeader()))
128 .contentType(MediaType.APPLICATION_JSON).body("Accepted");
131 private void executeStndDefinedValidation(List<VesEvent> vesEvents) {
132 if (settings.getExternalSchemaValidationCheckflag()) {
133 vesEvents.forEach(stndDefinedValidator::validate);
137 private CustomHeaderUtils createHeaderUtils(String version, HttpServletRequest request) {
138 return new CustomHeaderUtils(version.toLowerCase().replace("v", ""),
139 headerUtils.extractHeaders(request),
140 settings.getApiVersionDescriptionFilepath(),
141 headerUtils.getRestApiIdentify(request.getRequestURI()));
145 private List<VesEvent> transformEvent(VesEvent vesEvent, String type, String version, String requestURI) {
146 return this.eventUpdater.convert(vesEvent, version, generateUUID(vesEvent, version, requestURI), type);
149 private UUID generateUUID(VesEvent vesEvent, String version, String uri) {
150 UUID uuid = UUID.randomUUID();
151 setUpECOMPLoggingForRequest(uuid);
152 requestLogger.info(String.format(VES_EVENT_MESSAGE, vesEvent.asJsonObject(), uuid, version, uri));
156 private static void setUpECOMPLoggingForRequest(UUID uuid) {
157 LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid);
158 localLC.put(EcompFields.kBeginTimestampMs, SaClock.now());