Rename enrichment coordinator service to information coordinator service
[nonrtric.git] / information-coordinator-service / src / test / java / org / oransc / ics / controller / ProducerSimulatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.ics.controller;
22
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;
29
30 import java.lang.invoke.MethodHandles;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34
35 import lombok.Getter;
36
37 import org.oransc.ics.controllers.ErrorResponse;
38 import org.oransc.ics.controllers.VoidResponse;
39 import org.oransc.ics.controllers.r1producer.ProducerConsts;
40 import org.oransc.ics.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;
52
53 @RestController("ProducerSimulatorController")
54 @Tag(name = ProducerConsts.PRODUCER_API_CALLBACKS_NAME)
55 public class ProducerSimulatorController {
56
57     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59     public static final String JOB_URL = "/example_dataproducer/info_job";
60     public static final String JOB_ERROR_URL = "/example_dataproducer/info_job_error";
61
62     public static final String SUPERVISION_URL = "/example_dataproducer/health_check";
63     public static final String SUPERVISION_ERROR_URL = "/example_dataproducer/health_check_error";
64
65     public static class TestResults {
66
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;
72
73         public TestResults() {
74         }
75
76         public void reset() {
77             jobsStarted.clear();
78             jobsStopped.clear();
79             this.errorFound = false;
80             this.noOfRejectedCreate = 0;
81             this.noOfRejectedDelete = 0;
82         }
83     }
84
85     @Getter
86     private TestResults testResults = new TestResults();
87
88     @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
89     @Operation(
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.")
92     @ApiResponses(
93         value = { //
94             @ApiResponse(
95                 responseCode = "200",
96                 description = "OK", //
97                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
98         })
99     public ResponseEntity<Object> jobCreatedCallback( //
100         @RequestBody ProducerJobInfo request) {
101         try {
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");
106             }
107             return new ResponseEntity<>(HttpStatus.OK);
108         } catch (Exception e) {
109             this.testResults.errorFound = true;
110             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
111         }
112     }
113
114     @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
115     @Operation(
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.")
118     @ApiResponses(
119         value = { //
120             @ApiResponse(
121                 responseCode = "200",
122                 description = "OK", //
123                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
124         })
125     public ResponseEntity<Object> jobDeletedCallback( //
126         @PathVariable("infoJobId") String infoJobId) {
127         try {
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);
133         }
134     }
135
136     @PostMapping(path = JOB_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
137     @Operation(summary = "Callback for Information Job creation, returns error", description = "", hidden = true)
138     @ApiResponses(
139         value = { //
140             @ApiResponse(
141                 responseCode = "200",
142                 description = "OK", //
143                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
144         })
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);
150     }
151
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)
154     @ApiResponses(
155         value = { //
156             @ApiResponse(
157                 responseCode = "200",
158                 description = "OK", //
159                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
160         })
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);
166     }
167
168     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
169     @Operation(
170         summary = "Producer supervision",
171         description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
172     @ApiResponses(
173         value = { //
174             @ApiResponse(
175                 responseCode = "200",
176                 description = "The producer is OK", //
177                 content = @Content(schema = @Schema(implementation = String.class))) //
178         })
179     public ResponseEntity<Object> producerSupervision() {
180         logger.info("Producer supervision");
181         return new ResponseEntity<>(HttpStatus.OK);
182     }
183
184     @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
185     @Operation(summary = "Producer supervision error", description = "", hidden = true)
186     @ApiResponses(
187         value = { //
188             @ApiResponse(
189                 responseCode = "200",
190                 description = "OK", //
191                 content = @Content(schema = @Schema(implementation = String.class))) //
192         })
193     public ResponseEntity<Object> producerSupervisionError() {
194         logger.info("Producer supervision error");
195         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
196     }
197
198 }