diff --git a/src/main/java/eu/h2020/symbiote/client/AbstractSymbIoTeClientFactory.java b/src/main/java/eu/h2020/symbiote/client/AbstractSymbIoTeClientFactory.java index c8b26d5..53ddaf7 100644 --- a/src/main/java/eu/h2020/symbiote/client/AbstractSymbIoTeClientFactory.java +++ b/src/main/java/eu/h2020/symbiote/client/AbstractSymbIoTeClientFactory.java @@ -75,6 +75,14 @@ public static AbstractSymbIoTeClientFactory getFactory(Config config) */ public abstract PRClient getPRClient(String platformId); + /** + * Get a Subscription Manager (SM) client for querying the SM of a specific platform + * + * @param platformId the platform which we want to communicate with + * @return a SM client + */ + public abstract SMClient getSMClient(String platformId); + /** * Get an Authentication and Authorization (AAM) client to communicate with an AAM of a specific platform * diff --git a/src/main/java/eu/h2020/symbiote/client/feign/FeignSMClient.java b/src/main/java/eu/h2020/symbiote/client/feign/FeignSMClient.java new file mode 100644 index 0000000..0eb4572 --- /dev/null +++ b/src/main/java/eu/h2020/symbiote/client/feign/FeignSMClient.java @@ -0,0 +1,65 @@ +package eu.h2020.symbiote.client.feign; + +import eu.h2020.symbiote.client.interfaces.SMClient; +import eu.h2020.symbiote.cloud.model.internal.Subscription; +import eu.h2020.symbiote.security.commons.exceptions.custom.SecurityHandlerException; +import eu.h2020.symbiote.security.communication.ApacheCommonsLogger4Feign; +import eu.h2020.symbiote.security.communication.payloads.AAM; +import eu.h2020.symbiote.security.handler.ISecurityHandler; +import feign.Feign; +import feign.Headers; +import feign.Logger; +import feign.RequestLine; +import feign.jackson.JacksonDecoder; +import feign.jackson.JacksonEncoder; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Map; + +/** + * symbIoTe Subscription Manager client based on Feign + * + * @author Vasilis Glykantzis + */ +public class FeignSMClient implements SMClient { + + private static final Log logger = LogFactory.getLog(FeignSMClient.class); + + private SubscriptionManagerI smClient; + + /** + * + * @param securityHandler the security handler implementation that is going to be used + * @param homePlatformId the home Platform Id + */ + public FeignSMClient(ISecurityHandler securityHandler, + String homePlatformId) { + + try { + Map availableAAMs = securityHandler.getAvailableAAMs(); + String smBaseUrl = availableAAMs.get(homePlatformId).getAamAddress().replace("/paam", "/subscriptionManager"); + + this.smClient = Feign.builder() + .decoder(new JacksonDecoder()) + .encoder(new JacksonEncoder()) + .logger(new ApacheCommonsLogger4Feign(logger)) + .logLevel(Logger.Level.FULL) + .client(new SymbIoTeSecurityHandlerFeignClient()) + .target(SubscriptionManagerI.class, smBaseUrl); + } catch (SecurityHandlerException e) { + logger.error("Could not create FeignSMClient", e); + } + } + + @Override + public void subscribe(Subscription subscription) { + this.smClient.subscribe(subscription); + } + + private interface SubscriptionManagerI { + @RequestLine("POST /subscribe ") + @Headers({"Accept: application/json", "Content-Type: application/json"}) + void subscribe(Subscription subscription); + } +} diff --git a/src/main/java/eu/h2020/symbiote/client/feign/SymbIoTeFeignClientFactory.java b/src/main/java/eu/h2020/symbiote/client/feign/SymbIoTeFeignClientFactory.java index e2e2102..0e6e458 100644 --- a/src/main/java/eu/h2020/symbiote/client/feign/SymbIoTeFeignClientFactory.java +++ b/src/main/java/eu/h2020/symbiote/client/feign/SymbIoTeFeignClientFactory.java @@ -95,6 +95,9 @@ public void checkServerTrusted( @Override public PRClient getPRClient(String platformId) { return new FeignPRClient(securityHandler, platformId); } + @Override + public SMClient getSMClient(String platformId) { return new FeignSMClient(securityHandler, platformId); } + @Override public IAAMClient getAAMClient(String homePlatformId) { try { diff --git a/src/main/java/eu/h2020/symbiote/client/interfaces/SMClient.java b/src/main/java/eu/h2020/symbiote/client/interfaces/SMClient.java new file mode 100644 index 0000000..72143a7 --- /dev/null +++ b/src/main/java/eu/h2020/symbiote/client/interfaces/SMClient.java @@ -0,0 +1,18 @@ +package eu.h2020.symbiote.client.interfaces; + +import eu.h2020.symbiote.cloud.model.internal.Subscription; + +/** + * Interface for querying the platform Subscription Manager component + * + * @author Vasilis Glykantzis + */ +public interface SMClient { + + /** + * Queries the Platform Registry component with home token + * + * @param subscription the subscription request send to the Subscription Manager + */ + void subscribe(Subscription subscription); +}