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