NONRTRIC - Adding information consumer before information type
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / r1consumer / ConsumerController.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.controllers.r1consumer;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26
27 import io.swagger.v3.oas.annotations.Operation;
28 import io.swagger.v3.oas.annotations.Parameter;
29 import io.swagger.v3.oas.annotations.media.ArraySchema;
30 import io.swagger.v3.oas.annotations.media.Content;
31 import io.swagger.v3.oas.annotations.media.Schema;
32 import io.swagger.v3.oas.annotations.responses.ApiResponse;
33 import io.swagger.v3.oas.annotations.responses.ApiResponses;
34 import io.swagger.v3.oas.annotations.tags.Tag;
35
36 import java.lang.invoke.MethodHandles;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 import org.json.JSONObject;
41 import org.oransc.enrichment.configuration.ApplicationConfig;
42 import org.oransc.enrichment.controllers.ErrorResponse;
43 import org.oransc.enrichment.controllers.VoidResponse;
44 import org.oransc.enrichment.controllers.r1producer.ProducerCallbacks;
45 import org.oransc.enrichment.exceptions.ServiceException;
46 import org.oransc.enrichment.repository.EiJob;
47 import org.oransc.enrichment.repository.EiJobs;
48 import org.oransc.enrichment.repository.EiProducers;
49 import org.oransc.enrichment.repository.EiType;
50 import org.oransc.enrichment.repository.EiTypes;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.http.HttpStatus;
55 import org.springframework.http.MediaType;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.web.bind.annotation.DeleteMapping;
58 import org.springframework.web.bind.annotation.GetMapping;
59 import org.springframework.web.bind.annotation.PathVariable;
60 import org.springframework.web.bind.annotation.PutMapping;
61 import org.springframework.web.bind.annotation.RequestBody;
62 import org.springframework.web.bind.annotation.RequestMapping;
63 import org.springframework.web.bind.annotation.RequestParam;
64 import org.springframework.web.bind.annotation.RestController;
65 import reactor.core.publisher.Mono;
66
67 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
68 @RestController("Consumer registry")
69 @Tag(name = ConsumerConsts.CONSUMER_API_NAME)
70 @RequestMapping(path = ConsumerConsts.API_ROOT, produces = MediaType.APPLICATION_JSON_VALUE)
71 public class ConsumerController {
72
73     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
74
75     @Autowired
76     ApplicationConfig applicationConfig;
77
78     @Autowired
79     private EiJobs jobs;
80
81     @Autowired
82     private EiTypes infoTypes;
83
84     @Autowired
85     private EiProducers infoProducers;
86
87     @Autowired
88     ProducerCallbacks producerCallbacks;
89
90     private static Gson gson = new GsonBuilder().create();
91
92     @GetMapping(path = "/info-types", produces = MediaType.APPLICATION_JSON_VALUE)
93     @Operation(summary = "Information type identifiers", description = "")
94     @ApiResponses(
95         value = { //
96             @ApiResponse(
97                 responseCode = "200",
98                 description = "Information type identifiers", //
99                 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))), //
100         })
101     public ResponseEntity<Object> getinfoTypeIdentifiers( //
102     ) {
103         List<String> result = new ArrayList<>();
104         for (EiType infoType : this.infoTypes.getAllInfoTypes()) {
105             result.add(infoType.getId());
106         }
107
108         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
109     }
110
111     @GetMapping(path = "/info-types/{infoTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
112     @Operation(summary = "Individual information type", description = "")
113     @ApiResponses(
114         value = { //
115             @ApiResponse(
116                 responseCode = "200",
117                 description = "Information type", //
118                 content = @Content(schema = @Schema(implementation = ConsumerInfoTypeInfo.class))), //
119             @ApiResponse(
120                 responseCode = "404",
121                 description = "Information type is not found", //
122                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
123         })
124     public ResponseEntity<Object> getInfoType( //
125         @PathVariable("infoTypeId") String infoTypeId) {
126         try {
127             EiType type = this.infoTypes.getType(infoTypeId);
128             ConsumerInfoTypeInfo info = toInfoTypeInfo(type);
129             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
130         } catch (Exception e) {
131             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
132         }
133     }
134
135     @GetMapping(path = "/info-jobs", produces = MediaType.APPLICATION_JSON_VALUE)
136     @Operation(summary = "EI job identifiers", description = "query for information job identifiers")
137     @ApiResponses(
138         value = { //
139             @ApiResponse(
140                 responseCode = "200",
141                 description = "Information information job identifiers", //
142                 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))),
143             @ApiResponse(
144                 responseCode = "404",
145                 description = "Information type is not found", //
146                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
147         })
148     public ResponseEntity<Object> getJobIds( //
149         @Parameter(
150             name = ConsumerConsts.INFO_TYPE_ID_PARAM,
151             required = false, //
152             description = ConsumerConsts.INFO_TYPE_ID_PARAM_DESCRIPTION) //
153         @RequestParam(name = ConsumerConsts.INFO_TYPE_ID_PARAM, required = false) String infoTypeId,
154         @Parameter(
155             name = ConsumerConsts.OWNER_PARAM,
156             required = false, //
157             description = ConsumerConsts.OWNER_PARAM_DESCRIPTION) //
158         @RequestParam(name = ConsumerConsts.OWNER_PARAM, required = false) String owner) {
159         try {
160             List<String> result = new ArrayList<>();
161             if (owner != null) {
162                 for (EiJob job : this.jobs.getJobsForOwner(owner)) {
163                     if (infoTypeId == null || job.getTypeId().equals(infoTypeId)) {
164                         result.add(job.getId());
165                     }
166                 }
167             } else if (infoTypeId != null) {
168                 this.jobs.getJobsForType(infoTypeId).forEach(job -> result.add(job.getId()));
169             } else {
170                 this.jobs.getJobs().forEach(job -> result.add(job.getId()));
171             }
172             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
173         } catch (
174
175         Exception e) {
176             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
177         }
178     }
179
180     @GetMapping(path = "/info-jobs/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE) //
181     @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB, description = "") //
182     @ApiResponses(
183         value = { //
184             @ApiResponse(
185                 responseCode = "200",
186                 description = "Information subscription job", //
187                 content = @Content(schema = @Schema(implementation = ConsumerJobInfo.class))), //
188             @ApiResponse(
189                 responseCode = "404",
190                 description = "Information subscription job is not found", //
191                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
192         })
193     public ResponseEntity<Object> getIndividualEiJob( //
194         @PathVariable("infoJobId") String infoJobId) {
195         try {
196             EiJob job = this.jobs.getJob(infoJobId);
197             return new ResponseEntity<>(gson.toJson(toInfoJobInfo(job)), HttpStatus.OK);
198         } catch (Exception e) {
199             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
200         }
201     }
202
203     @GetMapping(path = "/info-jobs/{infoJobId}/status", produces = MediaType.APPLICATION_JSON_VALUE)
204     @Operation(summary = "Job status", description = "")
205     @ApiResponses(
206         value = { //
207             @ApiResponse(
208                 responseCode = "200",
209                 description = "Information subscription job status", //
210                 content = @Content(schema = @Schema(implementation = ConsumerJobStatus.class))), //
211             @ApiResponse(
212                 responseCode = "404",
213                 description = "Information subscription job is not found", //
214                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
215         })
216     public ResponseEntity<Object> getEiJobStatus( //
217         @PathVariable("infoJobId") String jobId) {
218         try {
219             EiJob job = this.jobs.getJob(jobId);
220             return new ResponseEntity<>(gson.toJson(toInfoJobStatus(job)), HttpStatus.OK);
221         } catch (Exception e) {
222             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
223         }
224     }
225
226     private ConsumerJobStatus toInfoJobStatus(EiJob job) {
227         return this.infoProducers.isJobEnabled(job)
228             ? new ConsumerJobStatus(ConsumerJobStatus.InfoJobStatusValues.ENABLED)
229             : new ConsumerJobStatus(ConsumerJobStatus.InfoJobStatusValues.DISABLED);
230
231     }
232
233     @DeleteMapping(path = "/info-jobs/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
234     @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB, description = "")
235     @ApiResponses(
236         value = { //
237             @ApiResponse(
238                 responseCode = "200",
239                 description = "Not used", //
240                 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
241             @ApiResponse(
242                 responseCode = "204",
243                 description = "Job deleted", //
244                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), // "Individual EI job"
245             @ApiResponse(
246                 responseCode = "404",
247                 description = "Information subscription job is not found", //
248                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
249         })
250     public ResponseEntity<Object> deleteIndividualEiJob( //
251         @PathVariable("infoJobId") String jobId) {
252         try {
253             EiJob job = this.jobs.getJob(jobId);
254             this.jobs.remove(job, this.infoProducers);
255             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
256         } catch (Exception e) {
257             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
258         }
259     }
260
261     @PutMapping(
262         path = "/info-jobs/{infoJobId}", //
263         produces = MediaType.APPLICATION_JSON_VALUE, //
264         consumes = MediaType.APPLICATION_JSON_VALUE)
265     @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB, description = ConsumerConsts.PUT_INDIVIDUAL_JOB_DESCRIPTION)
266     @ApiResponses(
267         value = { //
268             @ApiResponse(
269                 responseCode = "201",
270                 description = "Job created", //
271                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
272             @ApiResponse(
273                 responseCode = "200",
274                 description = "Job updated", //
275                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
276             @ApiResponse(
277                 responseCode = "404",
278                 description = "Enrichment Information type is not found", //
279                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
280         })
281     public Mono<ResponseEntity<Object>> putIndividualInfoJob( //
282         @PathVariable("infoJobId") String jobId, //
283         @Parameter(
284             name = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM,
285             required = false, //
286             description = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM_DESCRIPTION) //
287         @RequestParam(
288             name = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM,
289             required = false,
290             defaultValue = "false") boolean performTypeCheck,
291         @RequestBody ConsumerJobInfo informationJobObject) {
292
293         final boolean isNewJob = this.jobs.get(jobId) == null;
294
295         return validatePutInfoJob(jobId, informationJobObject, performTypeCheck) //
296             .flatMap(this::startInfoSubscriptionJob) //
297             .doOnNext(newEiJob -> this.jobs.put(newEiJob)) //
298             .flatMap(newEiJob -> Mono.just(new ResponseEntity<>(isNewJob ? HttpStatus.CREATED : HttpStatus.OK)))
299             .onErrorResume(throwable -> Mono.just(ErrorResponse.create(throwable, HttpStatus.NOT_FOUND)));
300     }
301
302     private Mono<EiJob> startInfoSubscriptionJob(EiJob newInfoJob) {
303         return this.producerCallbacks.startInfoSubscriptionJob(newInfoJob, infoProducers) //
304             .doOnNext(noOfAcceptingProducers -> this.logger.debug("Started job {}, number of activated producers: {}",
305                 newInfoJob.getId(), noOfAcceptingProducers)) //
306             .flatMap(noOfAcceptingProducers -> Mono.just(newInfoJob));
307     }
308
309     private Mono<EiJob> validatePutInfoJob(String jobId, ConsumerJobInfo jobInfo, boolean performTypeCheck) {
310         try {
311             if (performTypeCheck) {
312                 EiType infoType = this.infoTypes.getType(jobInfo.infoTypeId);
313                 validateJsonObjectAgainstSchema(infoType.getJobDataSchema(), jobInfo.jobDefinition);
314             }
315             EiJob existingEiJob = this.jobs.get(jobId);
316
317             if (existingEiJob != null && !existingEiJob.getTypeId().equals(jobInfo.infoTypeId)) {
318                 throw new ServiceException("Not allowed to change type for existing job", HttpStatus.CONFLICT);
319             }
320             return Mono.just(toEiJob(jobInfo, jobId, jobInfo.infoTypeId));
321         } catch (Exception e) {
322             return Mono.error(e);
323         }
324     }
325
326     private void validateJsonObjectAgainstSchema(Object schemaObj, Object object) throws ServiceException {
327         if (schemaObj != null) { // schema is optional for now
328             try {
329                 ObjectMapper mapper = new ObjectMapper();
330
331                 String schemaAsString = mapper.writeValueAsString(schemaObj);
332                 JSONObject schemaJSON = new JSONObject(schemaAsString);
333                 org.everit.json.schema.Schema schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
334
335                 String objectAsString = mapper.writeValueAsString(object);
336                 JSONObject json = new JSONObject(objectAsString);
337                 schema.validate(json);
338             } catch (Exception e) {
339                 throw new ServiceException("Json validation failure " + e.toString(), HttpStatus.CONFLICT);
340             }
341         }
342     }
343
344     private EiJob toEiJob(ConsumerJobInfo info, String id, String typeId) {
345         return EiJob.builder() //
346             .id(id) //
347             .typeId(typeId) //
348             .owner(info.owner) //
349             .jobData(info.jobDefinition) //
350             .targetUrl(info.jobResultUri) //
351             .jobStatusUrl(info.statusNotificationUri == null ? "" : info.statusNotificationUri) //
352             .build();
353     }
354
355     private EiJob toEiJob(ConsumerJobInfo info, String id, EiType type) {
356         return toEiJob(info, id, type.getId());
357     }
358
359     private ConsumerInfoTypeInfo toInfoTypeInfo(EiType type) {
360         return new ConsumerInfoTypeInfo(type.getJobDataSchema());
361     }
362
363     private ConsumerJobInfo toInfoJobInfo(EiJob s) {
364         return new ConsumerJobInfo(s.getTypeId(), s.getJobData(), s.getOwner(), s.getTargetUrl(), s.getJobStatusUrl());
365     }
366 }