NONRTRIC - Implement DMaaP mediator producer service in Java
[nonrtric.git] / dmaap-adaptor-java / src / test / java / org / oran / dmaapadapter / EcsSimulatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.oran.dmaapadapter;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.v3.oas.annotations.tags.Tag;
27
28 import java.lang.invoke.MethodHandles;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.oran.dmaapadapter.clients.AsyncRestClient;
33 import org.oran.dmaapadapter.r1.ConsumerJobInfo;
34 import org.oran.dmaapadapter.r1.ProducerInfoTypeInfo;
35 import org.oran.dmaapadapter.r1.ProducerJobInfo;
36 import org.oran.dmaapadapter.r1.ProducerRegistrationInfo;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PutMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RestController;
47
48 @RestController("EcsSimulatorController")
49 @Tag(name = "EcsSimulator")
50 public class EcsSimulatorController {
51
52     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53     private final static Gson gson = new GsonBuilder().create();
54
55     public static class TestResults {
56
57         ProducerRegistrationInfo registrationInfo;
58         Map<String, ProducerInfoTypeInfo> types = new HashMap<>();
59
60         public TestResults() {}
61
62         public void reset() {
63             registrationInfo = null;
64             types.clear();
65         }
66     }
67
68     final TestResults testResults = new TestResults();
69     public static final String API_ROOT = "/data-producer/v1";
70
71     @GetMapping(path = API_ROOT + "/info-producers/{infoProducerId}", produces = MediaType.APPLICATION_JSON_VALUE)
72     public ResponseEntity<Object> getInfoProducer( //
73             @PathVariable("infoProducerId") String infoProducerId) {
74
75         if (testResults.registrationInfo != null) {
76             return new ResponseEntity<>(gson.toJson(testResults.registrationInfo), HttpStatus.OK);
77         } else {
78             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
79         }
80
81     }
82
83     @PutMapping(path = API_ROOT + "/info-producers/{infoProducerId}", //
84             produces = MediaType.APPLICATION_JSON_VALUE)
85     public ResponseEntity<Object> putInfoProducer( //
86             @PathVariable("infoProducerId") String infoProducerId, //
87             @RequestBody ProducerRegistrationInfo registrationInfo) {
88         testResults.registrationInfo = registrationInfo;
89         return new ResponseEntity<>(HttpStatus.OK);
90     }
91
92     @PutMapping(path = API_ROOT + "/info-types/{infoTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
93     public ResponseEntity<Object> putInfoType( //
94             @PathVariable("infoTypeId") String infoTypeId, //
95             @RequestBody ProducerInfoTypeInfo registrationInfo) {
96         testResults.types.put(infoTypeId, registrationInfo);
97         return new ResponseEntity<>(HttpStatus.OK);
98     }
99
100     public void addJob(ConsumerJobInfo job, AsyncRestClient restClient) {
101         String url = this.testResults.registrationInfo.jobCallbackUrl;
102         ProducerJobInfo request =
103                 new ProducerJobInfo(job.jobDefinition, "ID", job.infoTypeId, job.jobResultUri, job.owner, "TIMESTAMP");
104         String body = gson.toJson(request);
105         restClient.post(url, body).block();
106
107     }
108 }