2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6 * ======================================================================
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.clients.ProducerJobInfo;
36 import org.oransc.enrichment.controllers.ErrorResponse;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RestController;
46 @RestController("ProducerSimulatorController")
47 @Api(tags = {"Producer Simulator"})
48 public class ProducerSimulatorController {
50 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52 public static final String JOB_CREATED_URL = "/producer_simulator/job_created";
53 public static final String JOB_DELETED_URL = "/producer_simulator/job_deleted";
55 public static class TestResults {
57 public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
58 public List<ProducerJobInfo> jobsStopped = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
59 public boolean errorFound = false;
61 public TestResults() {
67 this.errorFound = false;
72 private TestResults testResults = new TestResults();
74 @PostMapping(path = JOB_CREATED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
75 @ApiOperation(value = "Callback for EI job creation", notes = "")
78 @ApiResponse(code = 200, message = "OK", response = void.class)}//
80 public ResponseEntity<Object> jobCreatedCallback( //
81 @RequestBody ProducerJobInfo request) {
83 this.testResults.jobsStarted.add(request);
84 logger.info("Job started callback {}", request.id);
85 if (request.id == null) {
86 throw new NullPointerException("Illegal argument");
88 return new ResponseEntity<>(HttpStatus.OK);
89 } catch (Exception e) {
90 this.testResults.errorFound = true;
91 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
95 @PostMapping(path = JOB_DELETED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
96 @ApiOperation(value = "Callback for EI job deletion", notes = "")
99 @ApiResponse(code = 200, message = "OK", response = void.class)}//
101 public ResponseEntity<Object> jobDeletedCallback( //
102 @RequestBody ProducerJobInfo request) {
104 logger.info("Job deleted callback {}", request.id);
105 this.testResults.jobsStopped.add(request);
106 return new ResponseEntity<>(HttpStatus.OK);
107 } catch (Exception e) {
108 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);