4d99c58dfebb6d58791f29b83be579e4b963da06
[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.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.responses.ApiResponses;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32
33 import org.oran.dmaapadapter.r1.ProducerJobInfo;
34 import org.oran.dmaapadapter.repository.InfoTypes;
35 import org.oran.dmaapadapter.repository.Job;
36 import org.oran.dmaapadapter.repository.Jobs;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RestController;
49
50 @RestController("ConfigurationControllerV2")
51 @Tag(name = ProducerCallbacksController.API_NAME)
52 public class ProducerCallbacksController {
53     private static final Logger logger = LoggerFactory.getLogger(ProducerCallbacksController.class);
54
55     public static final String API_NAME = "Management of configuration";
56     public static final String API_DESCRIPTION = "";
57     public static final String JOB_URL = "/dmaap_dataproducer/info_job";
58     public static final String SUPERVISION_URL = "/dmaap_dataproducer/health_check";
59     private static Gson gson = new GsonBuilder().create();
60     private final Jobs jobs;
61     private final InfoTypes types;
62
63     public ProducerCallbacksController(@Autowired Jobs jobs, @Autowired InfoTypes types) {
64         this.jobs = jobs;
65         this.types = types;
66     }
67
68     @PostMapping(path = JOB_URL, produces = MediaType.APPLICATION_JSON_VALUE)
69     @Operation(summary = "Callback for Information Job creation/modification",
70             description = "The call is invoked to activate or to modify a data subscription. The endpoint is provided by the Information Producer.")
71     @ApiResponses(value = { //
72             @ApiResponse(responseCode = "200", description = "OK", //
73                     content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
74     })
75     public ResponseEntity<Object> jobCreatedCallback( //
76             @RequestBody String body) {
77         try {
78             ProducerJobInfo request = gson.fromJson(body, ProducerJobInfo.class);
79
80             logger.info("Job started callback {}", request.id);
81             Job job = new Job(request.id, request.targetUri, types.getType(request.typeId));
82             this.jobs.put(job);
83             return new ResponseEntity<>(HttpStatus.OK);
84         } catch (Exception e) {
85             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
86         }
87     }
88
89     @DeleteMapping(path = JOB_URL + "/{infoJobId}", produces = MediaType.APPLICATION_JSON_VALUE)
90     @Operation(summary = "Callback for Information Job deletion",
91             description = "The call is invoked to terminate a data subscription. The endpoint is provided by the Information Producer.")
92     @ApiResponses(value = { //
93             @ApiResponse(responseCode = "200", description = "OK", //
94                     content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
95     })
96     public ResponseEntity<Object> jobDeletedCallback( //
97             @PathVariable("infoJobId") String infoJobId) {
98         try {
99             logger.info("Job deleted callback {}", infoJobId);
100             this.jobs.remove(infoJobId);
101             return new ResponseEntity<>(HttpStatus.OK);
102         } catch (Exception e) {
103             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
104         }
105     }
106
107     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
108     @Operation(summary = "Producer supervision",
109             description = "The endpoint is provided by the Information Producer and is used for supervision of the producer.")
110     @ApiResponses(value = { //
111             @ApiResponse(responseCode = "200", description = "The producer is OK", //
112                     content = @Content(schema = @Schema(implementation = String.class))) //
113     })
114     public ResponseEntity<Object> producerSupervision() {
115         logger.info("Producer supervision");
116         return new ResponseEntity<>(HttpStatus.OK);
117     }
118
119 }