9e383949f66c042a44c13ec4611077c65733cb0d
[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.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;
53
54 @RestController("ProducerSimulatorController")
55 @Tag(name = ProducerConsts.PRODUCER_API_CALLBACKS_NAME, description = ProducerConsts.PRODUCER_API_CALLBACKS_DESCRIPTION)
56 public class ProducerSimulatorController {
57
58     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60     public static final String JOB_URL = "/example-dataproducer/info-job";
61     public static final String JOB_ERROR_URL = "/example-dataproducer/info-job-error";
62
63     public static final String SUPERVISION_URL = "/example-dataproducer/health-check";
64     public static final String SUPERVISION_ERROR_URL = "/example-dataproducer/health-check-error";
65
66     public static class TestResults {
67
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;
73
74         public TestResults() {
75         }
76
77         public void reset() {
78             jobsStarted.clear();
79             jobsStopped.clear();
80             this.errorFound = false;
81             this.noOfRejectedCreate = 0;
82             this.noOfRejectedDelete = 0;
83         }
84     }
85
86     @Getter
87     private TestResults testResults = new TestResults();
88
89     @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
90     @Operation(
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.")
93     @ApiResponses(
94         value = { //
95             @ApiResponse(
96                 responseCode = "200",
97                 description = "OK", //
98                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
99         })
100     public ResponseEntity<Object> jobCreatedCallback( //
101         @RequestBody ProducerJobInfo request) {
102         try {
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");
107             }
108             return new ResponseEntity<>(HttpStatus.OK);
109         } catch (Exception e) {
110             this.testResults.errorFound = true;
111             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
112         }
113     }
114
115     @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
116     @Operation(
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.")
119     @ApiResponses(
120         value = { //
121             @ApiResponse(
122                 responseCode = "200",
123                 description = "OK", //
124                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
125         })
126     public ResponseEntity<Object> jobDeletedCallback( //
127         @PathVariable(ConsumerConsts.INFO_JOB_ID_PATH) String infoJobId) {
128         try {
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);
134         }
135     }
136
137     @PostMapping(path = JOB_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
138     @Operation(summary = "Callback for Information Job creation, returns error", description = "", hidden = true)
139     @ApiResponses(
140         value = { //
141             @ApiResponse(
142                 responseCode = "200",
143                 description = "OK", //
144                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
145         })
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);
151     }
152
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)
155     @ApiResponses(
156         value = { //
157             @ApiResponse(
158                 responseCode = "200",
159                 description = "OK", //
160                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
161         })
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);
167     }
168
169     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
170     @Operation(
171         summary = "Producer supervision",
172         description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
173     @ApiResponses(
174         value = { //
175             @ApiResponse(
176                 responseCode = "200",
177                 description = "The producer is OK", //
178                 content = @Content(schema = @Schema(implementation = String.class))) //
179         })
180     public ResponseEntity<Object> producerSupervision() {
181         logger.info("Producer supervision");
182         return new ResponseEntity<>(HttpStatus.OK);
183     }
184
185     @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
186     @Operation(summary = "Producer supervision error", description = "", hidden = true)
187     @ApiResponses(
188         value = { //
189             @ApiResponse(
190                 responseCode = "200",
191                 description = "OK", //
192                 content = @Content(schema = @Schema(implementation = String.class))) //
193         })
194     public ResponseEntity<Object> producerSupervisionError() {
195         logger.info("Producer supervision error");
196         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
197     }
198
199 }