Creating PM-producer
[nonrtric/plt/ranpm.git] / pmproducer / src / test / java / org / oran / pmproducer / IcsSimulatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 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.pmproducer;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28
29 import io.swagger.v3.oas.annotations.tags.Tag;
30
31 import java.lang.invoke.MethodHandles;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import org.json.JSONObject;
37 import org.oran.pmproducer.clients.AsyncRestClient;
38 import org.oran.pmproducer.exceptions.ServiceException;
39 import org.oran.pmproducer.r1.ConsumerJobInfo;
40 import org.oran.pmproducer.r1.ProducerInfoTypeInfo;
41 import org.oran.pmproducer.r1.ProducerJobInfo;
42 import org.oran.pmproducer.r1.ProducerRegistrationInfo;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.MediaType;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PathVariable;
50 import org.springframework.web.bind.annotation.PutMapping;
51 import org.springframework.web.bind.annotation.RequestBody;
52 import org.springframework.web.bind.annotation.RestController;
53
54 @RestController("IcsSimulatorController")
55 @Tag(name = "Information Coordinator Service Simulator (exists only in test)")
56 public class IcsSimulatorController {
57
58     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59     private final static Gson gson = new GsonBuilder().disableHtmlEscaping().create();
60
61     public static class TestResults {
62
63         ProducerRegistrationInfo registrationInfo = null;
64         Map<String, ProducerInfoTypeInfo> types = Collections.synchronizedMap(new HashMap<>());
65         String infoProducerId = null;
66         ConsumerJobInfo createdJob = null;
67
68         public TestResults() {}
69
70         public synchronized void reset() {
71             registrationInfo = null;
72             types.clear();
73             infoProducerId = null;
74             createdJob = null;
75         }
76
77         public void setCreatedJob(ConsumerJobInfo informationJobObject) {
78             this.createdJob = informationJobObject;
79         }
80     }
81
82     final TestResults testResults = new TestResults();
83     public static final String API_ROOT = "/data-producer/v1";
84
85     @GetMapping(path = API_ROOT + "/info-producers/{infoProducerId}", produces = MediaType.APPLICATION_JSON_VALUE)
86     public ResponseEntity<Object> getInfoProducer( //
87             @PathVariable("infoProducerId") String infoProducerId) {
88
89         if (testResults.registrationInfo != null) {
90             return new ResponseEntity<>(gson.toJson(testResults.registrationInfo), HttpStatus.OK);
91         } else {
92             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
93         }
94     }
95
96     @PutMapping(path = API_ROOT + "/info-producers/{infoProducerId}", //
97             produces = MediaType.APPLICATION_JSON_VALUE)
98     public ResponseEntity<Object> putInfoProducer( //
99             @PathVariable("infoProducerId") String infoProducerId, //
100             @RequestBody ProducerRegistrationInfo registrationInfo) {
101         testResults.registrationInfo = registrationInfo;
102         testResults.infoProducerId = infoProducerId;
103         return new ResponseEntity<>(HttpStatus.OK);
104     }
105
106     @PutMapping(path = API_ROOT + "/info-types/{infoTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
107     public ResponseEntity<Object> putInfoType( //
108             @PathVariable("infoTypeId") String infoTypeId, //
109             @RequestBody ProducerInfoTypeInfo registrationInfo) {
110         testResults.types.put(infoTypeId, registrationInfo);
111         return new ResponseEntity<>(HttpStatus.OK);
112     }
113
114     @PutMapping(path = "/data-consumer/v1/info-jobs/{infoJobId}", //
115             produces = MediaType.APPLICATION_JSON_VALUE, //
116             consumes = MediaType.APPLICATION_JSON_VALUE)
117     public ResponseEntity<Object> putIndividualInfoJob( //
118             @PathVariable("infoJobId") String jobId, //
119             @RequestBody ConsumerJobInfo informationJobObject) {
120         logger.debug("*** added consumer job {}", jobId);
121         testResults.setCreatedJob(informationJobObject);
122         return new ResponseEntity<>(HttpStatus.OK);
123     }
124
125     public void addJob(ConsumerJobInfo job, String jobId, AsyncRestClient restClient) throws ServiceException {
126         String url = this.testResults.registrationInfo.jobCallbackUrl;
127         ProducerJobInfo request = new ProducerJobInfo(job.jobDefinition, jobId, job.infoTypeId, job.owner, "TIMESTAMP");
128         String body = gson.toJson(request);
129         ProducerInfoTypeInfo type = testResults.types.get(job.infoTypeId);
130         if (type == null) {
131             logger.error("type not found: {} size: {}", job.infoTypeId, testResults.types.size());
132         } else {
133             assertThat(type).isNotNull();
134             validateJsonObjectAgainstSchema(job.jobDefinition, type.jobDataSchema);
135             logger.debug("ICS Simulator PUT job: {}", body);
136             restClient.post(url, body, MediaType.APPLICATION_JSON).block();
137         }
138     }
139
140     private void validateJsonObjectAgainstSchema(Object object, Object schemaObj) throws ServiceException {
141         if (schemaObj != null) { // schema is optional for now
142             try {
143                 ObjectMapper mapper = new ObjectMapper();
144
145                 String schemaAsString = mapper.writeValueAsString(schemaObj);
146                 JSONObject schemaJSON = new JSONObject(schemaAsString);
147                 var schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
148
149                 String objectAsString = object.toString();
150                 JSONObject json = new JSONObject(objectAsString);
151                 schema.validate(json);
152             } catch (Exception e) {
153                 logger.error("Json validation failure {}", e.toString());
154                 throw new ServiceException("Json validation failure " + e.toString(), HttpStatus.BAD_REQUEST);
155             }
156         }
157     }
158
159     public void deleteJob(String jobId, AsyncRestClient restClient) {
160         String url = this.testResults.registrationInfo.jobCallbackUrl + "/" + jobId;
161         logger.debug("ICS Simulator DELETE job: {}", url);
162         restClient.delete(url).block();
163
164     }
165 }