2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.enrichment.clients;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.lang.invoke.MethodHandles;
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;
38 * Callbacks to the EiProducer
40 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
41 public class ProducerCallbacks {
43 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44 private static Gson gson = new GsonBuilder() //
49 ApplicationConfig applicationConfig;
51 public void notifyProducersJobCreated(EiJob eiJob) {
52 for (EiProducer producer : eiJob.type().getProducers()) {
53 notifyProducerJobStarted(producer, eiJob);
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);
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);
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);
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()) //
91 return new AsyncRestClient("", config);