Some changes in status notifications
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / consumer / ConsumerCallbacks.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.consumer;
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.clients.AsyncRestClient;
29 import org.oransc.enrichment.clients.AsyncRestClientFactory;
30 import org.oransc.enrichment.configuration.ApplicationConfig;
31 import org.oransc.enrichment.repository.EiJob;
32 import org.oransc.enrichment.repository.EiJobs;
33 import org.oransc.enrichment.repository.EiProducer;
34 import org.oransc.enrichment.repository.EiType;
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 /**
42  * Callbacks to the EiProducer
43  */
44 @Component
45 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
46 public class ConsumerCallbacks {
47
48     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49     private static Gson gson = new GsonBuilder().create();
50
51     private final AsyncRestClient restClient;
52     private final EiTypes eiTypes;
53     private final EiJobs eiJobs;
54
55     @Autowired
56     public ConsumerCallbacks(ApplicationConfig config, EiTypes eiTypes, EiJobs eiJobs) {
57         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig());
58         this.restClient = restClientFactory.createRestClient("");
59         this.eiTypes = eiTypes;
60         this.eiJobs = eiJobs;
61     }
62
63     public void notifyConsumersProducerDeleted(EiProducer eiProducer) {
64         for (EiType type : eiProducer.getEiTypes()) {
65             if (this.eiTypes.get(type.getId()) == null) {
66                 // The type is removed
67                 for (EiJob job : this.eiJobs.getJobsForType(type)) {
68                     if (job.isLastStatusReportedEnabled()) {
69                         noifyJobOwner(job, new ConsumerEiJobStatus(ConsumerEiJobStatus.EiJobStatusValues.DISABLED));
70                         job.setLastReportedStatus(false);
71                     }
72                 }
73             }
74         }
75     }
76
77     public void notifyConsumersProducerAdded(EiProducer eiProducer) {
78         for (EiType type : eiProducer.getEiTypes()) {
79             notifyConsumersTypeAdded(type);
80         }
81     }
82
83     public void notifyConsumersTypeAdded(EiType eiType) {
84         for (EiJob job : this.eiJobs.getJobsForType(eiType)) {
85             if (!job.isLastStatusReportedEnabled()) {
86                 noifyJobOwner(job, new ConsumerEiJobStatus(ConsumerEiJobStatus.EiJobStatusValues.ENABLED));
87                 job.setLastReportedStatus(true);
88             }
89         }
90     }
91
92     private void noifyJobOwner(EiJob job, ConsumerEiJobStatus status) {
93         if (!job.getJobStatusUrl().isEmpty()) {
94             String body = gson.toJson(status);
95             this.restClient.post(job.getJobStatusUrl(), body) //
96                 .subscribe(notUsed -> logger.debug("Consumer notified OK {}", job.getId()), //
97                     throwable -> logger.warn("Consumer notify failed {} {}", job.getJobStatusUrl(),
98                         throwable.toString()), //
99                     null);
100         }
101     }
102
103 }