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.ics.controllers.r1consumer;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import org.oransc.ics.clients.AsyncRestClient;
27 import org.oransc.ics.clients.AsyncRestClientFactory;
28 import org.oransc.ics.clients.SecurityContext;
29 import org.oransc.ics.configuration.ApplicationConfig;
30 import org.oransc.ics.repository.InfoType;
31 import org.oransc.ics.repository.InfoTypeSubscriptions;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34 import reactor.core.publisher.Mono;
37 * Callbacks to the Consumer. Notifies consumer according to the API (which this
41 public class ConsumerCallbacks implements InfoTypeSubscriptions.ConsumerCallbackHandler {
43 private static Gson gson = new GsonBuilder().create();
45 private final AsyncRestClient restClient;
47 public static final String API_VERSION = "version_1";
50 public ConsumerCallbacks(ApplicationConfig config, InfoTypeSubscriptions infoTypeSubscriptions,
51 SecurityContext securityContext) {
52 AsyncRestClientFactory restClientFactory =
53 new AsyncRestClientFactory(config.getWebClientConfig(), securityContext);
54 this.restClient = restClientFactory.createRestClientNoHttpProxy("");
55 infoTypeSubscriptions.registerCallbackhandler(this, API_VERSION);
59 public Mono<String> notifyTypeRegistered(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
60 String body = body(type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.REGISTERED);
61 return restClient.post(subscriptionInfo.getCallbackUrl(), body);
65 public Mono<String> notifyTypeRemoved(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
66 String body = body(type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.DEREGISTERED);
67 return restClient.post(subscriptionInfo.getCallbackUrl(), body);
70 private String body(InfoType type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues status) {
71 ConsumerTypeRegistrationInfo info =
72 new ConsumerTypeRegistrationInfo(type.getJobDataSchema(), status, type.getId());
73 return gson.toJson(info);