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.configuration.ApplicationConfig;
25 import org.oransc.policyagent.repository.Ric;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import reactor.core.publisher.Mono;
32 * Factory for A1 clients that supports four different protocol versions of the A1 api.
34 public class A1ClientFactory {
36 private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
38 private final ApplicationConfig appConfig;
41 public A1ClientFactory(ApplicationConfig appConfig) {
42 this.appConfig = appConfig;
46 * Creates an A1 client with the correct A1 protocol for the provided Ric.
49 * It detects the protocol version by trial and error, since there is no getVersion method specified in the A1
53 * As a side effect it also sets the protocol version in the provided Ric. This means that after the first
54 * successful creation it won't have to try which protocol to use, but can create the client directly.
56 * @param ric The Ric to get a client for.
57 * @return a client with the correct protocol, or a ServiceException if none of the protocols are supported by the
60 public Mono<A1Client> createA1Client(Ric ric) {
61 return getProtocolVersion(ric) //
62 .flatMap(version -> createA1Client(ric, version));
65 private Mono<A1Client> createA1Client(Ric ric, A1ProtocolType version) {
66 if (version == A1ProtocolType.STD_V1) {
67 return Mono.just(createStdA1ClientImpl(ric));
68 } else if (version == A1ProtocolType.OSC_V1) {
69 return Mono.just(createOscA1Client(ric));
70 } else if (version == A1ProtocolType.SDNC_OSC) {
71 return Mono.just(createSdncOscA1Client(ric));
72 } else { // A1ProtocolType.SDNR_ONAP
73 return Mono.just(createSdnrOnapA1Client(ric));
77 private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
78 if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
79 return fetchVersion(createSdnrOnapA1Client(ric)) //
80 .onErrorResume(err -> fetchVersion(createSdncOscA1Client(ric))) //
81 .onErrorResume(err -> fetchVersion(createOscA1Client(ric))) //
82 .onErrorResume(err -> fetchVersion(createStdA1ClientImpl(ric))) //
83 .doOnNext(version -> ric.setProtocolVersion(version))
84 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
85 .doOnError(t -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
87 return Mono.just(ric.getProtocolVersion());
91 protected A1Client createOscA1Client(Ric ric) {
92 return new OscA1Client(ric.getConfig());
95 protected A1Client createStdA1ClientImpl(Ric ric) {
96 return new StdA1Client(ric.getConfig());
99 protected A1Client createSdncOscA1Client(Ric ric) {
100 return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
101 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
104 protected A1Client createSdnrOnapA1Client(Ric ric) {
105 return new SdnrOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
106 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
109 private Mono<A1ProtocolType> fetchVersion(A1Client a1Client) {
110 return Mono.just(a1Client) //
111 .flatMap(client -> a1Client.getProtocolVersion());