X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-adaptor-java%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fdmaapadapter%2Ftasks%2FProducerRegstrationTask.java;h=e8b236c963a468f417d33d3bb2400ebfdeea85d2;hb=b2d6339441c650962e34502e7527ca0835fa342f;hp=837ca323d95b0ee44e7ccb0406cbdf0cea32ec9e;hpb=54e5f0a956ea4a9f5a31b3b6319f8f3b2b980c91;p=nonrtric.git diff --git a/dmaap-adaptor-java/src/main/java/org/oran/dmaapadapter/tasks/ProducerRegstrationTask.java b/dmaap-adaptor-java/src/main/java/org/oran/dmaapadapter/tasks/ProducerRegstrationTask.java index 837ca323..e8b236c9 100644 --- a/dmaap-adaptor-java/src/main/java/org/oran/dmaapadapter/tasks/ProducerRegstrationTask.java +++ b/dmaap-adaptor-java/src/main/java/org/oran/dmaapadapter/tasks/ProducerRegstrationTask.java @@ -20,14 +20,21 @@ package org.oran.dmaapadapter.tasks; +import com.google.common.io.CharStreams; import com.google.gson.JsonParser; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + import lombok.Getter; import org.oran.dmaapadapter.clients.AsyncRestClient; import org.oran.dmaapadapter.clients.AsyncRestClientFactory; import org.oran.dmaapadapter.configuration.ApplicationConfig; import org.oran.dmaapadapter.controllers.ProducerCallbacksController; +import org.oran.dmaapadapter.exceptions.ServiceException; import org.oran.dmaapadapter.r1.ProducerInfoTypeInfo; import org.oran.dmaapadapter.r1.ProducerRegistrationInfo; import org.oran.dmaapadapter.repository.InfoType; @@ -71,7 +78,7 @@ public class ProducerRegstrationTask { @Scheduled(fixedRate = REGISTRATION_SUPERVISION_INTERVAL_MS) public void supervisionTask() { checkRegistration() // - .filter(isRegisterred -> !isRegisterred) // + .filter(isRegistrationOk -> !isRegistrationOk || !this.isRegisteredInEcs) // .flatMap(isRegisterred -> registerTypesAndProducer()) // .subscribe( // null, // @@ -80,13 +87,12 @@ public class ProducerRegstrationTask { } private void handleRegistrationCompleted() { - logger.debug("Registering types and producer succeeded"); + logger.debug("Registering types and producer completed"); isRegisteredInEcs = true; } private void handleRegistrationFailure(Throwable t) { - logger.warn("Registration failed {}", t.getMessage()); - isRegisteredInEcs = false; + logger.warn("Registration of producer failed {}", t.getMessage()); } private Mono checkRegistration() { @@ -112,12 +118,12 @@ public class ProducerRegstrationTask { private Mono registerTypesAndProducer() { final int CONCURRENCY = 20; - final String producerUrl = - applicationConfig.getEcsBaseUrl() + "/data-producer/v1/info-producers/" + PRODUCER_ID; + final String producerUrl = applicationConfig.getEcsBaseUrl() + "/data-producer/v1/info-producers/" + + PRODUCER_ID; return Flux.fromIterable(this.types.getAll()) // .doOnNext(type -> logger.info("Registering type {}", type.getId())) // - .flatMap(type -> restClient.put(registerTypeUrl(type), gson.toJson(typeRegistrationInfo())), + .flatMap(type -> restClient.put(registerTypeUrl(type), gson.toJson(typeRegistrationInfo(type))), CONCURRENCY) // .collectList() // .doOnNext(type -> logger.info("Registering producer")) // @@ -128,18 +134,39 @@ public class ProducerRegstrationTask { return jsonObject("{}"); } - private ProducerInfoTypeInfo typeRegistrationInfo() { - return new ProducerInfoTypeInfo(jsonSchemaObject(), typeSpecifcInfoObject()); + private ProducerInfoTypeInfo typeRegistrationInfo(InfoType type) { + try { + return new ProducerInfoTypeInfo(jsonSchemaObject(type), typeSpecifcInfoObject()); + } catch (Exception e) { + logger.error("Fatal error {}", e.getMessage()); + return null; + } + } + + private Object jsonSchemaObject(InfoType type) throws IOException, ServiceException { + + if (type.isKafkaTopicDefined()) { + String schemaStrKafka = readSchemaFile("/typeSchemaKafka.json"); + return jsonObject(schemaStrKafka); + } else { + // An object with no properties + String schemaStr = "{" // + + "\"type\": \"object\"," // + + "\"properties\": {}," // + + "\"additionalProperties\": false" // + + "}"; // + + return jsonObject(schemaStr); + } } - private Object jsonSchemaObject() { - // An object with no properties - String schemaStr = "{" // - + "\"type\": \"object\"," // - + "\"properties\": {}," // - + "\"additionalProperties\": false" // - + "}"; // - return jsonObject(schemaStr); + private String readSchemaFile(String filePath) throws IOException, ServiceException { + InputStream in = getClass().getResourceAsStream(filePath); + logger.debug("Reading application schema file from: {} with: {}", filePath, in); + if (in == null) { + throw new ServiceException("Could not readfile: " + filePath); + } + return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8)); } private Object jsonObject(String json) {