2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 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
35 public class A1ClientFactory {
37 private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
39 private final ApplicationConfig appConfig;
42 public A1ClientFactory(ApplicationConfig appConfig) {
43 this.appConfig = appConfig;
47 * Creates an A1 client with the correct A1 protocol for the provided Ric.
50 * It detects the protocol version by trial and error, since there is no
51 * getVersion method specified in the A1 api yet.
54 * As a side effect it also sets the protocol version in the provided Ric. This
55 * means that after the first successful creation it won't have to try which
56 * protocol to use, but can create the client directly.
58 * @param ric The RIC to get a client for.
59 * @return a client with the correct protocol, or a ServiceException if none of
60 * the protocols are supported by the Ric.
62 public Mono<A1Client> createA1Client(Ric ric) {
63 return getProtocolVersion(ric) //
64 .flatMap(version -> createA1Client(ric, version));
67 private Mono<A1Client> createA1Client(Ric ric, A1ProtocolType version) {
68 if (version == A1ProtocolType.STD_V1) {
69 return Mono.just(createStdA1ClientImpl(ric));
70 } else if (version == A1ProtocolType.OSC_V1) {
71 return Mono.just(createOscA1Client(ric));
72 } else if (version == A1ProtocolType.SDNC_OSC) {
73 return Mono.just(createSdncOscA1Client(ric));
74 } else { // A1ProtocolType.SDNC_ONAP
75 return Mono.just(createSdncOnapA1Client(ric));
79 private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
80 if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
81 return fetchVersion(createSdncOnapA1Client(ric)) //
82 .onErrorResume(notUsed -> fetchVersion(createSdncOscA1Client(ric))) //
83 .onErrorResume(notUsed -> fetchVersion(createOscA1Client(ric))) //
84 .onErrorResume(notUsed -> fetchVersion(createStdA1ClientImpl(ric))) //
85 .doOnNext(ric::setProtocolVersion)
86 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
87 .doOnError(notUsed -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
89 return Mono.just(ric.getProtocolVersion());
93 protected A1Client createOscA1Client(Ric ric) {
94 return new OscA1Client(ric.getConfig());
97 protected A1Client createStdA1ClientImpl(Ric ric) {
98 return new StdA1Client(ric.getConfig());
101 protected A1Client createSdncOscA1Client(Ric ric) {
102 return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
103 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
106 protected A1Client createSdncOnapA1Client(Ric ric) {
107 return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
108 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
111 private Mono<A1ProtocolType> fetchVersion(A1Client a1Client) {
112 return Mono.just(a1Client) //
113 .flatMap(client -> a1Client.getProtocolVersion());