a5ddc490fed83bba828ed78494ac0ee12b474eca
[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 import java.util.Map;
35
36 import lombok.Getter;
37
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;
55
56 @RestController("ProducerSimulatorController")
57 @Tag(name = ProducerConsts.PRODUCER_API_CALLBACKS_NAME, description = ProducerConsts.PRODUCER_API_CALLBACKS_DESCRIPTION)
58 public class ProducerSimulatorController {
59
60     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62     public static final String JOB_URL = "/example-dataproducer/info-job";
63     public static final String JOB_ERROR_URL = "/example-dataproducer/info-job-error";
64
65     public static final String SUPERVISION_URL = "/example-dataproducer/health-check";
66     public static final String SUPERVISION_ERROR_URL = "/example-dataproducer/health-check-error";
67
68     public static class TestResults {
69
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;
77
78         public TestResults() {
79         }
80
81         public void reset() {
82             jobsStarted.clear();
83             jobsStopped.clear();
84             receivedHeaders.clear();
85             this.errorFound = false;
86             this.noOfRejectedCreate = 0;
87             this.noOfRejectedDelete = 0;
88         }
89     }
90
91     @Getter
92     private TestResults testResults = new TestResults();
93
94     @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
95     @Operation(
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.")
98     @ApiResponses(
99         value = { //
100             @ApiResponse(
101                 responseCode = "200",
102                 description = "OK", //
103                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
104         })
105     public ResponseEntity<Object> jobCreatedCallback( //
106         @RequestHeader Map<String, String> headers, //
107         @RequestBody ProducerJobInfo request) {
108         try {
109             logHeaders(headers);
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");
115             }
116             return new ResponseEntity<>(HttpStatus.OK);
117         } catch (Exception e) {
118             this.testResults.errorFound = true;
119             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
120         }
121     }
122
123     @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
124     @Operation(
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.")
127     @ApiResponses(
128         value = { //
129             @ApiResponse(
130                 responseCode = "200",
131                 description = "OK", //
132                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
133         })
134     public ResponseEntity<Object> jobDeletedCallback( //
135         @RequestHeader Map<String, String> headers, @PathVariable(ConsumerConsts.INFO_JOB_ID_PATH) String infoJobId) {
136         try {
137             logHeaders(headers);
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);
144         }
145     }
146
147     @PostMapping(path = JOB_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
148     @Operation(summary = "Callback for Information Job creation, returns error", description = "", hidden = true)
149     @ApiResponses(
150         value = { //
151             @ApiResponse(
152                 responseCode = "200",
153                 description = "OK", //
154                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
155         })
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);
161     }
162
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)
165     @ApiResponses(
166         value = { //
167             @ApiResponse(
168                 responseCode = "200",
169                 description = "OK", //
170                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
171         })
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);
177     }
178
179     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
180     @Operation(
181         summary = "Producer supervision",
182         description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
183     @ApiResponses(
184         value = { //
185             @ApiResponse(
186                 responseCode = "200",
187                 description = "The producer is OK", //
188                 content = @Content(schema = @Schema(implementation = String.class))) //
189         })
190     public ResponseEntity<Object> producerSupervision() {
191         logger.info("Producer supervision");
192         return new ResponseEntity<>("Hunky dory", HttpStatus.OK);
193     }
194
195     @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
196     @Operation(summary = "Producer supervision error", description = "", hidden = true)
197     @ApiResponses(
198         value = { //
199             @ApiResponse(
200                 responseCode = "200",
201                 description = "OK", //
202                 content = @Content(schema = @Schema(implementation = String.class))) //
203         })
204     public ResponseEntity<Object> producerSupervisionError() {
205         logger.info("Producer supervision error");
206         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
207     }
208
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");
213     }
214
215 }