Enrichment Service
[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.configuration.ImmutableWebClientConfig;
30 import org.oransc.enrichment.configuration.WebClientConfig;
31 import org.oransc.enrichment.repository.EiJob;
32 import org.oransc.enrichment.repository.EiProducer;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
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     @Autowired
49     ApplicationConfig applicationConfig;
50
51     public void notifyProducersJobCreated(EiJob eiJob) {
52         for (EiProducer producer : eiJob.type().getProducers()) {
53             notifyProducerJobStarted(producer, eiJob);
54         }
55     }
56
57     public void notifyProducersJobDeleted(EiJob eiJob) {
58         AsyncRestClient restClient = restClient(false);
59         ProducerJobInfo request = new ProducerJobInfo(eiJob.jobData(), eiJob, eiJob.type());
60         String body = gson.toJson(request);
61         for (EiProducer producer : eiJob.type().getProducers()) {
62             restClient.post(producer.jobDeletionCallbackUrl(), body) //
63                 .subscribe(notUsed -> logger.debug("Job subscription started OK {}", producer.id()), //
64                     throwable -> logger.warn("Job subscription failed {}", producer.id(), throwable.toString()), null);
65         }
66     }
67
68     public void notifyProducerJobStarted(EiProducer producer, EiJob eiJob) {
69         AsyncRestClient restClient = restClient(false);
70         ProducerJobInfo request = new ProducerJobInfo(eiJob.jobData(), eiJob, eiJob.type());
71         String body = gson.toJson(request);
72
73         restClient.post(producer.jobCreationCallbackUrl(), body) //
74             .subscribe(notUsed -> logger.debug("Job subscription started OK {}", producer.id()), //
75                 throwable -> logger.warn("Job subscription failed {}", producer.id(), throwable.toString()), null);
76
77     }
78
79     private AsyncRestClient restClient(boolean useTrustValidation) {
80         WebClientConfig config = this.applicationConfig.getWebClientConfig();
81         config = ImmutableWebClientConfig.builder() //
82             .keyStoreType(config.keyStoreType()) //
83             .keyStorePassword(config.keyStorePassword()) //
84             .keyStore(config.keyStore()) //
85             .keyPassword(config.keyPassword()) //
86             .isTrustStoreUsed(useTrustValidation) //
87             .trustStore(config.trustStore()) //
88             .trustStorePassword(config.trustStorePassword()) //
89             .build();
90
91         return new AsyncRestClient("", config);
92     }
93
94 }