X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FA1ClientFactory.java;h=0be4b31b069e744bbe762fe933edbb0d2a636da9;hb=96805287db06a91e569c184cc34ba4e69e4d22b2;hp=c150c08933279254fdd569581de447e72ef335b3;hpb=10811110a23cf9fcf464575bba609b8c65d19aec;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java index c150c089..0be4b31b 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * O-RAN-SC * %% - * Copyright (C) 2019 Nordix Foundation + * Copyright (C) 2020 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,10 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import reactor.core.publisher.Mono; +/** + * 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); @@ -40,55 +44,65 @@ public class A1ClientFactory { this.appConfig = appConfig; } + /** + * Creates an A1 client with the correct A1 protocol for the provided Ric. + * + *

+ * It detects the protocol version by trial and error, since there is no + * getVersion method specified in the A1 api yet. + * + *

+ * 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. + */ public Mono createA1Client(Ric ric) { return getProtocolVersion(ric) // - .flatMap(version -> createA1Client(ric, version)); + .flatMap(version -> createA1ClientMono(ric, version)); } - private Mono createA1Client(Ric ric, A1ProtocolType version) { - if (version == A1ProtocolType.STD_V1) { - return Mono.just(createStdA1ClientImpl(ric)); + A1Client createClient(Ric ric, A1ProtocolType version) { + if (version == A1ProtocolType.STD_V1_1) { + return new StdA1ClientVersion1(ric.getConfig()); } else if (version == A1ProtocolType.OSC_V1) { - return Mono.just(new OscA1Client(ric.getConfig())); + return new OscA1Client(ric.getConfig()); } else if (version == A1ProtocolType.SDNC_OSC) { - return Mono.just(createSdncOscA1Client(ric)); - } else if (version == A1ProtocolType.SDNR_ONAP) { - return Mono.just(createSdnrOnapA1Client(ric)); + return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(), + appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword()); + } else if (version == A1ProtocolType.SDNC_ONAP) { + return new SdncOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(), + appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword()); + } else { + logger.error("Unhandled protocol: {}", version); + return null; } - return Mono.error(new ServiceException("Not supported protocoltype: " + version)); + } + + private Mono createA1ClientMono(Ric ric, A1ProtocolType version) { + A1Client c = createClient(ric, version); + return c != null ? Mono.just(c) : Mono.error(new ServiceException("Unhandled protocol: " + version)); } private Mono getProtocolVersion(Ric ric) { if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) { - return fetchVersion(ric, createSdnrOnapA1Client(ric)) // - .onErrorResume(err -> fetchVersion(ric, createSdncOscA1Client(ric))) - .onErrorResume(err -> fetchVersion(ric, new OscA1Client(ric.getConfig()))) - .onErrorResume(err -> fetchVersion(ric, createStdA1ClientImpl(ric))) - .doOnNext(version -> ric.setProtocolVersion(version)) - .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) // - .doOnError(t -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); // + return fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1)) // + .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.OSC_V1))) // + .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_OSC))) // + .onErrorResume(notUsed -> fetchVersion(createClient(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())); // } else { return Mono.just(ric.getProtocolVersion()); } } - protected A1Client createStdA1ClientImpl(Ric ric) { - return new StdA1Client(ric.getConfig()); - } - - protected A1Client createSdncOscA1Client(Ric ric) { - return new SdncOscA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(), - appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword()); - } - - protected A1Client createSdnrOnapA1Client(Ric ric) { - return new SdnrOnapA1Client(ric.getConfig(), appConfig.getA1ControllerBaseUrl(), - appConfig.getA1ControllerUsername(), appConfig.getA1ControllerPassword()); - } - - private Mono fetchVersion(Ric ric, A1Client a1Client) { + private Mono fetchVersion(A1Client a1Client) { return Mono.just(a1Client) // .flatMap(client -> a1Client.getProtocolVersion()); } - }