2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
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
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.onap.ccsdk.oran.a1policymanagementservice.controllers;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.lang.invoke.MethodHandles;
28 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
29 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
40 * Callbacks to the Service
42 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
43 public class ServiceCallbacks {
45 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46 private static Gson gson = new GsonBuilder().create();
48 private final AsyncRestClient restClient;
50 public ServiceCallbacks(AsyncRestClientFactory restClientFactory) {
51 this.restClient = restClientFactory.createRestClientNoHttpProxy("");
54 public Flux<Service> notifyServicesRicAvailable(Ric ric, Services services) {
55 final int CONCURRENCY = 10;
56 return Flux.fromIterable(services.getAll()) //
57 .flatMap(service -> notifyService(ric, service, ServiceCallbackInfo.EventType.AVAILABLE), CONCURRENCY); //
60 private Mono<Service> notifyService(Ric ric, Service service, ServiceCallbackInfo.EventType eventType) {
61 if (service.getCallbackUrl().isEmpty()) {
65 ServiceCallbackInfo request = new ServiceCallbackInfo(ric.id(), eventType);
66 String body = gson.toJson(request);
68 return restClient.post(service.getCallbackUrl(), body)
69 .doOnNext(resp -> logger.debug("Invoking service {} callback, ric: {}", service.getName(), ric.id()))
70 .onErrorResume(throwable -> {
71 logger.warn("Service: {}, callback: {} failed: {}", service.getName(), service.getCallbackUrl(),
72 throwable.toString());
75 .flatMap(resp -> Mono.just(service));