d9069303992f5895360a48e0c2b080efecae3940
[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.repository.Ric;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import reactor.core.publisher.Mono;
30
31 /**
32  * Factory for A1 clients that supports four different protocol versions of the A1 api.
33  */
34 public class A1ClientFactory {
35
36     private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
37
38     private final ApplicationConfig appConfig;
39
40     @Autowired
41     public A1ClientFactory(ApplicationConfig appConfig) {
42         this.appConfig = appConfig;
43     }
44
45     /**
46      * Creates an A1 client with the correct A1 protocol for the provided Ric.
47      *
48      * <p>
49      * It detects the protocol version by trial and error, since there is no getVersion method specified in the A1
50      * api yet.
51      *
52      * <p>
53      * As a side effect it also sets the protocol version in the provided Ric. This means that after the first
54      * successful creation it won't have to try which protocol to use, but can create the client directly.
55      *
56      * @param ric The Ric to get a client for.
57      * @return a client with the correct protocol, or a ServiceException if none of the protocols are supported by the
58      *         Ric.
59      */
60     public Mono<A1Client> createA1Client(Ric ric) {
61         return getProtocolVersion(ric) //
62             .flatMap(version -> createA1Client(ric, version));
63     }
64
65     private Mono<A1Client> createA1Client(Ric ric, A1ProtocolType version) {
66         if (version == A1ProtocolType.STD_V1) {
67             return Mono.just(createStdA1ClientImpl(ric));
68         } else if (version == A1ProtocolType.OSC_V1) {
69             return Mono.just(createOscA1Client(ric));
70         } else if (version == A1ProtocolType.SDNC_OSC) {
71             return Mono.just(createSdncOscA1Client(ric));
72         } else { // A1ProtocolType.SDNR_ONAP
73             return Mono.just(createSdnrOnapA1Client(ric));
74         }
75     }
76
77     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
78         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
79             return fetchVersion(createSdnrOnapA1Client(ric)) //
80                 .onErrorResume(notUsed -> fetchVersion(createSdncOscA1Client(ric))) //
81                 .onErrorResume(notUsed -> fetchVersion(createOscA1Client(ric))) //
82                 .onErrorResume(notUsed -> fetchVersion(createStdA1ClientImpl(ric))) //
83                 .doOnNext(ric::setProtocolVersion)
84                 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
85                 .doOnError(notUsed -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
86         } else {
87             return Mono.just(ric.getProtocolVersion());
88         }
89     }
90
91     protected A1Client createOscA1Client(Ric ric) {
92         return new OscA1Client(ric.getConfig());
93     }
94
95     protected A1Client createStdA1ClientImpl(Ric ric) {
96         return new StdA1Client(ric.getConfig());
97     }
98
99     protected A1Client createSdncOscA1Client(Ric ric) {
100         return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
101             appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
102     }
103
104     protected A1Client createSdnrOnapA1Client(Ric ric) {
105         return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
106             appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
107     }
108
109     private Mono<A1ProtocolType> fetchVersion(A1Client a1Client) {
110         return Mono.just(a1Client) //
111             .flatMap(client -> a1Client.getProtocolVersion());
112     }
113 }