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