9b238654ce1006d73ad8919f587209485fca2995
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / clients / ProducerCallbacks.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 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.oransc.enrichment.clients;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.lang.invoke.MethodHandles;
27
28 import org.oransc.enrichment.configuration.ApplicationConfig;
29 import org.oransc.enrichment.repository.EiJob;
30 import org.oransc.enrichment.repository.EiProducer;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import reactor.core.publisher.Flux;
35 import reactor.core.publisher.Mono;
36
37 /**
38  * Callbacks to the EiProducer
39  */
40 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
41 public class ProducerCallbacks {
42
43     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44     private static Gson gson = new GsonBuilder() //
45         .serializeNulls() //
46         .create(); //
47
48     private final AsyncRestClient restClient;
49
50     public ProducerCallbacks(ApplicationConfig config) {
51         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig());
52         this.restClient = restClientFactory.createRestClient("");
53     }
54
55     public void notifyProducersJobDeleted(EiJob eiJob) {
56         ProducerJobInfo request = new ProducerJobInfo(eiJob);
57         String body = gson.toJson(request);
58         for (EiProducer producer : eiJob.type().getProducers()) {
59             restClient.post(producer.getJobDeletionCallbackUrl(), body) //
60                 .subscribe(notUsed -> logger.debug("Job deleted OK {}", producer.getId()), //
61                     throwable -> logger.warn("Job delete failed {}", producer.getId(), throwable.toString()), null);
62         }
63     }
64
65     /**
66      * Calls all producers for an EiJob activation.
67      * 
68      * @param eiJob an EI job
69      * @return the number of producers that returned OK
70      */
71     public Mono<Integer> notifyProducersJobStarted(EiJob eiJob) {
72         return Flux.fromIterable(eiJob.type().getProducers()) //
73             .flatMap(eiProducer -> notifyProducerJobStarted(eiProducer, eiJob)) //
74             .collectList() //
75             .flatMap(okResponses -> Mono.just(Integer.valueOf(okResponses.size()))); //
76     }
77
78     /**
79      * Calls one producer for an EiJob activation.
80      * 
81      * @param producer a producer
82      * @param eiJob an EI job
83      * @return the body of the response from the REST call
84      */
85     public Mono<String> notifyProducerJobStarted(EiProducer producer, EiJob eiJob) {
86         ProducerJobInfo request = new ProducerJobInfo(eiJob);
87         String body = gson.toJson(request);
88
89         return restClient.post(producer.getJobCreationCallbackUrl(), body)
90             .doOnNext(resp -> logger.debug("Job subscription started OK {}", producer.getId()))
91             .onErrorResume(throwable -> {
92                 logger.warn("Job subscription failed {}", producer.getId(), throwable.toString());
93                 return Mono.empty();
94             });
95     }
96
97 }