d6286eed8e0cba6d7de1b4a46756a992a23dccfc
[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.createRestClient("");
52     }
53
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"));
58
59     }
60
61     private Mono<Integer> createTask(Ric ric, Services services) {
62         return Flux.fromIterable(services.getAll()) //
63                 .flatMap(service -> notifyServiceRicSynchronized(ric, service)) //
64                 .collectList() //
65                 .flatMap(okResponses -> Mono.just(Integer.valueOf(okResponses.size()))); //
66     }
67
68     private Mono<String> notifyServiceRicSynchronized(Ric ric, Service service) {
69         if (service.getCallbackUrl().isEmpty()) {
70             return Mono.empty();
71         }
72
73         ServiceCallbackInfo request = new ServiceCallbackInfo(ric.id(), ServiceCallbackInfo.EventType.AVAILABLE);
74         String body = gson.toJson(request);
75
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());
81                     return Mono.empty();
82                 });
83     }
84
85 }