2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.clients;
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;
30 public class A1ClientFactory {
32 private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
34 public Mono<A1Client> createA1Client(Ric ric) {
35 return getProtocolVersion(ric) //
36 .flatMap(version -> createA1Client(ric, version));
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.CONTROLLER) {
45 return Mono.just(createControllerA1Client(ric));
47 return Mono.error(new ServiceException("Not supported protocoltype: " + version));
50 private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
51 if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
52 return fetchVersion(ric, createControllerA1Client(ric)) //
53 .onErrorResume(err -> fetchVersion(ric, new OscA1Client(ric.getConfig())))
54 .onErrorResume(err -> fetchVersion(ric, createStdA1ClientImpl(ric)))
55 .doOnNext(version -> ric.setProtocolVersion(version))
56 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
57 .doOnError(t -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
59 return Mono.just(ric.getProtocolVersion());
63 protected A1Client createStdA1ClientImpl(Ric ric) {
64 return new StdA1Client(ric.getConfig());
67 protected A1Client createControllerA1Client(Ric ric) {
68 return new ControllerA1Client(ric.getConfig());
71 private Mono<A1Client.A1ProtocolType> fetchVersion(Ric ric, A1Client a1Client) {
72 return Mono.just(a1Client) //
73 .flatMap(client -> a1Client.getProtocolVersion());