b028cd633ac73a38ad7974651772349a8042e34e
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.lang.invoke.MethodHandles;
27
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;
35
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38
39 /**
40  * Callbacks to the Service
41  */
42 @SuppressWarnings("java:S3457") // No need to call "toString()" method as formatting and string ..
43 public class ServiceCallbacks {
44
45     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46     private static Gson gson = new GsonBuilder().create();
47
48     private final AsyncRestClient restClient;
49
50     public ServiceCallbacks(AsyncRestClientFactory restClientFactory) {
51         this.restClient = restClientFactory.createRestClientNoHttpProxy("");
52     }
53
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); //
58     }
59
60     private Mono<Service> notifyService(Ric ric, Service service, ServiceCallbackInfo.EventType eventType) {
61         if (service.getCallbackUrl().isEmpty()) {
62             return Mono.empty();
63         }
64
65         ServiceCallbackInfo request = new ServiceCallbackInfo(ric.id(), eventType);
66         String body = gson.toJson(request);
67
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());
73                     return Mono.empty();
74                 }) //
75                 .flatMap(resp -> Mono.just(service));
76     }
77
78 }