Fix bug in SDNC, read controller info from yaml
[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.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 public class A1ClientFactory {
33
34     private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
35
36     private final ApplicationConfig appConfig;
37
38     @Autowired
39     public A1ClientFactory(ApplicationConfig appConfig) {
40         this.appConfig = appConfig;
41     }
42
43     public Mono<A1Client> createA1Client(Ric ric) {
44         return getProtocolVersion(ric) //
45             .flatMap(version -> createA1Client(ric, version));
46     }
47
48     private Mono<A1Client> createA1Client(Ric ric, A1ProtocolType version) {
49         if (version == A1ProtocolType.STD_V1) {
50             return Mono.just(createStdA1ClientImpl(ric));
51         } else if (version == A1ProtocolType.OSC_V1) {
52             return Mono.just(new OscA1Client(ric.getConfig()));
53         } else if (version == A1ProtocolType.SDNC_OSC) {
54             return Mono.just(createSdncOscA1Client(ric));
55         } else if (version == A1ProtocolType.SDNR_ONAP) {
56             return Mono.just(createSdnrOnapA1Client(ric));
57         }
58         return Mono.error(new ServiceException("Not supported protocoltype: " + version));
59     }
60
61     private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
62         if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
63             return fetchVersion(ric, createSdnrOnapA1Client(ric)) //
64                 .onErrorResume(err -> fetchVersion(ric, createSdncOscA1Client(ric)))
65                 .onErrorResume(err -> fetchVersion(ric, new OscA1Client(ric.getConfig())))
66                 .onErrorResume(err -> fetchVersion(ric, createStdA1ClientImpl(ric)))
67                 .doOnNext(version -> ric.setProtocolVersion(version))
68                 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
69                 .doOnError(t -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
70         } else {
71             return Mono.just(ric.getProtocolVersion());
72         }
73     }
74
75     protected A1Client createStdA1ClientImpl(Ric ric) {
76         return new StdA1Client(ric.getConfig());
77     }
78
79     protected A1Client createSdncOscA1Client(Ric ric) {
80         return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
81             appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
82     }
83
84     protected A1Client createSdnrOnapA1Client(Ric ric) {
85         return new SdnrOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
86             appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
87     }
88
89     private Mono<A1Client.A1ProtocolType> fetchVersion(Ric ric, A1Client a1Client) {
90         return Mono.just(a1Client) //
91             .flatMap(client -> a1Client.getProtocolVersion());
92     }
93
94 }