Updating FTC 2001, 10, 1 series tests to support a1pms V3 image
[nonrtric.git] / sample-services / ics-producer-consumer / consumer / src / main / java / com / demo / consumer / controllers / ConsumerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  *
5  * Copyright (C) 2024: OpenInfra Foundation Europe
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 com.demo.consumer.controllers;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.web.bind.annotation.PathVariable;
27 import org.springframework.web.bind.annotation.PostMapping;
28 import org.springframework.web.bind.annotation.RequestBody;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.bind.annotation.RestController;
31
32 import com.demo.consumer.repository.InfoType;
33 import com.demo.consumer.repository.InfoTypes;
34 import com.demo.consumer.repository.Job.Parameters;
35 import com.demo.consumer.repository.Job.Parameters.KafkaDeliveryInfo;
36 import com.demo.consumer.dme.ConsumerJobInfo;
37 import com.demo.consumer.dme.JobDataSchema;
38 import com.demo.consumer.repository.Jobs;
39 import com.google.gson.Gson;
40 import com.google.gson.GsonBuilder;
41
42 @RestController
43 @RequestMapping(path = "/consumer", produces = "application/json")
44 public class ConsumerController {
45     private static final Logger log = LoggerFactory.getLogger(ConsumerController.class);
46
47     private static Gson gson = new GsonBuilder().create();
48
49     private final Jobs jobs;
50     private final InfoTypes types;
51
52     public ConsumerController(@Autowired Jobs jobs, @Autowired InfoTypes types) {
53         this.jobs = jobs;
54         this.types = types;
55         InfoType type1 = InfoType.builder().build();
56         Parameters p = Parameters.builder().build();
57         type1.setId("type1");
58         type1.setKafkaInputTopic("mytopic");
59         type1.setInputJobType("type1");
60         type1.setInputJobDefinition(p);
61         types.put(type1);
62     }
63
64     @PostMapping("/job/{infoJobId}")
65     public void startinfojob(@RequestBody String requestBody, @PathVariable String infoJobId) {
66         ConsumerJobInfo request = gson.fromJson(requestBody, ConsumerJobInfo.class);
67         log.info("Add Job Info" + infoJobId, request);
68         try {
69             this.jobs.addJob(request.infoTypeId, types.getType(request.infoTypeId), request.owner,
70                     toJobParameters(request.jobDefinition));
71         } catch (Exception e) {
72             log.error("Error adding the job " + infoJobId + "{}", e.getMessage());
73         }
74     }
75
76     @PostMapping("/info-type-status")
77     public void statusChange(@RequestBody String requestBody) {
78         JobDataSchema request = gson.fromJson(requestBody, JobDataSchema.class);
79         log.debug("Body Received: {}" , requestBody);
80         try {
81             this.jobs.addJob(request.getInfo_type_id(), types.getType(request.getInfo_type_id()), "",
82                 new Parameters(new KafkaDeliveryInfo(
83                         request.getJob_data_schema().getTopic(),
84                         request.getJob_data_schema().getBootStrapServers(), 0)));
85         } catch (Exception e) {
86             log.error("Error adding the info type " + request.getInfo_type_id() + "{}", e.getMessage());
87         }
88     }
89
90     private Parameters toJobParameters(Object jobData) {
91         String json = gson.toJson(jobData);
92         return gson.fromJson(json, Parameters.class);
93     }
94 }