01891e9942f754fa39b418b89dec4095ec32cab3
[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 AsyncRestClientFactory restClientFactory;
47
48     public A1ClientFactory(ApplicationConfig appConfig, SecurityContext securityContext) {
49         this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig(), securityContext);
50     }
51
52     /**
53      * Creates an A1 client with the correct A1 protocol for the provided Ric.
54      *
55      * <p>
56      * It detects the protocol version by trial and error, since there is no
57      * getVersion method specified in the A1 api yet.
58      *
59      * <p>
60      * As a side effect it also sets the protocol version in the provided Ric. This
61      * means that after the first successful creation it won't have to try which
62      * protocol to use, but can create the client directly.
63      *
64      * @param ric The Near-RT RIC to get a client for.
65      * @return a client with the correct protocol, or a ServiceException if none of
66      *         the protocols are supported by the Near-RT RIC.
67      */
68     public Mono<A1Client> createA1Client(Ric ric) {
69         return getProtocolVersion(ric) //
70                 .flatMap(version -> createA1ClientMono(ric, version));
71     }
72
73     A1Client createClient(Ric ric, A1ProtocolType version) throws ServiceException {
74         if (version == A1ProtocolType.STD_V1_1) {
75             assertNoControllerConfig(ric, version);
76             return new StdA1ClientVersion1(ric.getConfig(), this.restClientFactory);
77         } else if (version == A1ProtocolType.STD_V2_0_0) {
78             assertNoControllerConfig(ric, version);
79             return new StdA1ClientVersion2(ric.getConfig(), this.restClientFactory);
80         } else if (version == A1ProtocolType.OSC_V1) {
81             assertNoControllerConfig(ric, version);
82             return new OscA1Client(ric.getConfig(), this.restClientFactory);
83         } else if (version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1
84                 || version == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1
85                 || version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
86             return new CcsdkA1AdapterClient(version, ric.getConfig(), this.restClientFactory);
87         } else if (version == A1ProtocolType.CUSTOM_PROTOCOL) {
88             return createCustomAdapter(ric);
89         } else {
90             logger.error("Unhandled protocol: {}", version);
91             throw new ServiceException("Unhandled protocol");
92         }
93     }
94
95     private ControllerConfig getControllerConfig(Ric ric) throws ServiceException {
96         ControllerConfig controllerConfig = ric.getConfig().getControllerConfig();
97         if (controllerConfig == null) {
98             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
99             throw new ServiceException("No controller configured for Near-RT RIC: " + ric.id());
100         }
101         return controllerConfig;
102     }
103
104     private A1Client createCustomAdapter(Ric ric) throws ServiceException {
105         try {
106             if (ric.getConfig().getCustomAdapterClass() != null && !ric.getConfig().getCustomAdapterClass().isEmpty()) {
107                 Class<?> clazz = Class.forName(ric.getConfig().getCustomAdapterClass());
108                 if (A1Client.class.isAssignableFrom(clazz)) {
109                     Constructor<?> constructor = clazz.getConstructor(RicConfig.class, AsyncRestClientFactory.class);
110                     logger.debug("A1Client (" + clazz.getTypeName() + ") being created for ric: {}",
111                             ric.getConfig().getRicId());
112                     return (A1Client) constructor.newInstance(ric.getConfig(), this.restClientFactory);
113                 } else if (A1Client.Factory.class.isAssignableFrom(clazz)) {
114                     A1Client.Factory factory = (A1Client.Factory) clazz.getDeclaredConstructor().newInstance();
115                     logger.debug("A1Client (" + clazz.getTypeName() + ") factory creating client for ric: {}",
116                             ric.getConfig().getRicId());
117                     return factory.create(ric.getConfig(), this.restClientFactory);
118                 } else {
119                     throw new ServiceException("The custom class must either implement A1Client.Factory or A1Client");
120                 }
121             } else {
122                 throw new ServiceException("Custom adapter class is required to use A1ProtocolType.CUSTOM_PROTOCOL");
123             }
124         } catch (ClassNotFoundException e) {
125             throw new ServiceException("Could not find class: " + ric.getConfig().getCustomAdapterClass(), e);
126         } catch (NoSuchMethodException e) {
127             throw new ServiceException("Could not find the required constructor in class "
128                                                + ric.getConfig().getCustomAdapterClass(), e);
129         } catch (ServiceException e) {
130             throw e;
131         } catch (Exception e) {
132             throw new ServiceException("Cannot create custom adapter: " + ric.getConfig().getCustomAdapterClass(), e);
133         }
134     }
135
136     private void assertNoControllerConfig(Ric ric, A1ProtocolType version) throws ServiceException {
137         if (ric.getConfig().getControllerConfig() != null) {
138             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
139             throw new ServiceException(
140                     "Controller config should be empty, ric: " + ric.id() + " when using protocol version: " + version);
141         }
142     }
143
144     private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
145         try {
146             return Mono.just(createClient(ric, version));
147         } catch (ServiceException e) {
148             return Mono.error(e);
149         }
150     }
151
152     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
153         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
154             return fetchVersion(ric, A1ProtocolType.STD_V2_0_0) //
155                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.STD_V1_1)) //
156                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.OSC_V1)) //
157                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1)) //
158                     .doOnNext(ric::setProtocolVersion)
159                     .doOnNext(version -> logger.debug("Established protocol version:{} for Near-RT RIC: {}", version,
160                             ric.id())) //
161                     .doOnError(notUsed -> logger.warn("Could not get protocol version from Near-RT RIC: {}", ric.id()));
162         } else {
163             return Mono.just(ric.getProtocolVersion());
164         }
165     }
166
167     private Mono<A1ProtocolType> fetchVersion(Ric ric, A1ProtocolType protocolType) {
168         return createA1ClientMono(ric, protocolType) //
169                 .flatMap(A1Client::getProtocolVersion);
170     }
171 }