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.createRestClient("");
54 public void notifyServicesRicSynchronized(Ric ric, Services services) {
55 createTask(ric, services).subscribe(numberOfServices -> logger.debug("Services {} notified", numberOfServices),
56 throwable -> logger.error("Service notification failed, cause: {}", throwable.getMessage()),
57 () -> logger.debug("All services notified"));
61 private Mono<Integer> createTask(Ric ric, Services services) {
62 return Flux.fromIterable(services.getAll()) //
63 .flatMap(service -> notifyServiceRicSynchronized(ric, service)) //
65 .flatMap(okResponses -> Mono.just(Integer.valueOf(okResponses.size()))); //
68 private Mono<String> notifyServiceRicSynchronized(Ric ric, Service service) {
69 if (service.getCallbackUrl().isEmpty()) {
73 ServiceCallbackInfo request = new ServiceCallbackInfo(ric.id(), ServiceCallbackInfo.EventType.AVAILABLE);
74 String body = gson.toJson(request);
76 return restClient.post(service.getCallbackUrl(), body)
77 .doOnNext(resp -> logger.debug("Invoking service {} callback, ric: {}", service.getName(), ric.id()))
78 .onErrorResume(throwable -> {
79 logger.error("Service: {}, callback: {} failed: {}", service.getName(), service.getCallbackUrl(),
80 throwable.toString());