04dd08ee4cef98f3c41b274144b9740e98c0503c
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
22
23 import java.lang.reflect.Constructor;
24
25 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
26 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
27 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
28 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
29 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import reactor.core.publisher.Mono;
35
36 /**
37  * Factory for A1 clients that supports four different protocol versions of the
38  * A1 api.
39  */
40 public class A1ClientFactory {
41
42     private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
43
44     private final ApplicationConfig appConfig;
45
46     private final AsyncRestClientFactory restClientFactory;
47
48     @Autowired
49     public A1ClientFactory(ApplicationConfig appConfig, SecurityContext securityContext) {
50         this.appConfig = appConfig;
51         this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig(), securityContext);
52     }
53
54     /**
55      * Creates an A1 client with the correct A1 protocol for the provided Ric.
56      *
57      * <p>
58      * It detects the protocol version by trial and error, since there is no
59      * getVersion method specified in the A1 api yet.
60      *
61      * <p>
62      * As a side effect it also sets the protocol version in the provided Ric. This
63      * means that after the first successful creation it won't have to try which
64      * protocol to use, but can create the client directly.
65      *
66      * @param ric The Near-RT RIC to get a client for.
67      * @return a client with the correct protocol, or a ServiceException if none of
68      *         the protocols are supported by the Near-RT RIC.
69      */
70     public Mono<A1Client> createA1Client(Ric ric) {
71         return getProtocolVersion(ric) //
72                 .flatMap(version -> createA1ClientMono(ric, version));
73     }
74
75     A1Client createClient(Ric ric, A1ProtocolType version) throws ServiceException {
76         if (version == A1ProtocolType.STD_V1_1) {
77             assertNoControllerConfig(ric, version);
78             return new StdA1ClientVersion1(ric.getConfig(), this.restClientFactory);
79         } else if (version == A1ProtocolType.STD_V2_0_0) {
80             assertNoControllerConfig(ric, version);
81             return new StdA1ClientVersion2(ric.getConfig(), this.restClientFactory);
82         } else if (version == A1ProtocolType.OSC_V1) {
83             assertNoControllerConfig(ric, version);
84             return new OscA1Client(ric.getConfig(), this.restClientFactory);
85         } else if (version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1
86                 || version == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1
87                 || version == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
88             return new CcsdkA1AdapterClient(version, ric.getConfig(), getControllerConfig(ric), this.restClientFactory);
89         } else if (version == A1ProtocolType.CUSTOM_PROTOCOL) {
90             return createCustomAdapter(ric);
91         } else {
92             logger.error("Unhandled protocol: {}", version);
93             throw new ServiceException("Unhandled protocol");
94         }
95     }
96
97     private ControllerConfig getControllerConfig(Ric ric) throws ServiceException {
98         String controllerName = ric.getConfig().controllerName();
99         if (controllerName.isEmpty()) {
100             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
101             throw new ServiceException("No controller configured for Near-RT RIC: " + ric.id());
102         }
103         try {
104             return this.appConfig.getControllerConfig(controllerName);
105         } catch (ServiceException e) {
106             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
107             throw e;
108         }
109     }
110
111     private A1Client createCustomAdapter(Ric ric) throws ServiceException {
112         try {
113             Class<?> clazz = Class.forName(ric.getConfig().customAdapterClass());
114             if (A1Client.class.isAssignableFrom(clazz)) {
115                 Constructor<?> constructor = clazz.getConstructor(RicConfig.class, AsyncRestClientFactory.class);
116                 return (A1Client) constructor.newInstance(ric.getConfig(), this.restClientFactory);
117             } else if (A1Client.Factory.class.isAssignableFrom(clazz)) {
118                 A1Client.Factory factory = (A1Client.Factory) clazz.getDeclaredConstructor().newInstance();
119                 return factory.create(ric.getConfig(), this.restClientFactory);
120             } else {
121                 throw new ServiceException("The custom class must either implement A1Client.Factory or A1Client");
122             }
123         } catch (ClassNotFoundException e) {
124             throw new ServiceException("Could not find class: " + ric.getConfig().customAdapterClass(), e);
125         } catch (Exception e) {
126             throw new ServiceException("Cannot create custom adapter: " + ric.getConfig().customAdapterClass(), e);
127         }
128     }
129
130     private void assertNoControllerConfig(Ric ric, A1ProtocolType version) throws ServiceException {
131         if (!ric.getConfig().controllerName().isEmpty()) {
132             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
133             throw new ServiceException(
134                     "Controller config should be empty, ric: " + ric.id() + " when using protocol version: " + version);
135         }
136     }
137
138     private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
139         try {
140             return Mono.just(createClient(ric, version));
141         } catch (ServiceException e) {
142             return Mono.error(e);
143         }
144     }
145
146     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
147         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
148             return fetchVersion(ric, A1ProtocolType.STD_V2_0_0) //
149                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.STD_V1_1)) //
150                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.OSC_V1)) //
151                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1)) //
152                     .doOnNext(ric::setProtocolVersion)
153                     .doOnNext(version -> logger.debug("Established protocol version:{} for Near-RT RIC: {}", version,
154                             ric.id())) //
155                     .doOnError(notUsed -> logger.warn("Could not get protocol version from Near-RT RIC: {}", ric.id())) //
156                     .onErrorResume(
157                             notUsed -> Mono.error(new ServiceException("Protocol negotiation failed for " + ric.id())));
158         } else {
159             return Mono.just(ric.getProtocolVersion());
160         }
161     }
162
163     private Mono<A1ProtocolType> fetchVersion(Ric ric, A1ProtocolType protocolType) {
164         return createA1ClientMono(ric, protocolType) //
165                 .flatMap(A1Client::getProtocolVersion);
166     }
167 }