Merge "Updates of the policy agent NBI"
[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) 2019 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>It detects the protocol version by trial and error, since there is no getVersion method specified in the A1
49      * api yet.
50      *
51      * <p>As a side effect it also sets the protocol version in the provided Ric. This means that after the first
52      * successful creation it won't have to try which protocol to use, but can create the client directly.
53      *
54      * @param ric The Ric to get a client for.
55      * @return a client with the correct protocol, or a ServiceException if none of the protocols are supported by the
56      *         Ric.
57      */
58     public Mono<A1Client> createA1Client(Ric ric) {
59         return getProtocolVersion(ric) //
60             .flatMap(version -> createA1Client(ric, version));
61     }
62
63     private Mono<A1Client> createA1Client(Ric ric, A1ProtocolType version) {
64         if (version == A1ProtocolType.STD_V1) {
65             return Mono.just(createStdA1ClientImpl(ric));
66         } else if (version == A1ProtocolType.OSC_V1) {
67             return Mono.just(createOscA1Client(ric));
68         } else if (version == A1ProtocolType.SDNC_OSC) {
69             return Mono.just(createSdncOscA1Client(ric));
70         } else { // A1ProtocolType.SDNR_ONAP
71             return Mono.just(createSdnrOnapA1Client(ric));
72         }
73     }
74
75     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
76         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
77             return fetchVersion(createSdnrOnapA1Client(ric)) //
78                 .onErrorResume(err -> fetchVersion(createSdncOscA1Client(ric))) //
79                 .onErrorResume(err -> fetchVersion(createOscA1Client(ric))) //
80                 .onErrorResume(err -> fetchVersion(createStdA1ClientImpl(ric))) //
81                 .doOnNext(version -> ric.setProtocolVersion(version))
82                 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
83                 .doOnError(t -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
84         } else {
85             return Mono.just(ric.getProtocolVersion());
86         }
87     }
88
89     protected A1Client createOscA1Client(Ric ric) {
90         return new OscA1Client(ric.getConfig());
91     }
92
93     protected A1Client createStdA1ClientImpl(Ric ric) {
94         return new StdA1Client(ric.getConfig());
95     }
96
97     protected A1Client createSdncOscA1Client(Ric ric) {
98         return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
99             appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
100     }
101
102     protected A1Client createSdnrOnapA1Client(Ric ric) {
103         return new SdnrOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
104             appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
105     }
106
107     private Mono<A1ProtocolType> fetchVersion(A1Client a1Client) {
108         return Mono.just(a1Client) //
109             .flatMap(client -> a1Client.getProtocolVersion());
110     }
111 }