2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 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.dmaapadapter.tasks;
23 import com.google.gson.JsonParser;
27 import org.oran.dmaapadapter.clients.AsyncRestClient;
28 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
29 import org.oran.dmaapadapter.configuration.ApplicationConfig;
30 import org.oran.dmaapadapter.controllers.ProducerCallbacksController;
31 import org.oran.dmaapadapter.r1.ProducerInfoTypeInfo;
32 import org.oran.dmaapadapter.r1.ProducerRegistrationInfo;
33 import org.oran.dmaapadapter.repository.InfoType;
34 import org.oran.dmaapadapter.repository.InfoTypes;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.scheduling.annotation.EnableScheduling;
39 import org.springframework.scheduling.annotation.Scheduled;
40 import org.springframework.stereotype.Component;
42 import reactor.core.publisher.Flux;
43 import reactor.core.publisher.Mono;
46 * Registers the types and this producer in ECS. This is done when needed.
50 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
51 public class ProducerRegstrationTask {
53 private static final Logger logger = LoggerFactory.getLogger(ProducerRegstrationTask.class);
54 private final AsyncRestClient restClient;
55 private final ApplicationConfig applicationConfig;
56 private final InfoTypes types;
57 private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
59 private static final String PRODUCER_ID = "DmaapGenericInfoProducer";
61 private boolean isRegisteredInEcs = false;
62 private static final int REGISTRATION_SUPERVISION_INTERVAL_MS = 1000 * 5;
64 public ProducerRegstrationTask(@Autowired ApplicationConfig applicationConfig, @Autowired InfoTypes types) {
65 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
66 this.restClient = restClientFactory.createRestClientNoHttpProxy("");
67 this.applicationConfig = applicationConfig;
71 @Scheduled(fixedRate = REGISTRATION_SUPERVISION_INTERVAL_MS)
72 public void supervisionTask() {
73 checkRegistration() //
74 .filter(isRegisterred -> !isRegisterred) //
75 .flatMap(isRegisterred -> registerTypesAndProducer()) //
78 this::handleRegistrationFailure, //
79 this::handleRegistrationCompleted);
82 private void handleRegistrationCompleted() {
83 logger.debug("Registering types and producer succeeded");
84 isRegisteredInEcs = true;
87 private void handleRegistrationFailure(Throwable t) {
88 logger.warn("Registration failed {}", t.getMessage());
89 isRegisteredInEcs = false;
92 private Mono<Boolean> checkRegistration() {
93 final String url = applicationConfig.getEcsBaseUrl() + "/data-producer/v1/info-producers/" + PRODUCER_ID;
94 return restClient.get(url) //
95 .flatMap(this::isRegisterredInfoCorrect) //
96 .onErrorResume(t -> Mono.just(Boolean.FALSE));
99 private Mono<Boolean> isRegisterredInfoCorrect(String registerredInfoStr) {
100 ProducerRegistrationInfo registerredInfo = gson.fromJson(registerredInfoStr, ProducerRegistrationInfo.class);
101 if (isEqual(producerRegistrationInfo(), registerredInfo)) {
102 logger.trace("Already registered");
103 return Mono.just(Boolean.TRUE);
105 return Mono.just(Boolean.FALSE);
109 private String registerTypeUrl(InfoType type) {
110 return applicationConfig.getEcsBaseUrl() + "/data-producer/v1/info-types/" + type.getId();
113 private Mono<String> registerTypesAndProducer() {
114 final int CONCURRENCY = 20;
115 final String producerUrl =
116 applicationConfig.getEcsBaseUrl() + "/data-producer/v1/info-producers/" + PRODUCER_ID;
118 return Flux.fromIterable(this.types.getAll()) //
119 .doOnNext(type -> logger.info("Registering type {}", type.getId())) //
120 .flatMap(type -> restClient.put(registerTypeUrl(type), gson.toJson(typeRegistrationInfo())),
123 .doOnNext(type -> logger.info("Registering producer")) //
124 .flatMap(resp -> restClient.put(producerUrl, gson.toJson(producerRegistrationInfo())));
127 private Object typeSpecifcInfoObject() {
128 return jsonObject("{}");
131 private ProducerInfoTypeInfo typeRegistrationInfo() {
132 return new ProducerInfoTypeInfo(jsonSchemaObject(), typeSpecifcInfoObject());
135 private Object jsonSchemaObject() {
136 // An object with no properties
137 String schemaStr = "{" //
138 + "\"type\": \"object\"," //
139 + "\"properties\": {}," //
140 + "\"additionalProperties\": false" //
142 return jsonObject(schemaStr);
145 private Object jsonObject(String json) {
147 return JsonParser.parseString(json).getAsJsonObject();
148 } catch (Exception e) {
149 logger.error("Bug, error in JSON: {}", json);
150 throw new NullPointerException(e.toString());
154 private boolean isEqual(ProducerRegistrationInfo a, ProducerRegistrationInfo b) {
155 return a.jobCallbackUrl.equals(b.jobCallbackUrl) //
156 && a.producerSupervisionCallbackUrl.equals(b.producerSupervisionCallbackUrl) //
157 && a.supportedTypeIds.size() == b.supportedTypeIds.size();
160 private ProducerRegistrationInfo producerRegistrationInfo() {
162 return ProducerRegistrationInfo.builder() //
163 .jobCallbackUrl(baseUrl() + ProducerCallbacksController.JOB_URL) //
164 .producerSupervisionCallbackUrl(baseUrl() + ProducerCallbacksController.SUPERVISION_URL) //
165 .supportedTypeIds(this.types.typeIds()) //
169 private String baseUrl() {
170 return this.applicationConfig.getSelfUrl();