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

com.sap.cds.feature.mt.MtSubscriptionServiceImpl Maven / Gradle / Ivy

/**************************************************************************
 * (C) 2019-2020 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.feature.mt;

import java.util.List;
import java.util.function.Supplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.cds.feature.LoadableService;
import com.sap.cds.services.impl.ServiceCatalogSPI;
import com.sap.cds.services.impl.ServiceImpl;
import com.sap.cds.services.impl.handlerregistry.HandlerRegistryTools;
import com.sap.cds.services.impl.request.RequestContextImpl;
import com.sap.cds.services.impl.request.RequestContextSPI;
import com.sap.cds.services.impl.runtime.CdsRuntimeRepository;
import com.sap.cds.services.mt.MtAsyncDeployEventContext;
import com.sap.cds.services.mt.MtAsyncDeployStatusEventContext;
import com.sap.cds.services.mt.MtDeployEventContext;
import com.sap.cds.services.mt.MtGetDependenciesEventContext;
import com.sap.cds.services.mt.MtSubscribeEventContext;
import com.sap.cds.services.mt.MtSubscriptionService;
import com.sap.cds.services.mt.MtUnsubscribeEventContext;
import com.sap.cds.services.runtime.CdsRuntime;
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;

public class MtSubscriptionServiceImpl extends ServiceImpl implements MtSubscriptionService, LoadableService {

	private Logger logger = LoggerFactory.getLogger(MtSubscriptionServiceImpl.class);

	public MtSubscriptionServiceImpl() {
		super(MtSubscriptionService.DEFAULT_NAME);
	}

	@Override
	public List getDependencies() {
		return runWithStaticModel(() -> {
			MtGetDependenciesEventContext context = MtGetDependenciesEventContext.create();
			emit(context);
			return context.getResult();
		});
	}

	@Override
	public String subscribe(SubscriptionPayload payload, String tenantId) {
		return runWithStaticModel(() -> {
			MtSubscribeEventContext context = MtSubscribeEventContext.create();
			context.setSubscriptionPayload(payload);
			context.setTenantId(tenantId);
			emit(context);
			return context.getResult();
		});
	}

	@Override
	public void unsubscribe(DeletePayload payload, String tenantId) {
		runWithStaticModel(() -> {
			MtUnsubscribeEventContext context = MtUnsubscribeEventContext.create();
			context.setDeletePayload(payload);
			context.setTenantId(tenantId);
			emit(context);
		});
	}

	@Override
	public void deploy(SidecarUpgradePayload payload) {
		runWithStaticModel(() -> {
			MtDeployEventContext context = MtDeployEventContext.create();
			context.setUpgradePayload(payload);
			emit(context);
		});
	}

	@Override
	public String asyncDeploy(SidecarUpgradePayload payload) {
		return runWithStaticModel(() -> {
			MtAsyncDeployEventContext context = MtAsyncDeployEventContext.create();
			context.setUpgradePayload(payload);
			emit(context);
			return context.getResult();
		});
	}

	@Override
	public String asyncDeployStatus(String jobId) {
		return runWithStaticModel(() -> {
			MtAsyncDeployStatusEventContext context = MtAsyncDeployStatusEventContext.create();
			context.setJobId(jobId);
			emit(context);
			return context.getResult();
		});
	}

	@Override
	public boolean isActiveFeature() {
		MtFeatureConditions unmetCondition = MtFeatureConditions.unmetCondition();
		if (unmetCondition != null) {
			logger.info(unmetCondition.getMessage());
			return false;
		}
		return true;
	}

	@Override
	public String getFeatureName() {
		return "Multi-Tenancy Feature";
	}

	@Override
	public void init() {
		HandlerRegistryTools.registerInstance(new MtSubscriptionDefaultOnHandler(), this);
	}

	// This is necessary because the subscription endpoints will always be called with the sap-provisioning tenant
	// which is not subscribed. Therefore it would not be possible to retrieve a model from the sidecar in this case.
	private  T runWithStaticModel(Supplier supplier) {
		CdsRuntime cdsRuntime = CdsRuntimeRepository.getDefault();
		try (RequestContextSPI context = RequestContextImpl.open(cdsRuntime.getCdsModel(), (ServiceCatalogSPI) cdsRuntime.getServiceCatalog())) {
			return supplier.get();
		}
	}

	private void runWithStaticModel(Runnable runnable) {
		CdsRuntime cdsRuntime = CdsRuntimeRepository.getDefault();
		try (RequestContextSPI context = RequestContextImpl.open(cdsRuntime.getCdsModel(), (ServiceCatalogSPI) cdsRuntime.getServiceCatalog())) {
			runnable.run();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy