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.ics.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.ics.controllers.ErrorResponse;
38 import org.oransc.ics.controllers.VoidResponse;
39 import org.oransc.ics.controllers.r1consumer.ConsumerConsts;
40 import org.oransc.ics.controllers.r1producer.ProducerConsts;
41 import org.oransc.ics.controllers.r1producer.ProducerJobInfo;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.MediaType;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.web.bind.annotation.DeleteMapping;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PathVariable;
50 import org.springframework.web.bind.annotation.PostMapping;
51 import org.springframework.web.bind.annotation.RequestBody;
52 import org.springframework.web.bind.annotation.RestController;
54 @RestController("ProducerSimulatorController")
55 @Tag(name = ProducerConsts.PRODUCER_API_CALLBACKS_NAME, description = ProducerConsts.PRODUCER_API_CALLBACKS_DESCRIPTION)
56 public class ProducerSimulatorController {
58 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 public static final String JOB_URL = "/example-dataproducer/info-job";
61 public static final String JOB_ERROR_URL = "/example-dataproducer/info-job-error";
63 public static final String SUPERVISION_URL = "/example-dataproducer/health-check";
64 public static final String SUPERVISION_ERROR_URL = "/example-dataproducer/health-check-error";
66 public static class TestResults {
68 public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
69 public List<String> jobsStopped = Collections.synchronizedList(new ArrayList<String>());
70 public int noOfRejectedCreate = 0;
71 public int noOfRejectedDelete = 0;
72 public boolean errorFound = false;
74 public TestResults() {
80 this.errorFound = false;
81 this.noOfRejectedCreate = 0;
82 this.noOfRejectedDelete = 0;
87 private TestResults testResults = new TestResults();
89 @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
91 summary = "Callback for Information Job creation/modification",
92 description = "The call is invoked to activate or to modify a data subscription. The endpoint is provided by the Information Producer.")
97 description = "OK", //
98 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
100 public ResponseEntity<Object> jobCreatedCallback( //
101 @RequestBody ProducerJobInfo request) {
103 this.testResults.jobsStarted.add(request);
104 logger.info("Job started callback {}", request.id);
105 if (request.id == null) {
106 throw new NullPointerException("Illegal argument");
108 return new ResponseEntity<>(HttpStatus.OK);
109 } catch (Exception e) {
110 this.testResults.errorFound = true;
111 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
115 @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
117 summary = "Callback for Information Job deletion",
118 description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
122 responseCode = "200",
123 description = "OK", //
124 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
126 public ResponseEntity<Object> jobDeletedCallback( //
127 @PathVariable(ConsumerConsts.INFO_JOB_ID_PATH) String infoJobId) {
129 logger.info("Job deleted callback {}", infoJobId);
130 this.testResults.jobsStopped.add(infoJobId);
131 return new ResponseEntity<>(HttpStatus.OK);
132 } catch (Exception e) {
133 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
137 @PostMapping(path = JOB_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
138 @Operation(summary = "Callback for Information Job creation, returns error", description = "", hidden = true)
142 responseCode = "200",
143 description = "OK", //
144 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
146 public ResponseEntity<Object> jobCreatedCallbackReturnError( //
147 @RequestBody ProducerJobInfo request) {
148 logger.info("Job created (returning error) callback {}", request.id);
149 this.testResults.noOfRejectedCreate += 1;
150 return ErrorResponse.create("Producer returns error on create job", HttpStatus.NOT_FOUND);
153 @DeleteMapping(path = JOB_ERROR_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
154 @Operation(summary = "Callback for Information Job deletion, returns error", description = "", hidden = true)
158 responseCode = "200",
159 description = "OK", //
160 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
162 public ResponseEntity<Object> jobDeletedCallbackReturnError( //
163 @PathVariable(ConsumerConsts.INFO_JOB_ID_PATH) String infoJobId) {
164 logger.info("Job created (returning error) callback {}", infoJobId);
165 this.testResults.noOfRejectedDelete += 1;
166 return ErrorResponse.create("Producer returns error on delete job", HttpStatus.NOT_FOUND);
169 @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
171 summary = "Producer supervision",
172 description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
176 responseCode = "200",
177 description = "The producer is OK", //
178 content = @Content(schema = @Schema(implementation = String.class))) //
180 public ResponseEntity<Object> producerSupervision() {
181 logger.info("Producer supervision");
182 return new ResponseEntity<>(HttpStatus.OK);
185 @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
186 @Operation(summary = "Producer supervision error", description = "", hidden = true)
190 responseCode = "200",
191 description = "OK", //
192 content = @Content(schema = @Schema(implementation = String.class))) //
194 public ResponseEntity<Object> producerSupervisionError() {
195 logger.info("Producer supervision error");
196 return new ResponseEntity<>(HttpStatus.NOT_FOUND);