2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 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.pmproducer.controllers;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.responses.ApiResponses;
32 import io.swagger.v3.oas.annotations.tags.Tag;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
38 import org.oran.pmproducer.exceptions.ServiceException;
39 import org.oran.pmproducer.r1.ProducerJobInfo;
40 import org.oran.pmproducer.repository.InfoTypes;
41 import org.oran.pmproducer.repository.Job;
42 import org.oran.pmproducer.repository.Jobs;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.bind.annotation.DeleteMapping;
50 import org.springframework.web.bind.annotation.GetMapping;
51 import org.springframework.web.bind.annotation.PathVariable;
52 import org.springframework.web.bind.annotation.PostMapping;
53 import org.springframework.web.bind.annotation.RequestBody;
54 import org.springframework.web.bind.annotation.RestController;
56 @RestController("ConfigurationControllerV2")
57 @Tag(name = ProducerCallbacksController.API_NAME)
58 public class ProducerCallbacksController {
59 private static final Logger logger = LoggerFactory.getLogger(ProducerCallbacksController.class);
61 public static final String API_NAME = "Producer job control API";
62 public static final String API_DESCRIPTION = "";
63 public static final String JOB_URL = "/generic_dataproducer/info_job";
64 public static final String SUPERVISION_URL = "/generic_dataproducer/health_check";
66 public static final String STATISTICS_URL = "/statistics";
68 private static Gson gson = new GsonBuilder().disableHtmlEscaping().create();
69 private final Jobs jobs;
70 private final InfoTypes types;
72 public ProducerCallbacksController(@Autowired Jobs jobs, @Autowired InfoTypes types) {
77 @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
78 @Operation(summary = "Callback for Information Job creation/modification",
79 description = "The call is invoked to activate or to modify a data subscription. The endpoint is provided by the Information Producer.")
80 @ApiResponses(value = { //
81 @ApiResponse(responseCode = "200", description = "OK", //
82 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
83 @ApiResponse(responseCode = "404", description = "Information type is not found", //
84 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
85 @ApiResponse(responseCode = "400", description = "Other error in the request", //
86 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
88 public ResponseEntity<Object> jobCreatedCallback( //
89 @RequestBody String body) {
91 ProducerJobInfo request = gson.fromJson(body, ProducerJobInfo.class);
92 logger.debug("Job started callback id: {}", request.id);
93 this.jobs.addJob(request.id, types.getType(request.typeId), request.owner, request.lastUpdated,
94 toJobParameters(request.jobData));
95 return new ResponseEntity<>(HttpStatus.OK);
96 } catch (ServiceException e) {
97 logger.warn("jobCreatedCallback failed: {}", e.getMessage());
98 return ErrorResponse.create(e, e.getHttpStatus());
99 } catch (Exception e) {
100 logger.warn("jobCreatedCallback failed: {}", e.getMessage(), e);
101 return ErrorResponse.create(e, HttpStatus.BAD_REQUEST);
105 private Job.Parameters toJobParameters(Object jobData) {
106 String json = gson.toJson(jobData);
107 return gson.fromJson(json, Job.Parameters.class);
110 @GetMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
111 @Operation(summary = "Get all jobs", description = "Returns all info jobs, can be used for trouble shooting")
112 @ApiResponse(responseCode = "200", //
113 description = "Information jobs", //
114 content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))) //
115 public ResponseEntity<Object> getJobs() {
117 Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
118 for (Job j : this.jobs.getAll()) {
120 .add(new ProducerJobInfo(null, j.getId(), j.getType().getId(), j.getOwner(), j.getLastUpdated()));
122 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
125 @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
126 @Operation(summary = "Callback for Information Job deletion",
127 description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
128 @ApiResponses(value = { //
129 @ApiResponse(responseCode = "200", description = "OK", //
130 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
132 public ResponseEntity<Object> jobDeletedCallback( //
133 @PathVariable("infoJobId") String infoJobId) {
135 logger.debug("Job deleted callback {}", infoJobId);
136 this.jobs.remove(infoJobId);
137 return new ResponseEntity<>(HttpStatus.OK);
140 @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
141 @Operation(summary = "Producer supervision",
142 description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
143 @ApiResponses(value = { //
144 @ApiResponse(responseCode = "200", description = "The producer is OK", //
145 content = @Content(schema = @Schema(implementation = String.class))) //
147 public ResponseEntity<Object> producerSupervision() {
148 logger.debug("Producer supervision");
149 return new ResponseEntity<>(HttpStatus.OK);
152 @Schema(name = "statistics_info", description = "Statistics information")
153 public class StatisticsCollection {
155 @Schema(description = "Statistics per job")
156 public final Collection<Job.Statistics> jobStatistics;
158 public StatisticsCollection(Collection<Job.Statistics> stats) {
159 this.jobStatistics = stats;
163 @GetMapping(path = STATISTICS_URL, produces = MediaType.APPLICATION_JSON_VALUE)
164 @Operation(summary = "Returns statistics", description = "")
165 @ApiResponses(value = { //
166 @ApiResponse(responseCode = "200", description = "OK", //
167 content = @Content(schema = @Schema(implementation = StatisticsCollection.class))) //
169 public ResponseEntity<Object> getStatistics() {
170 List<Job.Statistics> res = new ArrayList<>();
171 for (Job job : this.jobs.getAll()) {
172 res.add(job.getStatistics());
175 return new ResponseEntity<>(gson.toJson(new StatisticsCollection(res)), HttpStatus.OK);