2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.enrichment.controller;
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.media.Content;
25 import io.swagger.v3.oas.annotations.media.Schema;
26 import io.swagger.v3.oas.annotations.responses.ApiResponse;
27 import io.swagger.v3.oas.annotations.responses.ApiResponses;
28 import io.swagger.v3.oas.annotations.tags.Tag;
30 import java.lang.invoke.MethodHandles;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
37 import org.oransc.enrichment.controllers.ErrorResponse;
38 import org.oransc.enrichment.controllers.VoidResponse;
39 import org.oransc.enrichment.controllers.r1producer.ProducerConsts;
40 import org.oransc.enrichment.controllers.r1producer.ProducerJobInfo;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.MediaType;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.DeleteMapping;
47 import org.springframework.web.bind.annotation.GetMapping;
48 import org.springframework.web.bind.annotation.PathVariable;
49 import org.springframework.web.bind.annotation.PostMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RestController;
53 @RestController("ProducerSimulatorController")
54 @Tag(name = ProducerConsts.PRODUCER_API_CALLBACKS_NAME)
55 public class ProducerSimulatorController {
57 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 public static final String JOB_URL = "/example_dataproducer/info_job";
60 public static final String JOB_ERROR_URL = "/example_dataproducer/info_job_error";
62 public static final String SUPERVISION_URL = "/example_dataproducer/health_check";
63 public static final String SUPERVISION_ERROR_URL = "/example_dataproducer/health_check_error";
65 public static class TestResults {
67 public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
68 public List<String> jobsStopped = Collections.synchronizedList(new ArrayList<String>());
69 public int noOfRejectedCreate = 0;
70 public int noOfRejectedDelete = 0;
71 public boolean errorFound = false;
73 public TestResults() {
79 this.errorFound = false;
80 this.noOfRejectedCreate = 0;
81 this.noOfRejectedDelete = 0;
86 private TestResults testResults = new TestResults();
88 @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
90 summary = "Callback for Information Job creation/modification",
91 description = "The call is invoked to activate or to modify a data subscription. The endpoint is provided by the Information Producer.")
96 description = "OK", //
97 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
99 public ResponseEntity<Object> jobCreatedCallback( //
100 @RequestBody ProducerJobInfo request) {
102 this.testResults.jobsStarted.add(request);
103 logger.info("Job started callback {}", request.id);
104 if (request.id == null) {
105 throw new NullPointerException("Illegal argument");
107 return new ResponseEntity<>(HttpStatus.OK);
108 } catch (Exception e) {
109 this.testResults.errorFound = true;
110 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
114 @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
116 summary = "Callback for Information Job deletion",
117 description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
121 responseCode = "200",
122 description = "OK", //
123 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
125 public ResponseEntity<Object> jobDeletedCallback( //
126 @PathVariable("infoJobId") String infoJobId) {
128 logger.info("Job deleted callback {}", infoJobId);
129 this.testResults.jobsStopped.add(infoJobId);
130 return new ResponseEntity<>(HttpStatus.OK);
131 } catch (Exception e) {
132 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
136 @PostMapping(path = JOB_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
137 @Operation(summary = "Callback for Information Job creation, returns error", description = "", hidden = true)
141 responseCode = "200",
142 description = "OK", //
143 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
145 public ResponseEntity<Object> jobCreatedCallbackReturnError( //
146 @RequestBody ProducerJobInfo request) {
147 logger.info("Job created (returning error) callback {}", request.id);
148 this.testResults.noOfRejectedCreate += 1;
149 return ErrorResponse.create("Producer returns error on create job", HttpStatus.NOT_FOUND);
152 @DeleteMapping(path = JOB_ERROR_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
153 @Operation(summary = "Callback for Information Job deletion, returns error", description = "", hidden = true)
157 responseCode = "200",
158 description = "OK", //
159 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
161 public ResponseEntity<Object> jobDeletedCallbackReturnError( //
162 @PathVariable("infoJobId") String infoJobId) {
163 logger.info("Job created (returning error) callback {}", infoJobId);
164 this.testResults.noOfRejectedDelete += 1;
165 return ErrorResponse.create("Producer returns error on delete job", HttpStatus.NOT_FOUND);
168 @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
170 summary = "Producer supervision",
171 description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
175 responseCode = "200",
176 description = "The producer is OK", //
177 content = @Content(schema = @Schema(implementation = String.class))) //
179 public ResponseEntity<Object> producerSupervision() {
180 logger.info("Producer supervision");
181 return new ResponseEntity<>(HttpStatus.OK);
184 @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
185 @Operation(summary = "Producer supervision error", description = "", hidden = true)
189 responseCode = "200",
190 description = "OK", //
191 content = @Content(schema = @Schema(implementation = String.class))) //
193 public ResponseEntity<Object> producerSupervisionError() {
194 logger.info("Producer supervision error");
195 return new ResponseEntity<>(HttpStatus.NOT_FOUND);