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