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;
38 import org.oransc.ics.controllers.ErrorResponse;
39 import org.oransc.ics.controllers.VoidResponse;
40 import org.oransc.ics.controllers.r1consumer.ConsumerConsts;
41 import org.oransc.ics.controllers.r1producer.ProducerConsts;
42 import org.oransc.ics.controllers.r1producer.ProducerJobInfo;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.MediaType;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.web.bind.annotation.DeleteMapping;
49 import org.springframework.web.bind.annotation.GetMapping;
50 import org.springframework.web.bind.annotation.PathVariable;
51 import org.springframework.web.bind.annotation.PostMapping;
52 import org.springframework.web.bind.annotation.RequestBody;
53 import org.springframework.web.bind.annotation.RequestHeader;
54 import org.springframework.web.bind.annotation.RestController;
56 @RestController("ProducerSimulatorController")
57 @Tag(name = ProducerConsts.PRODUCER_API_CALLBACKS_NAME, description = ProducerConsts.PRODUCER_API_CALLBACKS_DESCRIPTION)
58 public class ProducerSimulatorController {
60 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62 public static final String JOB_URL = "/example-dataproducer/info-job";
63 public static final String JOB_ERROR_URL = "/example-dataproducer/info-job-error";
65 public static final String SUPERVISION_URL = "/example-dataproducer/health-check";
66 public static final String SUPERVISION_ERROR_URL = "/example-dataproducer/health-check-error";
68 public static class TestResults {
70 public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
71 public List<String> jobsStopped = Collections.synchronizedList(new ArrayList<String>());
72 public List<Map<String, String>> receivedHeaders =
73 Collections.synchronizedList(new ArrayList<Map<String, String>>());
74 public int noOfRejectedCreate = 0;
75 public int noOfRejectedDelete = 0;
76 public boolean errorFound = false;
78 public TestResults() {
84 receivedHeaders.clear();
85 this.errorFound = false;
86 this.noOfRejectedCreate = 0;
87 this.noOfRejectedDelete = 0;
92 private TestResults testResults = new TestResults();
94 @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
96 summary = "Callback for Information Job creation/modification",
97 description = "The call is invoked to activate or to modify a data subscription. The endpoint is provided by the Information Producer.")
101 responseCode = "200",
102 description = "OK", //
103 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
105 public ResponseEntity<Object> jobCreatedCallback( //
106 @RequestHeader Map<String, String> headers, //
107 @RequestBody ProducerJobInfo request) {
110 this.testResults.jobsStarted.add(request);
111 this.testResults.receivedHeaders.add(headers);
112 logger.info("Job started callback {}", request.id);
113 if (request.id == null) {
114 throw new NullPointerException("Illegal argument");
116 return new ResponseEntity<>(HttpStatus.OK);
117 } catch (Exception e) {
118 this.testResults.errorFound = true;
119 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
123 @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
125 summary = "Callback for Information Job deletion",
126 description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
130 responseCode = "200",
131 description = "OK", //
132 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
134 public ResponseEntity<Object> jobDeletedCallback( //
135 @RequestHeader Map<String, String> headers, @PathVariable(ConsumerConsts.INFO_JOB_ID_PATH) String infoJobId) {
138 logger.info("Job deleted callback {}", infoJobId);
139 this.testResults.jobsStopped.add(infoJobId);
140 this.testResults.receivedHeaders.add(headers);
141 return new ResponseEntity<>(HttpStatus.OK);
142 } catch (Exception e) {
143 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
147 @PostMapping(path = JOB_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
148 @Operation(summary = "Callback for Information Job creation, returns error", description = "", hidden = true)
152 responseCode = "200",
153 description = "OK", //
154 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
156 public ResponseEntity<Object> jobCreatedCallbackReturnError( //
157 @RequestBody ProducerJobInfo request) {
158 logger.info("Job created (returning error) callback {}", request.id);
159 this.testResults.noOfRejectedCreate += 1;
160 return ErrorResponse.create("Producer returns error on create job", HttpStatus.NOT_FOUND);
163 @DeleteMapping(path = JOB_ERROR_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
164 @Operation(summary = "Callback for Information Job deletion, returns error", description = "", hidden = true)
168 responseCode = "200",
169 description = "OK", //
170 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
172 public ResponseEntity<Object> jobDeletedCallbackReturnError( //
173 @PathVariable(ConsumerConsts.INFO_JOB_ID_PATH) String infoJobId) {
174 logger.info("Job created (returning error) callback {}", infoJobId);
175 this.testResults.noOfRejectedDelete += 1;
176 return ErrorResponse.create("Producer returns error on delete job", HttpStatus.NOT_FOUND);
179 @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
181 summary = "Producer supervision",
182 description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
186 responseCode = "200",
187 description = "The producer is OK", //
188 content = @Content(schema = @Schema(implementation = String.class))) //
190 public ResponseEntity<Object> producerSupervision() {
191 logger.info("Producer supervision");
192 return new ResponseEntity<>("Hunky dory", HttpStatus.OK);
195 @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
196 @Operation(summary = "Producer supervision error", description = "", hidden = true)
200 responseCode = "200",
201 description = "OK", //
202 content = @Content(schema = @Schema(implementation = String.class))) //
204 public ResponseEntity<Object> producerSupervisionError() {
205 logger.info("Producer supervision error");
206 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
209 private void logHeaders(Map<String, String> headers) {
210 logger.debug("Header begin");
211 headers.forEach((key, value) -> logger.debug(" key: {}, value: {}", key, value));
212 logger.debug("Header end");