2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 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.oransc.enrichment.controllers.r1consumer;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
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;
36 import java.lang.invoke.MethodHandles;
38 import java.net.URISyntaxException;
39 import java.util.ArrayList;
40 import java.util.List;
42 import org.json.JSONObject;
43 import org.oransc.enrichment.configuration.ApplicationConfig;
44 import org.oransc.enrichment.controllers.ErrorResponse;
45 import org.oransc.enrichment.controllers.VoidResponse;
46 import org.oransc.enrichment.controllers.r1producer.ProducerCallbacks;
47 import org.oransc.enrichment.exceptions.ServiceException;
48 import org.oransc.enrichment.repository.InfoJob;
49 import org.oransc.enrichment.repository.InfoJobs;
50 import org.oransc.enrichment.repository.InfoProducers;
51 import org.oransc.enrichment.repository.InfoType;
52 import org.oransc.enrichment.repository.InfoTypes;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.MediaType;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.web.bind.annotation.DeleteMapping;
60 import org.springframework.web.bind.annotation.GetMapping;
61 import org.springframework.web.bind.annotation.PathVariable;
62 import org.springframework.web.bind.annotation.PutMapping;
63 import org.springframework.web.bind.annotation.RequestBody;
64 import org.springframework.web.bind.annotation.RequestMapping;
65 import org.springframework.web.bind.annotation.RequestParam;
66 import org.springframework.web.bind.annotation.RestController;
67 import reactor.core.publisher.Mono;
69 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
70 @RestController("Consumer registry")
71 @Tag(name = ConsumerConsts.CONSUMER_API_NAME)
72 @RequestMapping(path = ConsumerConsts.API_ROOT, produces = MediaType.APPLICATION_JSON_VALUE)
73 public class ConsumerController {
75 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
78 ApplicationConfig applicationConfig;
81 private InfoJobs jobs;
84 private InfoTypes infoTypes;
87 private InfoProducers infoProducers;
90 ProducerCallbacks producerCallbacks;
92 private static Gson gson = new GsonBuilder().create();
94 @GetMapping(path = "/info-types", produces = MediaType.APPLICATION_JSON_VALUE)
95 @Operation(summary = "Information type identifiers", description = "")
100 description = "Information type identifiers", //
101 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))), //
103 public ResponseEntity<Object> getinfoTypeIdentifiers( //
105 List<String> result = new ArrayList<>();
106 for (InfoType infoType : this.infoTypes.getAllInfoTypes()) {
107 result.add(infoType.getId());
110 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
113 @GetMapping(path = "/info-types/{infoTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
114 @Operation(summary = "Individual information type", description = "")
118 responseCode = "200",
119 description = "Information type", //
120 content = @Content(schema = @Schema(implementation = ConsumerInfoTypeInfo.class))), //
122 responseCode = "404",
123 description = "Information type is not found", //
124 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
126 public ResponseEntity<Object> getInfoType( //
127 @PathVariable("infoTypeId") String infoTypeId) {
129 InfoType type = this.infoTypes.getType(infoTypeId);
130 ConsumerInfoTypeInfo info = toInfoTypeInfo(type);
131 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
132 } catch (Exception e) {
133 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
137 @GetMapping(path = "/info-jobs", produces = MediaType.APPLICATION_JSON_VALUE)
138 @Operation(summary = "Information Job identifiers", description = "query for information job identifiers")
142 responseCode = "200",
143 description = "Information information job identifiers", //
144 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))),
146 responseCode = "404",
147 description = "Information type is not found", //
148 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
150 public ResponseEntity<Object> getJobIds( //
152 name = ConsumerConsts.INFO_TYPE_ID_PARAM,
154 description = ConsumerConsts.INFO_TYPE_ID_PARAM_DESCRIPTION) //
155 @RequestParam(name = ConsumerConsts.INFO_TYPE_ID_PARAM, required = false) String infoTypeId,
157 name = ConsumerConsts.OWNER_PARAM,
159 description = ConsumerConsts.OWNER_PARAM_DESCRIPTION) //
160 @RequestParam(name = ConsumerConsts.OWNER_PARAM, required = false) String owner) {
162 List<String> result = new ArrayList<>();
164 for (InfoJob job : this.jobs.getJobsForOwner(owner)) {
165 if (infoTypeId == null || job.getTypeId().equals(infoTypeId)) {
166 result.add(job.getId());
169 } else if (infoTypeId != null) {
170 this.jobs.getJobsForType(infoTypeId).forEach(job -> result.add(job.getId()));
172 this.jobs.getJobs().forEach(job -> result.add(job.getId()));
174 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
178 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
182 @GetMapping(path = "/info-jobs/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE) //
183 @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB, description = "") //
187 responseCode = "200",
188 description = "Information subscription job", //
189 content = @Content(schema = @Schema(implementation = ConsumerJobInfo.class))), //
191 responseCode = "404",
192 description = "Information subscription job is not found", //
193 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
195 public ResponseEntity<Object> getIndividualEiJob( //
196 @PathVariable("infoJobId") String infoJobId) {
198 InfoJob job = this.jobs.getJob(infoJobId);
199 return new ResponseEntity<>(gson.toJson(toInfoJobInfo(job)), HttpStatus.OK);
200 } catch (Exception e) {
201 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
205 @GetMapping(path = "/info-jobs/{infoJobId}/status", produces = MediaType.APPLICATION_JSON_VALUE)
206 @Operation(summary = "Job status", description = "")
210 responseCode = "200",
211 description = "Information subscription job status", //
212 content = @Content(schema = @Schema(implementation = ConsumerJobStatus.class))), //
214 responseCode = "404",
215 description = "Information subscription job is not found", //
216 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
218 public ResponseEntity<Object> getEiJobStatus( //
219 @PathVariable("infoJobId") String jobId) {
221 InfoJob job = this.jobs.getJob(jobId);
222 return new ResponseEntity<>(gson.toJson(toInfoJobStatus(job)), HttpStatus.OK);
223 } catch (Exception e) {
224 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
228 private ConsumerJobStatus toInfoJobStatus(InfoJob job) {
229 return this.infoProducers.isJobEnabled(job)
230 ? new ConsumerJobStatus(ConsumerJobStatus.InfoJobStatusValues.ENABLED)
231 : new ConsumerJobStatus(ConsumerJobStatus.InfoJobStatusValues.DISABLED);
235 @DeleteMapping(path = "/info-jobs/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
236 @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB, description = "")
240 responseCode = "200",
241 description = "Not used", //
242 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
244 responseCode = "204",
245 description = "Job deleted", //
246 content = @Content(schema = @Schema(implementation = VoidResponse.class))), // "Individual
249 responseCode = "404",
250 description = "Information subscription job is not found", //
251 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
253 public ResponseEntity<Object> deleteIndividualEiJob( //
254 @PathVariable("infoJobId") String jobId) {
256 InfoJob job = this.jobs.getJob(jobId);
257 this.jobs.remove(job, this.infoProducers);
258 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
259 } catch (Exception e) {
260 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
265 path = "/info-jobs/{infoJobId}", //
266 produces = MediaType.APPLICATION_JSON_VALUE, //
267 consumes = MediaType.APPLICATION_JSON_VALUE)
268 @Operation(summary = ConsumerConsts.INDIVIDUAL_JOB, description = ConsumerConsts.PUT_INDIVIDUAL_JOB_DESCRIPTION)
272 responseCode = "201",
273 description = "Job created", //
274 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
276 responseCode = "200",
277 description = "Job updated", //
278 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
280 responseCode = "404",
281 description = "Information type is not found", //
282 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
284 public Mono<ResponseEntity<Object>> putIndividualInfoJob( //
285 @PathVariable("infoJobId") String jobId, //
287 name = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM,
289 description = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM_DESCRIPTION) //
291 name = ConsumerConsts.PERFORM_TYPE_CHECK_PARAM,
293 defaultValue = "false") boolean performTypeCheck,
294 @RequestBody ConsumerJobInfo informationJobObject) {
296 final boolean isNewJob = this.jobs.get(jobId) == null;
298 return validatePutInfoJob(jobId, informationJobObject, performTypeCheck) //
299 .flatMap(this::startInfoSubscriptionJob) //
300 .doOnNext(newEiJob -> this.jobs.put(newEiJob)) //
301 .flatMap(newEiJob -> Mono.just(new ResponseEntity<>(isNewJob ? HttpStatus.CREATED : HttpStatus.OK)))
302 .onErrorResume(throwable -> Mono.just(ErrorResponse.create(throwable, HttpStatus.NOT_FOUND)));
305 private Mono<InfoJob> startInfoSubscriptionJob(InfoJob newInfoJob) {
306 return this.producerCallbacks.startInfoSubscriptionJob(newInfoJob, infoProducers) //
307 .doOnNext(noOfAcceptingProducers -> this.logger.debug("Started job {}, number of activated producers: {}",
308 newInfoJob.getId(), noOfAcceptingProducers)) //
309 .flatMap(noOfAcceptingProducers -> Mono.just(newInfoJob));
312 private Mono<InfoJob> validatePutInfoJob(String jobId, ConsumerJobInfo jobInfo, boolean performTypeCheck) {
314 if (performTypeCheck) {
315 InfoType infoType = this.infoTypes.getType(jobInfo.infoTypeId);
316 validateJsonObjectAgainstSchema(infoType.getJobDataSchema(), jobInfo.jobDefinition);
318 InfoJob existingEiJob = this.jobs.get(jobId);
319 validateUri(jobInfo.statusNotificationUri);
320 validateUri(jobInfo.jobResultUri);
322 if (existingEiJob != null && !existingEiJob.getTypeId().equals(jobInfo.infoTypeId)) {
323 throw new ServiceException("Not allowed to change type for existing job", HttpStatus.CONFLICT);
325 return Mono.just(toEiJob(jobInfo, jobId, jobInfo.infoTypeId));
326 } catch (Exception e) {
327 return Mono.error(e);
331 private void validateUri(String url) throws URISyntaxException, ServiceException {
332 if (url != null && !url.isEmpty()) {
333 URI uri = new URI(url);
334 if (!uri.isAbsolute()) {
335 throw new ServiceException("URI: " + url + " is not absolute", HttpStatus.CONFLICT);
340 private void validateJsonObjectAgainstSchema(Object schemaObj, Object object) throws ServiceException {
341 if (schemaObj != null) { // schema is optional for now
343 ObjectMapper mapper = new ObjectMapper();
345 String schemaAsString = mapper.writeValueAsString(schemaObj);
346 JSONObject schemaJSON = new JSONObject(schemaAsString);
347 var schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
349 String objectAsString = mapper.writeValueAsString(object);
350 JSONObject json = new JSONObject(objectAsString);
351 schema.validate(json);
352 } catch (Exception e) {
353 throw new ServiceException("Json validation failure " + e.toString(), HttpStatus.CONFLICT);
358 private InfoJob toEiJob(ConsumerJobInfo info, String id, String typeId) {
359 return InfoJob.builder() //
362 .owner(info.owner) //
363 .jobData(info.jobDefinition) //
364 .targetUrl(info.jobResultUri) //
365 .jobStatusUrl(info.statusNotificationUri == null ? "" : info.statusNotificationUri) //
369 private ConsumerInfoTypeInfo toInfoTypeInfo(InfoType type) {
370 return new ConsumerInfoTypeInfo(type.getJobDataSchema());
373 private ConsumerJobInfo toInfoJobInfo(InfoJob s) {
374 return new ConsumerJobInfo(s.getTypeId(), s.getJobData(), s.getOwner(), s.getTargetUrl(), s.getJobStatusUrl());