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