Merge "ICS tests with istio and JWTs"
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / controllers / ProducerCallbacksController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.dmaapadapter.controllers;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
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;
33
34 import java.util.ArrayList;
35 import java.util.Collection;
36
37 import org.oran.dmaapadapter.exceptions.ServiceException;
38 import org.oran.dmaapadapter.r1.ProducerJobInfo;
39 import org.oran.dmaapadapter.repository.InfoTypes;
40 import org.oran.dmaapadapter.repository.Job;
41 import org.oran.dmaapadapter.repository.Jobs;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
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.DeleteMapping;
49 import org.springframework.web.bind.annotation.GetMapping;
50 import org.springframework.web.bind.annotation.PathVariable;
51 import org.springframework.web.bind.annotation.PostMapping;
52 import org.springframework.web.bind.annotation.RequestBody;
53 import org.springframework.web.bind.annotation.RestController;
54
55 @RestController("ConfigurationControllerV2")
56 @Tag(name = ProducerCallbacksController.API_NAME)
57 public class ProducerCallbacksController {
58     private static final Logger logger = LoggerFactory.getLogger(ProducerCallbacksController.class);
59
60     public static final String API_NAME = "Producer job control API";
61     public static final String API_DESCRIPTION = "";
62     public static final String JOB_URL = "/generic_dataproducer/info_job";
63     public static final String SUPERVISION_URL = "/generic_dataproducer/health_check";
64     private static Gson gson = new GsonBuilder().create();
65     private final Jobs jobs;
66     private final InfoTypes types;
67
68     public ProducerCallbacksController(@Autowired Jobs jobs, @Autowired InfoTypes types) {
69         this.jobs = jobs;
70         this.types = types;
71     }
72
73     @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
74     @Operation(summary = "Callback for Information Job creation/modification",
75             description = "The call is invoked to activate or to modify a data subscription. The endpoint is provided by the Information Producer.")
76     @ApiResponses(value = { //
77             @ApiResponse(responseCode = "200", description = "OK", //
78                     content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
79             @ApiResponse(responseCode = "404", description = "Information type is not found", //
80                     content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
81             @ApiResponse(responseCode = "400", description = "Other error in the request", //
82                     content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
83     })
84     public ResponseEntity<Object> jobCreatedCallback( //
85             @RequestBody String body) {
86         try {
87             ProducerJobInfo request = gson.fromJson(body, ProducerJobInfo.class);
88             logger.debug("Job started callback {}", request.id);
89             this.jobs.addJob(request.id, request.targetUri, types.getType(request.typeId), request.owner,
90                     request.lastUpdated, toJobParameters(request.jobData));
91             return new ResponseEntity<>(HttpStatus.OK);
92         } catch (ServiceException e) {
93             logger.warn("jobCreatedCallback failed: {}", e.getMessage());
94             return ErrorResponse.create(e, e.getHttpStatus());
95         } catch (Exception e) {
96             logger.warn("jobCreatedCallback failed: {}", e.getMessage());
97             return ErrorResponse.create(e, HttpStatus.BAD_REQUEST);
98         }
99     }
100
101     private Job.Parameters toJobParameters(Object jobData) {
102         String json = gson.toJson(jobData);
103         return gson.fromJson(json, Job.Parameters.class);
104     }
105
106     @GetMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
107     @Operation(summary = "Get all jobs", description = "Returns all info jobs, can be used for trouble shooting")
108     @ApiResponse(responseCode = "200", //
109             description = "Information jobs", //
110             content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))) //
111     public ResponseEntity<Object> getJobs() {
112
113         Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
114         for (Job j : this.jobs.getAll()) {
115             producerJobs.add(new ProducerJobInfo(null, j.getId(), j.getType().getId(), j.getCallbackUrl(), j.getOwner(),
116                     j.getLastUpdated()));
117         }
118         return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
119     }
120
121     @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
122     @Operation(summary = "Callback for Information Job deletion",
123             description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
124     @ApiResponses(value = { //
125             @ApiResponse(responseCode = "200", description = "OK", //
126                     content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
127     })
128     public ResponseEntity<Object> jobDeletedCallback( //
129             @PathVariable("infoJobId") String infoJobId) {
130
131         logger.debug("Job deleted callback {}", infoJobId);
132         this.jobs.remove(infoJobId);
133         return new ResponseEntity<>(HttpStatus.OK);
134     }
135
136     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
137     @Operation(summary = "Producer supervision",
138             description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
139     @ApiResponses(value = { //
140             @ApiResponse(responseCode = "200", description = "The producer is OK", //
141                     content = @Content(schema = @Schema(implementation = String.class))) //
142     })
143     public ResponseEntity<Object> producerSupervision() {
144         logger.debug("Producer supervision");
145         return new ResponseEntity<>(HttpStatus.OK);
146     }
147
148 }