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.exceptions.ServiceException;
26 import org.oransc.policyagent.repository.Ric;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import reactor.core.publisher.Mono;
33 * Factory for A1 clients that supports four different protocol versions of the
36 public class A1ClientFactory {
38 private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
40 private final ApplicationConfig appConfig;
43 public A1ClientFactory(ApplicationConfig appConfig) {
44 this.appConfig = appConfig;
48 * Creates an A1 client with the correct A1 protocol for the provided Ric.
51 * It detects the protocol version by trial and error, since there is no
52 * getVersion method specified in the A1 api yet.
55 * As a side effect it also sets the protocol version in the provided Ric. This
56 * means that after the first successful creation it won't have to try which
57 * protocol to use, but can create the client directly.
59 * @param ric The RIC to get a client for.
60 * @return a client with the correct protocol, or a ServiceException if none of
61 * the protocols are supported by the Ric.
63 public Mono<A1Client> createA1Client(Ric ric) {
64 return getProtocolVersion(ric) //
65 .flatMap(version -> createA1ClientMono(ric, version));
68 A1Client createClient(Ric ric, A1ProtocolType version) {
69 if (version == A1ProtocolType.STD_V1_1) {
70 return new StdA1ClientVersion1(ric.getConfig());
71 } else if (version == A1ProtocolType.OSC_V1) {
72 return new OscA1Client(ric.getConfig());
73 } else if (version == A1ProtocolType.SDNC_OSC_STD_V1_1 || version == A1ProtocolType.SDNC_OSC_OSC_V1) {
74 return new SdncOscA1Client(version, ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
75 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
76 } else if (version == A1ProtocolType.SDNC_ONAP) {
77 return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
78 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
80 logger.error("Unhandled protocol: {}", version);
85 private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
86 A1Client c = createClient(ric, version);
87 return c != null ? Mono.just(c) : Mono.error(new ServiceException("Unhandled protocol: " + version));
90 private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
91 if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
92 return fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1)) //
93 .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.OSC_V1))) //
94 .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_OSC_STD_V1_1))) //
95 .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_ONAP))) //
96 .doOnNext(ric::setProtocolVersion)
97 .doOnNext(version -> logger.debug("Established protocol version:{} for Ric: {}", version, ric.name())) //
98 .doOnError(notUsed -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
100 return Mono.just(ric.getProtocolVersion());
104 private Mono<A1ProtocolType> fetchVersion(A1Client a1Client) {
105 return a1Client.getProtocolVersion();