b5c10bf637026ac003b3484fb9c61d1f6e197396
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
6  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
7  * ======================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21
22 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
23
24 import java.lang.invoke.MethodHandles;
25 import java.lang.reflect.Constructor;
26
27 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
28 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
29 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
30 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
31 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import reactor.core.publisher.Mono;
37
38 /**
39  * Factory for A1 clients that supports four different protocol versions of the
40  * A1 api.
41  */
42 public class A1ClientFactory {
43
44     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45
46     private final ApplicationConfig appConfig;
47
48     private final AsyncRestClientFactory restClientFactory;
49
50     public A1ClientFactory(ApplicationConfig appConfig, SecurityContext securityContext) {
51         this.appConfig = appConfig;
52         this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig(), securityContext);
53     }
54
55     /**
56      * Creates an A1 client with the correct A1 protocol for the provided Ric.
57      *
58      * <p>
59      * It detects the protocol version by trial and error, since there is no
60      * getVersion method specified in the A1 api yet.
61      *
62      * <p>
63      * As a side effect it also sets the protocol version in the provided Ric. This
64      * means that after the first successful creation it won't have to try which
65      * protocol to use, but can create the client directly.
66      *
67      * @param ric The Near-RT RIC to get a client for.
68      * @return a client with the correct protocol, or a ServiceException if none of
69      *         the protocols are supported by the Near-RT RIC.
70      */
71     public Mono<A1Client> createA1Client(Ric ric) {
72         return getProtocolVersion(ric) //
73                 .flatMap(version -> createA1ClientMono(ric, version));
74     }
75
76     A1Client createClient(Ric ric, A1ProtocolType version) throws ServiceException {
77         if (version == A1ProtocolType.STD_V1_1) {
78             assertNoControllerConfig(ric, version);
79             return new StdA1ClientVersion1(ric.getConfig(), this.restClientFactory);
80         } else if (version == A1ProtocolType.STD_V2_0_0) {
81             assertNoControllerConfig(ric, version);
82             return new StdA1ClientVersion2(ric.getConfig(), this.restClientFactory);
83         } else if (version == A1ProtocolType.OSC_V1) {
84             assertNoControllerConfig(ric, version);
85             return new OscA1Client(ric.getConfig(), this.restClientFactory);
86         } else if (version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1
87                 || version == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1
88                 || version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
89             return new CcsdkA1AdapterClient(version, ric.getConfig(), this.restClientFactory);
90         } else if (version == A1ProtocolType.CUSTOM_PROTOCOL) {
91             return createCustomAdapter(ric);
92         } else {
93             logger.error("Unhandled protocol: {}", version);
94             throw new ServiceException("Unhandled protocol");
95         }
96     }
97
98     private ControllerConfig getControllerConfig(Ric ric) throws ServiceException {
99         ControllerConfig controllerConfig = ric.getConfig().getControllerConfig();
100         if (controllerConfig == null) {
101             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
102             throw new ServiceException("No controller configured for Near-RT RIC: " + ric.id());
103         }
104         return controllerConfig;
105     }
106
107     private A1Client createCustomAdapter(Ric ric) throws ServiceException {
108         try {
109             if (ric.getConfig().getCustomAdapterClass() != null && !ric.getConfig().getCustomAdapterClass().isEmpty()) {
110                 Class<?> clazz = Class.forName(ric.getConfig().getCustomAdapterClass());
111                 if (A1Client.class.isAssignableFrom(clazz)) {
112                     Constructor<?> constructor = clazz.getConstructor(RicConfig.class, AsyncRestClientFactory.class);
113                     logger.debug("A1Client (" + clazz.getTypeName() + ") being created for ric: {}",
114                             ric.getConfig().getRicId());
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                     logger.debug("A1Client (" + clazz.getTypeName() + ") factory creating client for ric: {}",
119                             ric.getConfig().getRicId());
120                     return factory.create(ric.getConfig(), this.restClientFactory);
121                 } else {
122                     throw new ServiceException("The custom class must either implement A1Client.Factory or A1Client");
123                 }
124             } else {
125                 throw new ServiceException("Custom adapter class is required to use A1ProtocolType.CUSTOM_PROTOCOL");
126             }
127         } catch (ClassNotFoundException e) {
128             throw new ServiceException("Could not find class: " + ric.getConfig().getCustomAdapterClass(), e);
129         } catch (NoSuchMethodException e) {
130             throw new ServiceException("Could not find the required constructor in class "
131                                                + ric.getConfig().getCustomAdapterClass(), e);
132         } catch (ServiceException e) {
133             throw e;
134         } catch (Exception e) {
135             throw new ServiceException("Cannot create custom adapter: " + ric.getConfig().getCustomAdapterClass(), e);
136         }
137     }
138
139     private void assertNoControllerConfig(Ric ric, A1ProtocolType version) throws ServiceException {
140         if (ric.getConfig().getControllerConfig() != null) {
141             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
142             throw new ServiceException(
143                     "Controller config should be empty, ric: " + ric.id() + " when using protocol version: " + version);
144         }
145     }
146
147     private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
148         try {
149             return Mono.just(createClient(ric, version));
150         } catch (ServiceException e) {
151             return Mono.error(e);
152         }
153     }
154
155     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
156         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
157             return fetchVersion(ric, A1ProtocolType.STD_V2_0_0) //
158                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.STD_V1_1)) //
159                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.OSC_V1)) //
160                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1)) //
161                     .doOnNext(ric::setProtocolVersion)
162                     .doOnNext(version -> logger.debug("Established protocol version:{} for Near-RT RIC: {}", version,
163                             ric.id())) //
164                     .doOnError(notUsed -> logger.warn("Could not get protocol version from Near-RT RIC: {}", ric.id()));
165         } else {
166             return Mono.just(ric.getProtocolVersion());
167         }
168     }
169
170     private Mono<A1ProtocolType> fetchVersion(Ric ric, A1ProtocolType protocolType) {
171         return createA1ClientMono(ric, protocolType) //
172                 .flatMap(A1Client::getProtocolVersion);
173     }
174 }