d9c724fc0646613d1ca6efff84ee94456ee4f55d
[ccsdk/oran.git] /
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.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.responses.ApiResponses;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32
33 import java.lang.invoke.MethodHandles;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Vector;
39
40 import lombok.Getter;
41
42 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
43 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.bind.annotation.PostMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestHeader;
52 import org.springframework.web.bind.annotation.RestController;
53
54 @RestController("RappCallbacksController")
55 @Tag( //
56         name = Consts.V2_API_SERVICE_CALLBACKS_NAME, //
57         description = Consts.V2_API_SERVICE_CALLBACKS_DESCRIPTION //
58 )
59 public class RappSimulatorController {
60
61     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62     public static final String SERVICE_CALLBACK_URL = "/r-app/near-rt-ric-status";
63     private static Gson gson = new GsonBuilder().create();
64
65     public static class TestResults {
66         @Getter
67         private Vector<ServiceCallbackInfo> receivedInfo = new Vector<>();
68
69         public List<Map<String, String>> receivedHeaders =
70                 Collections.synchronizedList(new ArrayList<Map<String, String>>());
71
72         public void clear() {
73             receivedInfo.clear();
74             receivedHeaders.clear();
75         }
76     }
77
78     @Getter
79     private TestResults testResults = new TestResults();
80
81     private static final String CALLBACK_DESCRIPTION = "The URL to this call is registerred at Service registration.";
82
83     @PostMapping(path = SERVICE_CALLBACK_URL, produces = MediaType.APPLICATION_JSON_VALUE)
84     @Operation(summary = "Callback for Near-RT RIC status", description = CALLBACK_DESCRIPTION)
85     @ApiResponses(value = { //
86             @ApiResponse(responseCode = "200", description = "OK",
87                     content = @Content(schema = @Schema(implementation = VoidResponse.class)))} //
88     )
89
90     public ResponseEntity<Object> serviceCallback( //
91             @RequestBody ServiceCallbackInfo body, @RequestHeader Map<String, String> headers) {
92         logHeaders(headers);
93         logger.info("R-App callback body: {}", gson.toJson(body));
94         this.testResults.receivedHeaders.add(headers);
95         this.testResults.receivedInfo.add(body);
96         return new ResponseEntity<>(HttpStatus.OK);
97     }
98
99     private void logHeaders(Map<String, String> headers) {
100         logger.debug("Header begin");
101         headers.forEach((key, value) -> logger.debug("  key: {}, value: {}", key, value));
102         logger.debug("Header end");
103     }
104
105 }