Add SDNR A1 client in policy-agent
[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         } else if (version == A1ProtocolType.SDNC_OSC) {
45             return Mono.just(createSdncOscA1Client(ric));
46         } else if (version == A1ProtocolType.SDNR_ONAP) {
47             return Mono.just(createSdnrOnapA1Client(ric));
48         }
49         return Mono.error(new ServiceException("Not supported protocoltype: " + version));
50     }
51
52     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
53         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
54             return fetchVersion(ric, createSdnrOnapA1Client(ric)) //
55                 .onErrorResume(err -> fetchVersion(ric, createSdncOscA1Client(ric)))
56                 .onErrorResume(err -> fetchVersion(ric, new OscA1Client(ric.getConfig())))
57                 .onErrorResume(err -> fetchVersion(ric, createStdA1ClientImpl(ric)))
58                 .doOnNext(version -> ric.setProtocolVersion(version))
59                 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
60                 .doOnError(t -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
61         } else {
62             return Mono.just(ric.getProtocolVersion());
63         }
64     }
65
66     protected A1Client createStdA1ClientImpl(Ric ric) {
67         return new StdA1Client(ric.getConfig());
68     }
69
70     protected A1Client createSdncOscA1Client(Ric ric) {
71         return new SdncOscA1Client(ric.getConfig());
72     }
73
74     protected A1Client createSdnrOnapA1Client(Ric ric) {
75         return new SdnrOnapA1Client(ric.getConfig());
76     }
77
78     private Mono<A1Client.A1ProtocolType> fetchVersion(Ric ric, A1Client a1Client) {
79         return Mono.just(a1Client) //
80             .flatMap(client -> a1Client.getProtocolVersion());
81     }
82
83 }