cb8f9dd4d67080f8082e9c06de16e0acc5d105c0
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / A1ClientFactory.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.clients;
22
23 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
24 import org.oransc.policyagent.exceptions.ServiceException;
25 import org.oransc.policyagent.repository.Ric;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import reactor.core.publisher.Mono;
29
30 public class A1ClientFactory {
31
32     private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
33
34     public Mono<A1Client> createA1Client(Ric ric) {
35         return getProtocolVersion(ric) //
36             .flatMap(version -> createA1Client(ric, version));
37     }
38
39     private Mono<A1Client> createA1Client(Ric ric, A1ProtocolType version) {
40         if (version == A1ProtocolType.STD_V1) {
41             return Mono.just(createStdA1ClientImpl(ric));
42         } else if (version == A1ProtocolType.OSC_V1) {
43             return Mono.just(new OscA1Client(ric.getConfig()));
44         }
45         return Mono.error(new ServiceException("Not supported protocoltype: " + version));
46     }
47
48     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
49         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
50             return fetchVersion(ric, new OscA1Client(ric.getConfig())) //
51                 .onErrorResume(err -> fetchVersion(ric, createStdA1ClientImpl(ric)))
52                 .doOnNext(version -> ric.setProtocolVersion(version))
53                 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
54                 .doOnError(t -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
55         } else {
56             return Mono.just(ric.getProtocolVersion());
57         }
58     }
59
60     protected A1Client createStdA1ClientImpl(Ric ric) {
61         return new StdA1Client(ric.getConfig());
62     }
63
64     private Mono<A1Client.A1ProtocolType> fetchVersion(Ric ric, A1Client a1Client) {
65         return Mono.just(a1Client) //
66             .flatMap(client -> a1Client.getProtocolVersion());
67     }
68
69 }