2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 Nordix Foundation
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 org.oran.pmlog;
23 import java.time.Duration;
27 import org.oran.pmlog.clients.AsyncRestClient;
28 import org.oran.pmlog.clients.AsyncRestClientFactory;
29 import org.oran.pmlog.configuration.ApplicationConfig;
30 import org.oran.pmlog.oauth2.SecurityContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Component;
37 import reactor.core.publisher.Mono;
38 import reactor.util.retry.Retry;
41 * Registers the types and this producer in Innformation Coordinator Service.
42 * This is done when needed.
45 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
46 public class ConsumerRegstrationTask {
48 private static final Logger logger = LoggerFactory.getLogger(ConsumerRegstrationTask.class);
49 private final AsyncRestClient restClient;
50 private final ApplicationConfig applicationConfig;
52 private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder() //
53 .disableHtmlEscaping() //
54 .excludeFieldsWithoutExposeAnnotation() //
58 private boolean isRegisteredInIcs = false;
60 public ConsumerRegstrationTask(@Autowired ApplicationConfig applicationConfig,
61 @Autowired SecurityContext securityContext) {
62 AsyncRestClientFactory restClientFactory =
63 new AsyncRestClientFactory(applicationConfig.getWebClientConfig(), securityContext);
64 this.restClient = restClientFactory.createRestClientNoHttpProxy("");
65 this.applicationConfig = applicationConfig;
67 if (this.applicationConfig.getIcsBaseUrl().isEmpty()) {
68 logger.info("Skipping createtion of info job. app.ics-base-url is empty.");
74 private void createSubscription() {
76 .doOnError(this::handleRegistrationFailure)
77 .retryWhen(Retry.fixedDelay(100, Duration.ofMillis(5 * 1000))) //
80 this::handleRegistrationFailure, //
81 this::handleRegistrationCompleted);
84 private void handleRegistrationCompleted() {
85 logger.info("Registration of subscription/info job succeeded");
86 isRegisteredInIcs = true;
89 private void handleRegistrationFailure(Throwable t) {
90 logger.warn("Creation of subscription/info job failed {}", t.getMessage());
93 private Mono<ResponseEntity<String>> putInfoJob() {
95 String jobId = this.applicationConfig.getConsumerJobId();
97 if (jobId.isBlank()) {
98 logger.info("Skipping creation of infojob. job_id is empty.");
101 String url = applicationConfig.getIcsBaseUrl() + "/data-consumer/v1/info-jobs/" + jobId;
102 String body = this.applicationConfig.getConsumerJobInfo();
103 return restClient.putForEntity(url, body);
104 } catch (Exception e) {
105 logger.error("Registration of subscription failed {}", e.getMessage());
106 return Mono.error(e);