Merge "ICS tests with istio and JWTs"
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / tasks / ProducerRegstrationTask.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.dmaapadapter.tasks;
22
23 import com.google.common.io.CharStreams;
24 import com.google.gson.JsonParser;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.nio.charset.StandardCharsets;
30
31 import lombok.Getter;
32
33 import org.oran.dmaapadapter.clients.AsyncRestClient;
34 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
35 import org.oran.dmaapadapter.configuration.ApplicationConfig;
36 import org.oran.dmaapadapter.controllers.ProducerCallbacksController;
37 import org.oran.dmaapadapter.exceptions.ServiceException;
38 import org.oran.dmaapadapter.r1.ProducerInfoTypeInfo;
39 import org.oran.dmaapadapter.r1.ProducerRegistrationInfo;
40 import org.oran.dmaapadapter.repository.InfoType;
41 import org.oran.dmaapadapter.repository.InfoTypes;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.scheduling.annotation.EnableScheduling;
47 import org.springframework.scheduling.annotation.Scheduled;
48 import org.springframework.stereotype.Component;
49
50 import reactor.core.publisher.Flux;
51 import reactor.core.publisher.Mono;
52
53 /**
54  * Registers the types and this producer in Innformation Coordinator Service.
55  * This is done when needed.
56  */
57 @Component
58 @EnableScheduling
59 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
60 public class ProducerRegstrationTask {
61
62     private static final Logger logger = LoggerFactory.getLogger(ProducerRegstrationTask.class);
63     private final AsyncRestClient restClient;
64     private final ApplicationConfig applicationConfig;
65     private final InfoTypes types;
66     private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
67
68     private static final String PRODUCER_ID = "DmaapGenericInfoProducer";
69     @Getter
70     private boolean isRegisteredInIcs = false;
71     private static final int REGISTRATION_SUPERVISION_INTERVAL_MS = 1000 * 5;
72
73     public ProducerRegstrationTask(@Autowired ApplicationConfig applicationConfig, @Autowired InfoTypes types) {
74         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
75         this.restClient = restClientFactory.createRestClientNoHttpProxy("");
76         this.applicationConfig = applicationConfig;
77         this.types = types;
78     }
79
80     @Scheduled(fixedRate = REGISTRATION_SUPERVISION_INTERVAL_MS)
81     public void runSupervisionTask() {
82         supervisionTask().subscribe( //
83                 null, //
84                 this::handleRegistrationFailure, //
85                 this::handleRegistrationCompleted);
86     }
87
88     public Mono<String> supervisionTask() {
89         return checkRegistration() //
90                 .filter(isRegistrationOk -> !isRegistrationOk || !this.isRegisteredInIcs) //
91                 .flatMap(isRegisterred -> registerTypesAndProducer());
92     }
93
94     private void handleRegistrationCompleted() {
95         isRegisteredInIcs = true;
96     }
97
98     private void handleRegistrationFailure(Throwable t) {
99         logger.warn("Registration of producer failed {}", t.getMessage());
100     }
101
102     // Returns TRUE if registration is correct
103     private Mono<Boolean> checkRegistration() {
104         final String url = applicationConfig.getIcsBaseUrl() + "/data-producer/v1/info-producers/" + PRODUCER_ID;
105         return restClient.get(url) //
106                 .flatMap(this::isRegisterredInfoCorrect) //
107                 .onErrorResume(t -> Mono.just(Boolean.FALSE));
108     }
109
110     private Mono<Boolean> isRegisterredInfoCorrect(String registerredInfoStr) {
111         ProducerRegistrationInfo registerredInfo = gson.fromJson(registerredInfoStr, ProducerRegistrationInfo.class);
112         if (isEqual(producerRegistrationInfo(), registerredInfo)) {
113             logger.trace("Already registered in ICS");
114             return Mono.just(Boolean.TRUE);
115         } else {
116             return Mono.just(Boolean.FALSE);
117         }
118     }
119
120     private String registerTypeUrl(InfoType type) {
121         return applicationConfig.getIcsBaseUrl() + "/data-producer/v1/info-types/" + type.getId();
122     }
123
124     private Mono<String> registerTypesAndProducer() {
125         final int CONCURRENCY = 20;
126         final String producerUrl =
127                 applicationConfig.getIcsBaseUrl() + "/data-producer/v1/info-producers/" + PRODUCER_ID;
128
129         return Flux.fromIterable(this.types.getAll()) //
130                 .doOnNext(type -> logger.info("Registering type {}", type.getId())) //
131                 .flatMap(type -> restClient.put(registerTypeUrl(type), gson.toJson(typeRegistrationInfo(type))),
132                         CONCURRENCY) //
133                 .collectList() //
134                 .doOnNext(type -> logger.info("Registering producer")) //
135                 .flatMap(resp -> restClient.put(producerUrl, gson.toJson(producerRegistrationInfo())));
136     }
137
138     private Object typeSpecifcInfoObject() {
139         return jsonObject("{}");
140     }
141
142     private ProducerInfoTypeInfo typeRegistrationInfo(InfoType type) {
143         try {
144             return new ProducerInfoTypeInfo(jsonSchemaObject(type), typeSpecifcInfoObject());
145         } catch (Exception e) {
146             logger.error("Fatal error {}", e.getMessage());
147             return null;
148         }
149     }
150
151     private Object jsonSchemaObject(InfoType type) throws IOException, ServiceException {
152         String schemaFile = type.isKafkaTopicDefined() ? "/typeSchemaKafka.json" : "/typeSchemaDmaap.json";
153         return jsonObject(readSchemaFile(schemaFile));
154     }
155
156     private String readSchemaFile(String filePath) throws IOException, ServiceException {
157         InputStream in = getClass().getResourceAsStream(filePath);
158         logger.debug("Reading application schema file from: {} with: {}", filePath, in);
159         if (in == null) {
160             throw new ServiceException("Could not readfile: " + filePath, HttpStatus.INTERNAL_SERVER_ERROR);
161         }
162         return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
163     }
164
165     @SuppressWarnings("java:S2139") // Log exception
166     private Object jsonObject(String json) {
167         try {
168             return JsonParser.parseString(json).getAsJsonObject();
169         } catch (Exception e) {
170             logger.error("Bug, error in JSON: {} {}", json, e.getMessage());
171             throw new NullPointerException(e.getMessage());
172         }
173     }
174
175     private boolean isEqual(ProducerRegistrationInfo a, ProducerRegistrationInfo b) {
176         return a.jobCallbackUrl.equals(b.jobCallbackUrl) //
177                 && a.producerSupervisionCallbackUrl.equals(b.producerSupervisionCallbackUrl) //
178                 && a.supportedTypeIds.size() == b.supportedTypeIds.size();
179     }
180
181     private ProducerRegistrationInfo producerRegistrationInfo() {
182         return ProducerRegistrationInfo.builder() //
183                 .jobCallbackUrl(baseUrl() + ProducerCallbacksController.JOB_URL) //
184                 .producerSupervisionCallbackUrl(baseUrl() + ProducerCallbacksController.SUPERVISION_URL) //
185                 .supportedTypeIds(this.types.typeIds()) //
186                 .build();
187     }
188
189     private String baseUrl() {
190         return this.applicationConfig.getSelfUrl();
191     }
192 }