4f2edcdc0565f19d9ac161ffdc09e6aa0eddf329
[nonrtric.git] / information-coordinator-service / src / test / java / org / oransc / ics / controller / ConsumerSimulatorController.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.VoidResponse;
38 import org.oransc.ics.controllers.r1consumer.ConsumerConsts;
39 import org.oransc.ics.controllers.r1consumer.ConsumerTypeRegistrationInfo;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RestController;
48
49 @RestController("ConsumerSimulatorController")
50 @Tag(name = ConsumerConsts.CONSUMER_API_CALLBACKS_NAME, description = ConsumerConsts.CONSUMER_API_DESCRIPTION)
51 public class ConsumerSimulatorController {
52
53     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55     public static class TestResults {
56
57         public List<ConsumerTypeRegistrationInfo> typeRegistrationInfoCallbacks =
58             Collections.synchronizedList(new ArrayList<ConsumerTypeRegistrationInfo>());
59
60         public void reset() {
61             typeRegistrationInfoCallbacks.clear();
62         }
63     }
64
65     @Getter
66     private TestResults testResults = new TestResults();
67
68     private static final String TYPE_STATUS_CALLBACK_URL = "/example-dataconsumer/info-type-status";
69
70     public static String getTypeStatusCallbackUrl() {
71         return TYPE_STATUS_CALLBACK_URL;
72     }
73
74     @PostMapping(path = TYPE_STATUS_CALLBACK_URL, produces = MediaType.APPLICATION_JSON_VALUE)
75     @Operation(
76         summary = "Callback for changed Information type registration status",
77         description = "The primitive is implemented by the data consumer and is invoked when a Information type status has been changed. <br/>"
78             + "Subscription are managed by primitives in '" + ConsumerConsts.CONSUMER_API_NAME + "'")
79     @ApiResponses(
80         value = { //
81             @ApiResponse(
82                 responseCode = "200",
83                 description = "OK", //
84                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
85         })
86     public ResponseEntity<Object> typeStatusCallback( //
87         @RequestBody ConsumerTypeRegistrationInfo status) {
88         logger.info("Job type registration status callback status: {}", status);
89         this.testResults.typeRegistrationInfoCallbacks.add(status);
90         return new ResponseEntity<>(HttpStatus.OK);
91     }
92
93 }