Storage of PM Data
[nonrtric.git] / pmlog / src / main / java / org / oran / pmlog / ConsumerRegstrationTask.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 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.pmlog;
22
23 import java.time.Duration;
24
25 import lombok.Getter;
26
27 import org.oran.pmlog.clients.AsyncRestClient;
28 import org.oran.pmlog.clients.AsyncRestClientFactory;
29 import org.oran.pmlog.clients.SecurityContext;
30 import org.oran.pmlog.configuration.ApplicationConfig;
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;
36
37 import reactor.core.publisher.Mono;
38 import reactor.util.retry.Retry;
39
40 /**
41  * Registers the types and this producer in Innformation Coordinator Service.
42  * This is done when needed.
43  */
44 @Component
45 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
46 public class ConsumerRegstrationTask {
47
48     private static final Logger logger = LoggerFactory.getLogger(ConsumerRegstrationTask.class);
49     private final AsyncRestClient restClient;
50     private final ApplicationConfig applicationConfig;
51
52     private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder() //
53             .disableHtmlEscaping() //
54             .excludeFieldsWithoutExposeAnnotation() //
55             .create();
56
57     @Getter
58     private boolean isRegisteredInIcs = false;
59
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;
66
67         if (this.applicationConfig.getIcsBaseUrl().isEmpty()) {
68             logger.info("Skipping createtion of info job. app.ics-base-url is empty.");
69         } else {
70             createSubscription();
71         }
72     }
73
74     private void createSubscription() {
75         putInfoJob() //
76                 .doOnError(this::handleRegistrationFailure)
77                 .retryWhen(Retry.fixedDelay(100, Duration.ofMillis(5 * 1000))) //
78                 .subscribe( //
79                         null, //
80                         this::handleRegistrationFailure, //
81                         this::handleRegistrationCompleted);
82     }
83
84     private void handleRegistrationCompleted() {
85         logger.info("Registration of subscription/info job succeeded");
86         isRegisteredInIcs = true;
87     }
88
89     private void handleRegistrationFailure(Throwable t) {
90         logger.warn("Creation of subscription/info job failed {}", t.getMessage());
91     }
92
93     private Mono<ResponseEntity<String>> putInfoJob() {
94         try {
95             String jobId = this.applicationConfig.getConsumerJobId();
96
97             if (jobId.isBlank()) {
98                 logger.info("Skipping creation of infojob. job_id is empty.");
99                 return Mono.empty();
100             }
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);
107         }
108     }
109
110 }