2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 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.controllers.r1consumer;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.lang.invoke.MethodHandles;
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.InfoType;
32 import org.oransc.enrichment.repository.InfoTypeSubscriptions;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
39 * Callbacks to the Consumer. Notifies consumer according to the API (which this
42 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
44 public class ConsumerCallbacks implements InfoTypeSubscriptions.Callbacks {
46 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47 private static Gson gson = new GsonBuilder().create();
49 private final AsyncRestClient restClient;
51 public ConsumerCallbacks(@Autowired ApplicationConfig config) {
52 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig());
53 this.restClient = restClientFactory.createRestClientNoHttpProxy("");
57 public void notifyTypeRegistered(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
58 ConsumerTypeRegistrationInfo info = new ConsumerTypeRegistrationInfo(type.getJobDataSchema(),
59 ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.REGISTERED, type.getId());
60 String body = gson.toJson(info);
62 post(subscriptionInfo.getCallbackUrl(), body);
67 public void notifyTypeRemoved(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
68 ConsumerTypeRegistrationInfo info = new ConsumerTypeRegistrationInfo(type.getJobDataSchema(),
69 ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.DEREGISTERED, type.getId());
70 String body = gson.toJson(info);
71 post(subscriptionInfo.getCallbackUrl(), body);
75 private void post(String url, String body) {
76 restClient.post(url, body) //
77 .subscribe(response -> logger.debug("Post OK {}", url), //
78 throwable -> logger.warn("Post failed for consumer callback {} {}", url, body), null);