Merge "Renamed things to fit with namechange of RicSynchronizationTask"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / A1ClientFactory.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.clients;
22
23 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
24 import org.oransc.policyagent.configuration.ApplicationConfig;
25 import org.oransc.policyagent.exceptions.ServiceException;
26 import org.oransc.policyagent.repository.Ric;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import reactor.core.publisher.Mono;
31
32 /**
33  * Factory for A1 clients that supports four different protocol versions of the
34  * A1 api.
35  */
36 public class A1ClientFactory {
37
38     private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
39
40     private final ApplicationConfig appConfig;
41
42     @Autowired
43     public A1ClientFactory(ApplicationConfig appConfig) {
44         this.appConfig = appConfig;
45     }
46
47     /**
48      * Creates an A1 client with the correct A1 protocol for the provided Ric.
49      *
50      * <p>
51      * It detects the protocol version by trial and error, since there is no
52      * getVersion method specified in the A1 api yet.
53      *
54      * <p>
55      * As a side effect it also sets the protocol version in the provided Ric. This
56      * means that after the first successful creation it won't have to try which
57      * protocol to use, but can create the client directly.
58      *
59      * @param ric The RIC to get a client for.
60      * @return a client with the correct protocol, or a ServiceException if none of
61      *         the protocols are supported by the Ric.
62      */
63     public Mono<A1Client> createA1Client(Ric ric) {
64         return getProtocolVersion(ric) //
65             .flatMap(version -> createA1ClientMono(ric, version));
66     }
67
68     A1Client createClient(Ric ric, A1ProtocolType version) {
69         if (version == A1ProtocolType.STD_V1_1) {
70             return new StdA1ClientVersion1(ric.getConfig());
71         } else if (version == A1ProtocolType.OSC_V1) {
72             return new OscA1Client(ric.getConfig());
73         } else if (version == A1ProtocolType.SDNC_OSC) {
74             return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
75                 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
76         } else if (version == A1ProtocolType.SDNC_ONAP) {
77             return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
78                 appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
79         } else {
80             logger.error("Unhandled protocol: {}", version);
81             return null;
82         }
83     }
84
85     private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
86         A1Client c = createClient(ric, version);
87         return c != null ? Mono.just(c) : Mono.error(new ServiceException("Unhandled protocol: " + version));
88     }
89
90     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
91         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
92             return fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1)) //
93                 .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.OSC_V1))) //
94                 .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_OSC))) //
95                 .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_ONAP))) //
96                 .doOnNext(ric::setProtocolVersion)
97                 .doOnNext(version -> logger.debug("Established protocol version:{} for Ric: {}", version, ric.name())) //
98                 .doOnError(notUsed -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
99         } else {
100             return Mono.just(ric.getProtocolVersion());
101         }
102     }
103
104     private Mono<A1ProtocolType> fetchVersion(A1Client a1Client) {
105         return Mono.just(a1Client) //
106             .flatMap(client -> a1Client.getProtocolVersion());
107     }
108 }