ca7c96cda83b20b5adb5b53b8e6aed142a5a22a6
[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);
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     @GetMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
97     @Operation(summary = "Get all jobs", description = "Returns all info jobs, can be used for trouble shooting")
98     @ApiResponse(responseCode = "200", //
99             description = "Information jobs", //
100             content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))) //
101     public ResponseEntity<Object> getJobs() {
102
103         Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
104         for (Job j : this.jobs.getAll()) {
105             producerJobs.add(new ProducerJobInfo(null, j.getId(), j.getType().getId(), j.getCallbackUrl(), j.getOwner(),
106                     j.getLastUpdated()));
107         }
108         return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
109     }
110
111     @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
112     @Operation(summary = "Callback for Information Job deletion",
113             description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
114     @ApiResponses(value = { //
115             @ApiResponse(responseCode = "200", description = "OK", //
116                     content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
117     })
118     public ResponseEntity<Object> jobDeletedCallback( //
119             @PathVariable("infoJobId") String infoJobId) {
120
121         logger.info("Job deleted callback {}", infoJobId);
122         this.jobs.remove(infoJobId);
123         return new ResponseEntity<>(HttpStatus.OK);
124     }
125
126     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
127     @Operation(summary = "Producer supervision",
128             description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
129     @ApiResponses(value = { //
130             @ApiResponse(responseCode = "200", description = "The producer is OK", //
131                     content = @Content(schema = @Schema(implementation = String.class))) //
132     })
133     public ResponseEntity<Object> producerSupervision() {
134         logger.debug("Producer supervision");
135         return new ResponseEntity<>(HttpStatus.OK);
136     }
137
138 }