All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.mt.subscription.SubscriberSidecarImpl Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
/******************************************************************************
 * © 2020 SAP SE or an SAP affiliate company. All rights reserved.            *
 ******************************************************************************/
package com.sap.cloud.mt.subscription;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sap.cloud.mt.subscription.HanaEncryptionTool.DbEncryptionMode;
import com.sap.cloud.mt.subscription.exceptions.AuthorityError;
import com.sap.cloud.mt.subscription.exceptions.InternalError;
import com.sap.cloud.mt.subscription.exceptions.NotFound;
import com.sap.cloud.mt.subscription.exceptions.NotSupported;
import com.sap.cloud.mt.subscription.exceptions.ParameterError;
import com.sap.cloud.mt.subscription.exits.Exits;
import com.sap.cloud.mt.subscription.json.ApplicationDependency;
import com.sap.cloud.mt.subscription.json.DeletePayload;
import com.sap.cloud.mt.subscription.json.SidecarUpgradePayload;
import com.sap.cloud.mt.subscription.json.SubscriptionPayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class SubscriberSidecarImpl implements Subscriber {
    private static final Logger logger = LoggerFactory.getLogger(SubscriberSidecarImpl.class);
    private final Exits exits;
    private final SecurityChecker securityChecker;
    private final SaasRegistry saasRegistry;
    private final SidecarAccess sidecarAccess;
    private final MtxTools mtxTools;
    private final String baseUiUrl;
    private final String urlSeparator;
    private final boolean withoutAuthorityCheck;

    private SubscriberSidecarImpl(String baseUiUrl,
                                  String urlSeparator,
                                  Exits exits,
                                  SecurityChecker securityChecker, SaasRegistry saasRegistry,
                                  SidecarAccess sidecarAccess, PollingParameters sidecarPolling,
                                  boolean withoutAuthorityCheck, DbEncryptionMode hanaEncryptionMode) throws InternalError {
        this.baseUiUrl = baseUiUrl;
        this.urlSeparator = urlSeparator;
        this.exits = exits;
        this.saasRegistry = saasRegistry;
        if (exits.getUnSubscribeExit() == null) throw new InternalError("No unsubscribe exit found");
        this.securityChecker = securityChecker;
        this.sidecarAccess = sidecarAccess;
        this.mtxTools = new MtxTools(securityChecker, baseUiUrl, urlSeparator,
                sidecarPolling != null ? sidecarPolling : PollingParameters.Builder.create().timeout(Duration.ofHours(1)).build(), hanaEncryptionMode);
        this.withoutAuthorityCheck = withoutAuthorityCheck;
    }

    @Override
    public void unsubscribe(String tenantId, DeletePayload deletePayload, String jwt) throws InternalError, ParameterError, AuthorityError {
        mtxTools.unsubscribe(tenantId,
                () -> sidecarAccess.callSidecarUnsubscribe(tenantId, jwt, deletePayload),
                jobId -> sidecarAccess.callSidecarStatus(jobId),
                deletePayload,
                withoutAuthorityCheck, exits);
    }

    @Override
    public List getApplicationDependencies() throws AuthorityError {
        if (!withoutAuthorityCheck) {
            securityChecker.checkSubscriptionAuthority();
        }
        return exits.getDependencyExit() != null ? exits.getDependencyExit().onGetDependencies() : new ArrayList<>();
    }

    @Override
    public String subscribe(String tenantId, SubscriptionPayload subscriptionPayload, String jwt) throws InternalError, ParameterError, AuthorityError {
        return mtxTools.subscribe(tenantId,
                instanceCreationOptions -> sidecarAccess.callSidecarSubscribe(tenantId, jwt, subscriptionPayload, instanceCreationOptions),
                jobId -> sidecarAccess.callSidecarStatus(jobId),
                subscriptionPayload,
                withoutAuthorityCheck, exits);
    }

    @Override
    public String getSubscribeUrl(SubscriptionPayload subscriptionPayload) throws InternalError, ParameterError, AuthorityError {
        if (!withoutAuthorityCheck) {
            securityChecker.checkSubscriptionAuthority();
        }
        return Tools.getApplicationUrl(subscriptionPayload, exits.getSubscribeExit()::uiURL, exits.getSubscribeExit()::uiURL, baseUiUrl, urlSeparator);
    }

    @Override
    public void setupDbTables(List tenants) throws InternalError, NotSupported, AuthorityError {
        if (!withoutAuthorityCheck) {
            securityChecker.checkInitDbAuthority();
        }
        logger.debug("Synchronous init db is not supported with sidecar");
        throw new NotSupported("Synchronous init db is not supported with sidecar");
    }

    @Override
    public String setupDbTablesAsync(List tenants) throws InternalError, AuthorityError {
        if (!withoutAuthorityCheck) {
            securityChecker.checkInitDbAuthority();
        }
        SidecarUpgradePayload upgradePayload = new SidecarUpgradePayload();
        upgradePayload.tenants = tenants.toArray(new String[tenants.size()]);
        return sidecarAccess.callSidecarUpgrade(upgradePayload);
    }

    @Override
    public String updateStatus(String jobId) throws InternalError, ParameterError, NotFound, AuthorityError {
        if (!withoutAuthorityCheck) {
            securityChecker.checkInitDbAuthority();
        }
        if (!jobId.matches(Tools.SECURE_CHARS)) {
            throw new ParameterError("Job id contains illegal characters");
        }
        Map resultMap = sidecarAccess.callSidecarStatus(jobId);
        try {
            return new ObjectMapper().writeValueAsString(resultMap);
        } catch (JsonProcessingException e) {
            throw new InternalError(e);
        }
    }

    @Override
    public void callSaasRegistry(boolean ok, String message, String applicationUrl, String saasRegistryUrl) throws InternalError {
        saasRegistry.callBackSaasRegistry(ok, message, applicationUrl, saasRegistryUrl);
    }

    @Override
    public void checkAuthority(SecurityChecker.Authority authority) throws AuthorityError {
        securityChecker.checkAuthority(authority);
    }

    public static final class SubscriberSidecarImplBuilder {
        private Exits exits;
        private String baseUiUrl;
        private String urlSeparator;
        private SecurityChecker securityChecker;
        private SaasRegistry saasRegistry;
        private SidecarAccess sidecarAccess;
        private boolean withoutAuthorityCheck;
        private DbEncryptionMode hanaEncryptionMode;
        private ServiceSpecification sidecarSpecification;

        private SubscriberSidecarImplBuilder() {
        }

        public static SubscriberSidecarImplBuilder create() {
            return new SubscriberSidecarImplBuilder();
        }

        public SubscriberSidecarImplBuilder exits(Exits exits) {
            this.exits = exits;
            return this;
        }

        public SubscriberSidecarImplBuilder baseUiUrl(String baseUiUrl) {
            this.baseUiUrl = baseUiUrl;
            return this;
        }

        public SubscriberSidecarImplBuilder urlSeparator(String urlSeparator) {
            this.urlSeparator = urlSeparator;
            return this;
        }

        public SubscriberSidecarImplBuilder securityChecker(SecurityChecker securityChecker) {
            this.securityChecker = securityChecker;
            return this;
        }

        public SubscriberSidecarImplBuilder saasRegistry(SaasRegistry saasRegistry) {
            this.saasRegistry = saasRegistry;
            return this;
        }

        public SubscriberSidecarImplBuilder sidecarAccess(SidecarAccess sidecarAccess) {
            this.sidecarAccess = sidecarAccess;
            return this;
        }

        public SubscriberSidecarImplBuilder withoutAuthorityCheck(boolean withoutAuthorityCheck) {
            this.withoutAuthorityCheck = withoutAuthorityCheck;
            return this;
        }

        public SubscriberSidecarImplBuilder hanaEncryptionMode(DbEncryptionMode hanaEncryptionMode) {
            this.hanaEncryptionMode = hanaEncryptionMode;
            return this;
        }

        public SubscriberSidecarImplBuilder sidecarSpecification(ServiceSpecification sidecarSpecification) {
            this.sidecarSpecification = sidecarSpecification;
            return this;
        }

        public SubscriberSidecarImpl build() throws InternalError {
            if (sidecarAccess == null) {
                sidecarAccess = new SidecarAccess(sidecarSpecification.getUrl(), sidecarSpecification.getResilienceConfig()
                        , sidecarSpecification.getRequestEnhancer());
            }
            return new SubscriberSidecarImpl(baseUiUrl, urlSeparator, exits, securityChecker, saasRegistry, sidecarAccess, sidecarSpecification.getPolling(),
                    withoutAuthorityCheck, hanaEncryptionMode);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy