2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oran.dmaapadapter;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import io.swagger.v3.oas.annotations.tags.Tag;
28 import java.lang.invoke.MethodHandles;
29 import java.util.HashMap;
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;
48 @RestController("IcsSimulatorController")
49 @Tag(name = "Information Coordinator Service Simulator (exists only in test)")
50 public class IcsSimulatorController {
52 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53 private final static Gson gson = new GsonBuilder().create();
55 public static class TestResults {
57 ProducerRegistrationInfo registrationInfo = null;
58 Map<String, ProducerInfoTypeInfo> types = new HashMap<>();
59 String infoProducerId = null;
61 public TestResults() {}
64 registrationInfo = null;
66 infoProducerId = null;
70 final TestResults testResults = new TestResults();
71 public static final String API_ROOT = "/data-producer/v1";
73 @GetMapping(path = API_ROOT + "/info-producers/{infoProducerId}", produces = MediaType.APPLICATION_JSON_VALUE)
74 public ResponseEntity<Object> getInfoProducer( //
75 @PathVariable("infoProducerId") String infoProducerId) {
77 if (testResults.registrationInfo != null) {
78 return new ResponseEntity<>(gson.toJson(testResults.registrationInfo), HttpStatus.OK);
80 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
84 @PutMapping(path = API_ROOT + "/info-producers/{infoProducerId}", //
85 produces = MediaType.APPLICATION_JSON_VALUE)
86 public ResponseEntity<Object> putInfoProducer( //
87 @PathVariable("infoProducerId") String infoProducerId, //
88 @RequestBody ProducerRegistrationInfo registrationInfo) {
89 testResults.registrationInfo = registrationInfo;
90 testResults.infoProducerId = infoProducerId;
91 return new ResponseEntity<>(HttpStatus.OK);
94 @PutMapping(path = API_ROOT + "/info-types/{infoTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
95 public ResponseEntity<Object> putInfoType( //
96 @PathVariable("infoTypeId") String infoTypeId, //
97 @RequestBody ProducerInfoTypeInfo registrationInfo) {
98 testResults.types.put(infoTypeId, registrationInfo);
99 return new ResponseEntity<>(HttpStatus.OK);
102 public void addJob(ConsumerJobInfo job, String jobId, AsyncRestClient restClient) {
103 String url = this.testResults.registrationInfo.jobCallbackUrl;
104 ProducerJobInfo request =
105 new ProducerJobInfo(job.jobDefinition, jobId, job.infoTypeId, job.jobResultUri, job.owner, "TIMESTAMP");
106 String body = gson.toJson(request);
107 logger.info("ICS Simulator PUT job: {}", body);
108 restClient.post(url, body, MediaType.APPLICATION_JSON).block();
111 public void deleteJob(String jobId, AsyncRestClient restClient) {
112 String url = this.testResults.registrationInfo.jobCallbackUrl + "/" + jobId;
113 logger.info("ICS Simulator DELETE job: {}", url);
114 restClient.delete(url).block();