Merge "Kafka now works in kube for calls outside its namespace"
[nonrtric.git] / enrichment-coordinator-service / src / test / java / org / oransc / enrichment / 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.enrichment.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.enrichment.controllers.VoidResponse;
38 import org.oransc.enrichment.controllers.a1e.A1eConsts;
39 import org.oransc.enrichment.controllers.a1e.A1eEiJobStatus;
40 import org.oransc.enrichment.controllers.r1consumer.ConsumerConsts;
41 import org.oransc.enrichment.controllers.r1consumer.ConsumerTypeRegistrationInfo;
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.PathVariable;
48 import org.springframework.web.bind.annotation.PostMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RestController;
51
52 @RestController("ConsumerSimulatorController")
53 public class ConsumerSimulatorController {
54
55     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56
57     public static class TestResults {
58
59         public List<A1eEiJobStatus> eiJobStatusCallbacks =
60             Collections.synchronizedList(new ArrayList<A1eEiJobStatus>());
61         public List<ConsumerTypeRegistrationInfo> typeRegistrationInfoCallbacks =
62             Collections.synchronizedList(new ArrayList<ConsumerTypeRegistrationInfo>());
63
64         public void reset() {
65             eiJobStatusCallbacks.clear();
66             typeRegistrationInfoCallbacks.clear();
67         }
68     }
69
70     @Getter
71     private TestResults testResults = new TestResults();
72
73     public static String getJobStatusUrl(String infoJobId) {
74         return "/example_dataconsumer/info_jobs/" + infoJobId + "/status";
75     }
76
77     @Tag(name = A1eConsts.CONSUMER_API_CALLBACKS_NAME)
78     @PostMapping(
79         path = "/example_dataconsumer/info_jobs/{infoJobId}/status",
80         produces = MediaType.APPLICATION_JSON_VALUE)
81     @Operation(
82         summary = "Callback for changed Information Job status",
83         description = "The primitive is implemented by the data consumer and is invoked when a Information Job status has been changed.")
84     @ApiResponses(
85         value = { //
86             @ApiResponse(
87                 responseCode = "200",
88                 description = "OK", //
89                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
90         })
91     public ResponseEntity<Object> jobStatusCallback( //
92         @PathVariable("infoJobId") String infoJobId, //
93         @RequestBody A1eEiJobStatus status) {
94         logger.info("Job status callback status: {} infoJobId: {}", status.state, infoJobId);
95         this.testResults.eiJobStatusCallbacks.add(status);
96         return new ResponseEntity<>(HttpStatus.OK);
97     }
98
99     private static final String TYPE_STATUS_CALLBACK_URL = "/example_dataconsumer/info_type_status";
100
101     public static String getTypeStatusCallbackUrl() {
102         return TYPE_STATUS_CALLBACK_URL;
103     }
104
105     @Tag(name = ConsumerConsts.CONSUMER_API_CALLBACKS_NAME)
106     @PostMapping(path = TYPE_STATUS_CALLBACK_URL, produces = MediaType.APPLICATION_JSON_VALUE)
107     @Operation(
108         summary = "Callback for changed Information type registration status",
109         description = "The primitive is implemented by the data consumer and is invoked when a Information type status has been changed. <br/>"
110             + "Subscription are managed by primitives in '" + ConsumerConsts.CONSUMER_API_NAME + "'")
111     @ApiResponses(
112         value = { //
113             @ApiResponse(
114                 responseCode = "200",
115                 description = "OK", //
116                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
117         })
118     public ResponseEntity<Object> typeStatusCallback( //
119         @RequestBody ConsumerTypeRegistrationInfo status) {
120         logger.info("Job type registration status callback status: {}", status);
121         this.testResults.typeRegistrationInfoCallbacks.add(status);
122         return new ResponseEntity<>(HttpStatus.OK);
123     }
124
125 }