package org.oransc.policyagent.clients;
+import lombok.Getter;
+
import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
import org.oransc.policyagent.configuration.ApplicationConfig;
+import org.oransc.policyagent.configuration.ControllerConfig;
+import org.oransc.policyagent.exceptions.ServiceException;
import org.oransc.policyagent.repository.Ric;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
/**
- * Factory for A1 clients that supports four different protocol versions of the A1 api.
+ * Factory for A1 clients that supports four different protocol versions of the
+ * A1 api.
*/
public class A1ClientFactory {
private static final Logger logger = LoggerFactory.getLogger(A1ClientFactory.class);
+ @Getter
private final ApplicationConfig appConfig;
@Autowired
* Creates an A1 client with the correct A1 protocol for the provided Ric.
*
* <p>
- * It detects the protocol version by trial and error, since there is no getVersion method specified in the A1
- * api yet.
+ * It detects the protocol version by trial and error, since there is no
+ * getVersion method specified in the A1 api yet.
*
* <p>
- * As a side effect it also sets the protocol version in the provided Ric. This means that after the first
- * successful creation it won't have to try which protocol to use, but can create the client directly.
+ * As a side effect it also sets the protocol version in the provided Ric. This
+ * means that after the first successful creation it won't have to try which
+ * protocol to use, but can create the client directly.
*
- * @param ric The Ric to get a client for.
- * @return a client with the correct protocol, or a ServiceException if none of the protocols are supported by the
- * Ric.
+ * @param ric The RIC to get a client for.
+ * @return a client with the correct protocol, or a ServiceException if none of
+ * the protocols are supported by the Ric.
*/
public Mono<A1Client> createA1Client(Ric ric) {
return getProtocolVersion(ric) //
- .flatMap(version -> createA1Client(ric, version));
+ .flatMap(version -> createA1ClientMono(ric, version));
}
- private Mono<A1Client> createA1Client(Ric ric, A1ProtocolType version) {
- if (version == A1ProtocolType.STD_V1) {
- return Mono.just(createStdA1ClientImpl(ric));
+ A1Client createClient(Ric ric, A1ProtocolType version) throws ServiceException {
+ if (version == A1ProtocolType.STD_V1_1) {
+ assertNoControllerConfig(ric, version);
+ return new StdA1ClientVersion1(ric.getConfig(), this.appConfig.getWebClientConfig());
} else if (version == A1ProtocolType.OSC_V1) {
- return Mono.just(createOscA1Client(ric));
- } else if (version == A1ProtocolType.SDNC_OSC) {
- return Mono.just(createSdncOscA1Client(ric));
- } else { // A1ProtocolType.SDNR_ONAP
- return Mono.just(createSdnrOnapA1Client(ric));
- }
- }
-
- private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
- if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
- return fetchVersion(createSdnrOnapA1Client(ric)) //
- .onErrorResume(notUsed -> fetchVersion(createSdncOscA1Client(ric))) //
- .onErrorResume(notUsed -> fetchVersion(createOscA1Client(ric))) //
- .onErrorResume(notUsed -> fetchVersion(createStdA1ClientImpl(ric))) //
- .doOnNext(ric::setProtocolVersion)
- .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
- .doOnError(notUsed -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
+ assertNoControllerConfig(ric, version);
+ return new OscA1Client(ric.getConfig(), this.appConfig.getWebClientConfig());
+ } else if (version == A1ProtocolType.SDNC_OSC_STD_V1_1 || version == A1ProtocolType.SDNC_OSC_OSC_V1) {
+ return new SdncOscA1Client(version, ric.getConfig(), getControllerConfig(ric),
+ this.appConfig.getWebClientConfig());
+ } else if (version == A1ProtocolType.SDNC_ONAP) {
+ return new SdncOnapA1Client(ric.getConfig(), getControllerConfig(ric), this.appConfig.getWebClientConfig());
} else {
- return Mono.just(ric.getProtocolVersion());
+ logger.error("Unhandled protocol: {}", version);
+ throw new ServiceException("Unhandled protocol");
}
}
- protected A1Client createOscA1Client(Ric ric) {
- return new OscA1Client(ric.getConfig());
+ private ControllerConfig getControllerConfig(Ric ric) throws ServiceException {
+ String controllerName = ric.getConfig().controllerName();
+ if (controllerName.isEmpty()) {
+ ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
+ throw new ServiceException("No controller configured for RIC: " + ric.name());
+ }
+ try {
+ return this.appConfig.getControllerConfig(controllerName);
+ } catch (ServiceException e) {
+ ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
+ throw e;
+ }
}
- protected A1Client createStdA1ClientImpl(Ric ric) {
- return new StdA1Client(ric.getConfig());
+ private void assertNoControllerConfig(Ric ric, A1ProtocolType version) throws ServiceException {
+ if (!ric.getConfig().controllerName().isEmpty()) {
+ ric.setProtocolVersion(A1ProtocolType.UNKNOWN);
+ throw new ServiceException(
+ "Controller config should be empty, ric: " + ric.name() + " when using protocol version: " + version);
+ }
}
- protected A1Client createSdncOscA1Client(Ric ric) {
- return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
- appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
+ private Mono<A1Client> createA1ClientMono(Ric ric, A1ProtocolType version) {
+ try {
+ return Mono.just(createClient(ric, version));
+ } catch (ServiceException e) {
+ return Mono.error(e);
+ }
}
- protected A1Client createSdnrOnapA1Client(Ric ric) {
- return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(),
- appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword());
+ private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
+ if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
+ return fetchVersion(ric, A1ProtocolType.STD_V1_1) //
+ .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.OSC_V1)) //
+ .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.SDNC_OSC_STD_V1_1)) //
+ .onErrorResume(notUsed -> fetchVersion(ric, A1ProtocolType.SDNC_ONAP)) //
+ .doOnNext(ric::setProtocolVersion)
+ .doOnNext(version -> logger.debug("Established protocol version:{} for Ric: {}", version, ric.name())) //
+ .doOnError(notUsed -> logger.warn("Could not get protocol version from RIC: {}", ric.name())) //
+ .onErrorResume(
+ notUsed -> Mono.error(new ServiceException("Protocol negotiation failed for " + ric.name())));
+ } else {
+ return Mono.just(ric.getProtocolVersion());
+ }
}
- private Mono<A1ProtocolType> fetchVersion(A1Client a1Client) {
- return Mono.just(a1Client) //
- .flatMap(client -> a1Client.getProtocolVersion());
+ private Mono<A1ProtocolType> fetchVersion(Ric ric, A1ProtocolType protocolType) {
+ return createA1ClientMono(ric, protocolType) //
+ .flatMap(A1Client::getProtocolVersion);
}
}