2 * ========================LICENSE_START=================================
5 * Copyright (C) 2024: OpenInfra Foundation Europe
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package com.demo.producer.controllers;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.http.ResponseEntity;
28 import org.springframework.web.bind.annotation.DeleteMapping;
29 import org.springframework.web.bind.annotation.GetMapping;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.PostMapping;
32 import org.springframework.web.bind.annotation.RequestBody;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RestController;
36 import com.demo.producer.repository.InfoType;
37 import com.demo.producer.repository.InfoTypes;
38 import com.demo.producer.repository.Job.Parameters;
39 import com.demo.producer.repository.Jobs;
40 import com.demo.producer.dme.ProducerJobInfo;
41 import com.demo.producer.messages.KafkaMessageHandlerImpl;
42 import com.demo.producer.producer.SimpleProducer;
43 import com.google.gson.Gson;
44 import com.google.gson.GsonBuilder;
47 @RequestMapping(path = "/producer", produces = "application/json")
48 public class ProducerController {
49 private static final Logger log = LoggerFactory.getLogger(ProducerController.class);
50 private static Gson gson = new GsonBuilder().create();
51 private final Jobs jobs;
52 private final InfoTypes types;
53 private String topicName = "mytopic";
55 public ProducerController(@Autowired Jobs jobs, @Autowired InfoTypes types) {
58 InfoType type1 = InfoType.builder().build();
60 type1.setKafkaInputTopic(topicName);
61 type1.setInputJobType("type1");
62 type1.setInputJobDefinition(null);
66 @GetMapping("/publish/{numberOfMessages}")
67 public ResponseEntity<?> publishMessage(@PathVariable int numberOfMessages) {
69 new SimpleProducer().run(topicName, numberOfMessages, new KafkaMessageHandlerImpl());
70 return ResponseEntity.ok("message published successfully ..");
71 } catch (Exception ex) {
72 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
77 @PostMapping("/job/{infoJobId}")
78 public void jobCallback(@RequestBody String requestBody, @PathVariable String infoJobId) {
79 ProducerJobInfo request = gson.fromJson(requestBody, ProducerJobInfo.class);
81 log.info("Adding producer job info " + request.toString());
82 this.jobs.addJob(request.id, types.getType(request.typeId), request.owner,
83 toJobParameters(request.jobData));
84 } catch (Exception e) {
85 log.error("Error adding producer job info: " + request.toString(), e.getMessage());
90 public void jobCallbackNoId(@RequestBody String requestBody) {
91 ProducerJobInfo request = gson.fromJson(requestBody, ProducerJobInfo.class);
93 log.info("Adding producer job info "+request.toString());
94 this.jobs.addJob(request.id, types.getType(request.typeId), request.owner,
95 toJobParameters(request.jobData));
96 } catch (Exception e) {
97 log.error("Error adding producer job info: " + request.toString(), e.getMessage());
101 private Parameters toJobParameters(Object jobData) {
102 String json = gson.toJson(jobData);
103 return gson.fromJson(json, Parameters.class);
107 public ResponseEntity<String> getJobs() {
109 log.info("Get all jobs");
110 return new ResponseEntity<>(this.jobs.getAll().toString(), HttpStatus.OK);
111 } catch (Exception e) {
112 log.error("Error finding jobs", e.getMessage());
113 return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
117 @DeleteMapping("/job/{infoJobId}")
118 public ResponseEntity<String> deleteJob(@PathVariable String infoJobId) {
120 log.info("Delete Job" + infoJobId);
121 this.jobs.delete(infoJobId);
122 return new ResponseEntity<>("Deleted job:" + infoJobId, HttpStatus.OK);
123 } catch (Exception e) {
124 log.error("Error finding job " + infoJobId, e.getMessage());
125 return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
129 @GetMapping("/supervision")
130 public ResponseEntity<String> getSupervision() {
131 log.info("Get Supervision");
132 return new ResponseEntity<>("Ok", HttpStatus.OK);