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.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
35 import org.oransc.enrichment.controllers.ErrorResponse;
36 import org.oransc.enrichment.controllers.VoidResponse;
37 import org.oransc.enrichment.controllers.producer.ProducerJobInfo;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RestController;
50 @RestController("ProducerSimulatorController")
51 @Api(tags = {"Producer Callbacks"})
52 public class ProducerSimulatorController {
54 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56 public static final String JOB_URL = "/producer_simulator/ei_job";
57 public static final String JOB_ERROR_URL = "/producer_simulator/ei_job_error";
59 public static final String SUPERVISION_URL = "/producer_simulator/health_check";
60 public static final String SUPERVISION_ERROR_URL = "/producer_simulator/health_check_error";
62 public static class TestResults {
64 public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
65 public List<String> jobsStopped = Collections.synchronizedList(new ArrayList<String>());
66 public int noOfRejectedCreate = 0;
67 public int noOfRejectedDelete = 0;
68 public boolean errorFound = false;
70 public TestResults() {
76 this.errorFound = false;
77 this.noOfRejectedCreate = 0;
78 this.noOfRejectedDelete = 0;
83 private TestResults testResults = new TestResults();
85 @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
86 @ApiOperation(value = "Callback for EI job creation", notes = "")
89 @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
91 public ResponseEntity<Object> jobCreatedCallback( //
92 @RequestBody ProducerJobInfo request) {
94 this.testResults.jobsStarted.add(request);
95 logger.info("Job started callback {}", request.id);
96 if (request.id == null) {
97 throw new NullPointerException("Illegal argument");
99 return new ResponseEntity<>(HttpStatus.OK);
100 } catch (Exception e) {
101 this.testResults.errorFound = true;
102 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
106 @DeleteMapping(path = "/producer_simulator/ei_job/{eiJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
107 @ApiOperation(value = "Callback for EI job deletion", notes = "")
110 @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
112 public ResponseEntity<Object> jobDeletedCallback( //
113 @PathVariable("eiJobId") String eiJobId) {
115 logger.info("Job deleted callback {}", eiJobId);
116 this.testResults.jobsStopped.add(eiJobId);
117 return new ResponseEntity<>(HttpStatus.OK);
118 } catch (Exception e) {
119 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
123 @PostMapping(path = JOB_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
124 @ApiOperation(value = "Callback for EI job creation, returns error", notes = "", hidden = true)
127 @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
129 public ResponseEntity<Object> jobCreatedCallbackReturnError( //
130 @RequestBody ProducerJobInfo request) {
131 logger.info("Job created (returning error) callback {}", request.id);
132 this.testResults.noOfRejectedCreate += 1;
133 return ErrorResponse.create("Producer returns error on create job", HttpStatus.NOT_FOUND);
136 @DeleteMapping(path = JOB_ERROR_URL + "/{eiJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
137 @ApiOperation(value = "Callback for EI job creation, returns error", notes = "", hidden = true)
140 @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
142 public ResponseEntity<Object> jobDeletedCallbackReturnError( //
143 @RequestBody ProducerJobInfo request) {
144 logger.info("Job created (returning error) callback {}", request.id);
145 this.testResults.noOfRejectedDelete += 1;
146 return ErrorResponse.create("Producer returns error on delete job", HttpStatus.NOT_FOUND);
149 @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
150 @ApiOperation(value = "Producer supervision", notes = "")
153 @ApiResponse(code = 200, message = "OK", response = String.class)}//
155 public ResponseEntity<Object> producerSupervision() {
156 logger.info("Producer supervision");
157 return new ResponseEntity<>(HttpStatus.OK);
160 @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
161 @ApiOperation(value = "Producer supervision error", notes = "", hidden = true)
164 @ApiResponse(code = 200, message = "OK", response = String.class)}//
166 public ResponseEntity<Object> producerSupervisionError() {
167 logger.info("Producer supervision error");
168 return new ResponseEntity<>(HttpStatus.NOT_FOUND);