2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
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
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.onap.ccsdk.oran.a1policymanagementservice.clients;
23 import java.lang.invoke.MethodHandles;
24 import java.lang.reflect.Constructor;
26 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
27 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
28 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
29 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
30 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import reactor.core.publisher.Mono;
37 * Factory for A1 clients that supports four different protocol versions of the
40 public class A1ClientFactory {
42 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44 private final ApplicationConfig appConfig;
46 private final AsyncRestClientFactory restClientFactory;
48 public A1ClientFactory(ApplicationConfig appConfig, SecurityContext securityContext) {
49 this.appConfig = appConfig;
50 this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig(), securityContext);
54 * Creates an A1 client with the correct A1 protocol for the provided Ric.
57 * It detects the protocol version by trial and error, since there is no
58 * getVersion method specified in the A1 api yet.
61 * As a side effect it also sets the protocol version in the provided Ric. This
62 * means that after the first successful creation it won't have to try which
63 * protocol to use, but can create the client directly.
65 * @param ric The Near-RT RIC to get a client for.
66 * @return a client with the correct protocol, or a ServiceException if none of
67 * the protocols are supported by the Near-RT RIC.
69 public Mono<A1Client> createA1Client(Ric ric) {
70 return getProtocolVersion(ric) //
71 .flatMap(version -> createA1ClientMono(ric, version));
74 A1Client createClient(Ric ric, A1ProtocolType version) throws ServiceException {
75 if (version == A1ProtocolType.STD_V1_1) {
76 assertNoControllerConfig(ric, version);
77 return new StdA1ClientVersion1(ric.getConfig(), this.restClientFactory);
78 } else if (version == A1ProtocolType.STD_V2_0_0) {
79 assertNoControllerConfig(ric, version);
80 return new StdA1ClientVersion2(ric.getConfig(), this.restClientFactory);
81 } else if (version == A1ProtocolType.OSC_V1) {
82 assertNoControllerConfig(ric, version);
83 return new OscA1Client(ric.getConfig(), this.restClientFactory);
84 } else if (version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1
85 || version == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1
86 || version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
87 return new CcsdkA1AdapterClient(version, ric.getConfig(), getControllerConfig(ric), this.restClientFactory);
88 } else if (version == A1ProtocolType.CUSTOM_PROTOCOL) {
89 return createCustomAdapter(ric);
91 logger.error("Unhandled protocol: {}", version);
92 throw new ServiceException("Unhandled protocol");
96 private ControllerConfig getControllerConfig(Ric ric) throws ServiceException {
97 String controllerName = ric.getConfig().getControllerName();
98 if (controllerName.isEmpty()) {
99 ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
100 throw new ServiceException("No controller configured for Near-RT RIC: " + ric.id());
103 return this.appConfig.getControllerConfig(controllerName);
104 } catch (ServiceException e) {
105 ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
110 private A1Client createCustomAdapter(Ric ric) throws ServiceException {
112 Class<?> clazz = Class.forName(ric.getConfig().getCustomAdapterClass());
113 if (A1Client.class.isAssignableFrom(clazz)) {
114 Constructor<?> constructor = clazz.getConstructor(RicConfig.class, AsyncRestClientFactory.class);
115 return (A1Client) constructor.newInstance(ric.getConfig(), this.restClientFactory);
116 } else if (A1Client.Factory.class.isAssignableFrom(clazz)) {
117 A1Client.Factory factory = (A1Client.Factory) clazz.getDeclaredConstructor().newInstance();
118 return factory.create(ric.getConfig(), this.restClientFactory);
120 throw new ServiceException("The custom class must either implement A1Client.Factory or A1Client");
122 } catch (ClassNotFoundException e) {
123 throw new ServiceException("Could not find class: " + ric.getConfig().getCustomAdapterClass(), e);
124 } catch (Exception e) {
125 throw new ServiceException("Cannot create custom adapter: " + ric.getConfig().getCustomAdapterClass(), e);
129 private void assertNoControllerConfig(Ric ric, A1ProtocolType version) throws ServiceException {
130 if (!ric.getConfig().getControllerName().isEmpty()) {
131 ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
132 throw new ServiceException(
133 "Controller config should be empty, ric: " + ric.id() + " when using protocol version: " + version);
137 private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
139 return Mono.just(createClient(ric, version));
140 } catch (ServiceException e) {
141 return Mono.error(e);
145 private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
146 if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
147 return fetchVersion(ric, A1ProtocolType.STD_V2_0_0) //
148 .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.STD_V1_1)) //
149 .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.OSC_V1)) //
150 .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1)) //
151 .doOnNext(ric::setProtocolVersion)
152 .doOnNext(version -> logger.debug("Established protocol version:{} for Near-RT RIC: {}", version,
154 .doOnError(notUsed -> logger.warn("Could not get protocol version from Near-RT RIC: {}", ric.id()));
156 return Mono.just(ric.getProtocolVersion());
160 private Mono<A1ProtocolType> fetchVersion(Ric ric, A1ProtocolType protocolType) {
161 return createA1ClientMono(ric, protocolType) //
162 .flatMap(A1Client::getProtocolVersion);