66ba60c03e66562ccb9a5817726dd5bd237389f8
[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 org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
24 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
25 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
26 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
27 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import reactor.core.publisher.Mono;
32
33 /**
34  * Factory for A1 clients that supports four different protocol versions of the
35  * A1 api.
36  */
37 public class A1ClientFactory {
38
39     private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
40
41     private final ApplicationConfig appConfig;
42
43     private final AsyncRestClientFactory restClientFactory;
44
45     @Autowired
46     public A1ClientFactory(ApplicationConfig appConfig) {
47         this.appConfig = appConfig;
48         this.restClientFactory = new AsyncRestClientFactory(appConfig.getWebClientConfig());
49     }
50
51     /**
52      * Creates an A1 client with the correct A1 protocol for the provided Ric.
53      *
54      * <p>
55      * It detects the protocol version by trial and error, since there is no
56      * getVersion method specified in the A1 api yet.
57      *
58      * <p>
59      * As a side effect it also sets the protocol version in the provided Ric. This
60      * means that after the first successful creation it won't have to try which
61      * protocol to use, but can create the client directly.
62      *
63      * @param ric The Near-RT RIC to get a client for.
64      * @return a client with the correct protocol, or a ServiceException if none of
65      *         the protocols are supported by the Near-RT RIC.
66      */
67     public Mono<A1Client> createA1Client(Ric ric) {
68         return getProtocolVersion(ric) //
69                 .flatMap(version -> createA1ClientMono(ric, version));
70     }
71
72     A1Client createClient(Ric ric, A1ProtocolType version) throws ServiceException {
73         if (version == A1ProtocolType.STD_V1_1) {
74             assertNoControllerConfig(ric, version);
75             return new StdA1ClientVersion1(ric.getConfig(), this.restClientFactory);
76         } else if (version == A1ProtocolType.STD_V2_0_0) {
77             assertNoControllerConfig(ric, version);
78             return new StdA1ClientVersion2(ric.getConfig(), this.restClientFactory);
79         } else if (version == A1ProtocolType.OSC_V1) {
80             assertNoControllerConfig(ric, version);
81             return new OscA1Client(ric.getConfig(), this.restClientFactory);
82         } else if (version == A1ProtocolType.SDNC_OSC_STD_V1_1 || version == A1ProtocolType.SDNC_OSC_OSC_V1
83                 || version == A1ProtocolType.SDNC_OSC_STD_V2_0_0) {
84             return new SdncOscA1Client(version, ric.getConfig(), getControllerConfig(ric), this.restClientFactory);
85         } else if (version == A1ProtocolType.SDNC_ONAP) {
86             return new SdncOnapA1Client(ric.getConfig(), getControllerConfig(ric), this.restClientFactory);
87         } else {
88             logger.error("Unhandled protocol: {}", version);
89             throw new ServiceException("Unhandled protocol");
90         }
91     }
92
93     private ControllerConfig getControllerConfig(Ric ric) throws ServiceException {
94         String controllerName = ric.getConfig().controllerName();
95         if (controllerName.isEmpty()) {
96             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
97             throw new ServiceException("No controller configured for Near-RT RIC: " + ric.id());
98         }
99         try {
100             return this.appConfig.getControllerConfig(controllerName);
101         } catch (ServiceException e) {
102             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
103             throw e;
104         }
105     }
106
107     private void assertNoControllerConfig(Ric ric, A1ProtocolType version) throws ServiceException {
108         if (!ric.getConfig().controllerName().isEmpty()) {
109             ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
110             throw new ServiceException(
111                     "Controller config should be empty, ric: " + ric.id() + " when using protocol version: " + version);
112         }
113     }
114
115     private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
116         try {
117             return Mono.just(createClient(ric, version));
118         } catch (ServiceException e) {
119             return Mono.error(e);
120         }
121     }
122
123     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
124         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
125             return fetchVersion(ric, A1ProtocolType.STD_V2_0_0) //
126                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.STD_V1_1)) //
127                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.OSC_V1)) //
128                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.SDNC_OSC_STD_V1_1)) //
129                     .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.SDNC_ONAP)) //
130                     .doOnNext(ric::setProtocolVersion)
131                     .doOnNext(version -> logger.debug("Established protocol version:{} for Near-RT RIC: {}", version,
132                             ric.id())) //
133                     .doOnError(notUsed -> logger.warn("Could not get protocol version from Near-RT RIC: {}", ric.id())) //
134                     .onErrorResume(
135                             notUsed -> Mono.error(new ServiceException("Protocol negotiation failed for " + ric.id())));
136         } else {
137             return Mono.just(ric.getProtocolVersion());
138         }
139     }
140
141     private Mono<A1ProtocolType> fetchVersion(Ric ric, A1ProtocolType protocolType) {
142         return createA1ClientMono(ric, protocolType) //
143                 .flatMap(A1Client::getProtocolVersion);
144     }
145 }