NONRTRIC - Implement DMaaP mediator producer service in Java
[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
86             logger.info("Job started callback {}", request.id);
87             Job job = new Job(request.id, request.targetUri, types.getType(request.typeId), request.owner,
88                     request.lastUpdated, toJobParameters(request.jobData));
89             this.jobs.put(job);
90             return new ResponseEntity<>(HttpStatus.OK);
91         } catch (Exception e) {
92             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
93         }
94     }
95
96     private Job.Parameters toJobParameters(Object jobData) {
97         String json = gson.toJson(jobData);
98         return gson.fromJson(json, Job.Parameters.class);
99     }
100
101     @GetMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
102     @Operation(summary = "Get all jobs", description = "Returns all info jobs, can be used for trouble shooting")
103     @ApiResponse(responseCode = "200", //
104             description = "Information jobs", //
105             content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))) //
106     public ResponseEntity<Object> getJobs() {
107
108         Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
109         for (Job j : this.jobs.getAll()) {
110             producerJobs.add(new ProducerJobInfo(null, j.getId(), j.getType().getId(), j.getCallbackUrl(), j.getOwner(),
111                     j.getLastUpdated()));
112         }
113         return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
114     }
115
116     @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
117     @Operation(summary = "Callback for Information Job deletion",
118             description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
119     @ApiResponses(value = { //
120             @ApiResponse(responseCode = "200", description = "OK", //
121                     content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
122     })
123     public ResponseEntity<Object> jobDeletedCallback( //
124             @PathVariable("infoJobId") String infoJobId) {
125
126         logger.info("Job deleted callback {}", infoJobId);
127         this.jobs.remove(infoJobId);
128         return new ResponseEntity<>(HttpStatus.OK);
129     }
130
131     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
132     @Operation(summary = "Producer supervision",
133             description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
134     @ApiResponses(value = { //
135             @ApiResponse(responseCode = "200", description = "The producer is OK", //
136                     content = @Content(schema = @Schema(implementation = String.class))) //
137     })
138     public ResponseEntity<Object> producerSupervision() {
139         logger.debug("Producer supervision");
140         return new ResponseEntity<>(HttpStatus.OK);
141     }
142
143 }