0a92a1012019886aafdf4070e01a7314bcde6356
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Modifications Copyright (C) 2025 OpenInfra Foundation Europe
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.oran.smo.teiv.adapters.focom_to_teiv_adapter;
21
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import io.cloudevents.CloudEvent;
25 import lombok.RequiredArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27 import org.oran.smo.teiv.adapters.focom_to_teiv_adapter.service.FocomToTeivModelBuilder;
28 import org.springframework.scheduling.annotation.Scheduled;
29 import org.springframework.stereotype.Component;
30
31 import java.io.IOException;
32 import java.util.Map;
33
34 @Slf4j
35 @Component
36 @RequiredArgsConstructor
37 public class FocomToTeivIngestion {
38
39     private static final ObjectMapper objectMapper = new ObjectMapper();
40     private final KafkaEventProducer kafkaEventProducer;
41     private final FocomToTeivModelBuilder jsonBuilder;
42
43     @Scheduled(fixedRateString = "${polling.interval}")
44     public void pollExternalApi() throws IOException {
45         Map<String, Object> json = jsonBuilder.getFocomtoTeivJson();
46         log.debug("Retrieved JSON for FOCOM_PROVISION_REQUEST_NAME: {}", json);
47         try {
48             sendCloudEvent(json, "merge");
49         } catch (IOException e) {
50             log.error("Failed to poll external API or send CloudEvent", e);
51         }
52     }
53
54     private void sendCloudEvent(Map<String, Object> json, String eventType) throws JsonProcessingException {
55         String payload = objectMapper.writeValueAsString(json);
56         CloudEvent event = CloudEventFactory.createEvent(payload, eventType);
57         log.info("Sending CloudEvent with payload: {}", payload);
58         kafkaEventProducer.sendCloudEvent(event);
59     }
60 }