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 org.oransc.enrichment.clients.AsyncRestClient;
27 import org.oransc.enrichment.clients.AsyncRestClientFactory;
28 import org.oransc.enrichment.configuration.ApplicationConfig;
29 import org.oransc.enrichment.repository.InfoType;
30 import org.oransc.enrichment.repository.InfoTypeSubscriptions;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33 import reactor.core.publisher.Mono;
36 * Callbacks to the Consumer. Notifies consumer according to the API (which this
40 public class ConsumerCallbacks implements InfoTypeSubscriptions.ConsumerCallbackHandler {
42 private static Gson gson = new GsonBuilder().create();
44 private final AsyncRestClient restClient;
46 public static final String API_VERSION = "version_1";
48 public ConsumerCallbacks(@Autowired ApplicationConfig config,
49 @Autowired InfoTypeSubscriptions infoTypeSubscriptions) {
50 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig());
51 this.restClient = restClientFactory.createRestClientNoHttpProxy("");
52 infoTypeSubscriptions.registerCallbackhandler(this, API_VERSION);
56 public Mono<String> notifyTypeRegistered(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
57 String body = body(type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.REGISTERED);
58 return restClient.post(subscriptionInfo.getCallbackUrl(), body);
62 public Mono<String> notifyTypeRemoved(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
63 String body = body(type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.DEREGISTERED);
64 return restClient.post(subscriptionInfo.getCallbackUrl(), body);
67 private String body(InfoType type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues status) {
68 ConsumerTypeRegistrationInfo info =
69 new ConsumerTypeRegistrationInfo(type.getJobDataSchema(), status, type.getId());
70 return gson.toJson(info);