15394ab6092ace6c065725e877ca082e74472a09
[nonrtric.git] / information-coordinator-service / src / main / java / org / oransc / ics / controllers / a1e / A1eCallbacks.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.ics.controllers.a1e;
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
29 import org.oransc.ics.clients.AsyncRestClient;
30 import org.oransc.ics.clients.AsyncRestClientFactory;
31 import org.oransc.ics.clients.SecurityContext;
32 import org.oransc.ics.configuration.ApplicationConfig;
33 import org.oransc.ics.repository.InfoJob;
34 import org.oransc.ics.repository.InfoJobs;
35 import org.oransc.ics.repository.InfoProducers;
36 import org.oransc.ics.repository.InfoType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 import reactor.core.publisher.Flux;
43 import reactor.core.publisher.Mono;
44
45 /**
46  * Callbacks to the EiProducer
47  */
48 @Component
49 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
50 public class A1eCallbacks {
51
52     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53     private static Gson gson = new GsonBuilder().create();
54
55     private final AsyncRestClient restClient;
56     private final InfoJobs eiJobs;
57
58     @Autowired
59     public A1eCallbacks(ApplicationConfig config, InfoJobs eiJobs, SecurityContext securityContext) {
60         AsyncRestClientFactory restClientFactory =
61             new AsyncRestClientFactory(config.getWebClientConfig(), securityContext);
62         this.restClient = restClientFactory.createRestClientUseHttpProxy("");
63         this.eiJobs = eiJobs;
64     }
65
66     public Flux<String> notifyJobStatus(Collection<InfoType> eiTypes, InfoProducers eiProducers) {
67         return Flux.fromIterable(eiTypes) //
68             .flatMap(eiType -> Flux.fromIterable(this.eiJobs.getJobsForType(eiType))) //
69             .filter(eiJob -> !eiJob.getJobStatusUrl().isEmpty()) //
70             .filter(eiJob -> eiProducers.isJobEnabled(eiJob) != eiJob.isLastStatusReportedEnabled())
71             .flatMap(eiJob -> noifyStatusToJobOwner(eiJob, eiProducers));
72     }
73
74     private Mono<String> noifyStatusToJobOwner(InfoJob job, InfoProducers eiProducers) {
75         boolean isJobEnabled = eiProducers.isJobEnabled(job);
76         A1eEiJobStatus status = isJobEnabled ? new A1eEiJobStatus(A1eEiJobStatus.EiJobStatusValues.ENABLED)
77             : new A1eEiJobStatus(A1eEiJobStatus.EiJobStatusValues.DISABLED);
78         String body = gson.toJson(status);
79         return this.restClient.post(job.getJobStatusUrl(), body) //
80             .doOnNext(response -> logger.debug("Consumer notified OK {}", job.getId())) //
81             .doOnNext(response -> job.setLastReportedStatus(isJobEnabled)) //
82             .onErrorResume(throwable -> {
83                 logger.warn("Consumer notify failed {} {}", job.getJobStatusUrl(), throwable.toString());
84                 return Mono.empty();
85             });
86     }
87 }