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

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

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sap.cloud.mt.subscription.exceptions.InternalError;
import com.sap.cloud.mt.subscription.exceptions.NotFound;
import com.sap.cloud.mt.subscription.json.DeletePayload;
import com.sap.cloud.mt.subscription.json.SidecarUnSubscriptionPayload;
import com.sap.cloud.mt.subscription.json.SidecarUpgradePayload;
import com.sap.cloud.mt.subscription.json.SubscriptionPayload;
import com.sap.cloud.mt.tools.api.RequestEnhancer;
import com.sap.cloud.mt.tools.api.ResilienceConfig;
import com.sap.cloud.mt.tools.api.ServiceCall;
import com.sap.cloud.mt.tools.api.ServiceEndpoint;
import com.sap.cloud.mt.tools.api.ServiceResponse;
import com.sap.cloud.mt.tools.exception.InternalException;
import com.sap.cloud.mt.tools.exception.ServiceException;
import com.sap.xsa.core.instancemanager.client.InstanceCreationOptions;
import org.apache.http.HttpHeaders;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static com.sap.cloud.mt.subscription.MtxTools.extractJobId;
import static org.apache.http.HttpStatus.SC_ACCEPTED;
import static org.apache.http.HttpStatus.SC_BAD_GATEWAY;
import static org.apache.http.HttpStatus.SC_CREATED;
import static org.apache.http.HttpStatus.SC_GATEWAY_TIMEOUT;
import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR;
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
import static org.apache.http.HttpStatus.SC_NO_CONTENT;
import static org.apache.http.HttpStatus.SC_OK;
import static org.apache.http.HttpStatus.SC_SERVICE_UNAVAILABLE;

public class SidecarAccess {
    private static final String SIDECAR_SUBSCRIBE_ENDPOINT = "/mtx/v1/internal/provisioning/subscribe";
    private static final String SIDECAR_UNSUBSCRIBE_ENDPOINT = "/mtx/v1/internal/provisioning/unsubscribe";
    private static final String SIDECAR_UPGRADE_ENDPOINT = "/mtx/v1/model/asyncUpgrade";
    private static final String SIDECAR_STATUS_ENDPOINT = "/mtx/v1/model/status/";
    private static final String MTX_SIDECAR_DESTINATION = "com.sap.cds.mtxSidecar";
    private static final String UNEXPECTED_RETURN_CODE = "Unexpected return code ";
    private final ServiceEndpoint upgradeEndpoint;
    private final ServiceEndpoint getStatusEndpoint;
    private final ServiceEndpoint subscribeEndpoint;
    private final ServiceEndpoint unsubscribeEndpoint;
    private final RequestEnhancer requestEnhancer;


    SidecarAccess(String sidecarUrl, ResilienceConfig resilienceConfig, RequestEnhancer requestEnhancer) throws InternalError {
        this.requestEnhancer = requestEnhancer;
        Set retryCodes = new HashSet<>();
        retryCodes.add(SC_BAD_GATEWAY);
        retryCodes.add(SC_GATEWAY_TIMEOUT);
        retryCodes.add(SC_INTERNAL_SERVER_ERROR);
        retryCodes.add(SC_SERVICE_UNAVAILABLE);
        retryCodes.add(SC_NOT_FOUND);
        try {
            upgradeEndpoint = ServiceEndpoint.create()
                    .at(sidecarUrl)
                    .destinationName(MTX_SIDECAR_DESTINATION)
                    .path(SIDECAR_UPGRADE_ENDPOINT)
                    .returnCodeChecker(c -> {
                        if (c != SC_OK) {
                            return new InternalError(UNEXPECTED_RETURN_CODE + c);
                        } else {
                            return null;
                        }
                    }).retry().forReturnCodes(retryCodes)
                    .config(resilienceConfig)
                    .end();
            getStatusEndpoint = ServiceEndpoint.create()
                    .at(sidecarUrl)
                    .destinationName(MTX_SIDECAR_DESTINATION)
                    .path(SIDECAR_STATUS_ENDPOINT)
                    .returnCodeChecker(c -> {
                        if (c == SC_NOT_FOUND) {
                            return new NotFound("Job id not known");
                        }
                        if (c != SC_OK) {
                            return new InternalError(UNEXPECTED_RETURN_CODE + c);
                        }
                        return null;
                    }).retry().forReturnCodes(retryCodes)
                    .config(resilienceConfig)
                    .end();
            subscribeEndpoint = ServiceEndpoint.create()
                    .at(sidecarUrl)
                    .destinationName(MTX_SIDECAR_DESTINATION)
                    .path(SIDECAR_SUBSCRIBE_ENDPOINT)
                    .returnCodeChecker(c -> {
                        if (c != SC_CREATED && c != SC_OK && c != SC_ACCEPTED) {
                            return new InternalError(UNEXPECTED_RETURN_CODE + c);
                        }
                        return null;
                    }).retry().forReturnCodes(retryCodes)
                    .config(resilienceConfig)
                    .end();
            unsubscribeEndpoint = ServiceEndpoint.create()
                    .at(sidecarUrl)
                    .destinationName(MTX_SIDECAR_DESTINATION)
                    .path(SIDECAR_UNSUBSCRIBE_ENDPOINT)
                    .returnCodeChecker(c -> {
                        if (c != SC_ACCEPTED && c != SC_NO_CONTENT) {
                            return new InternalError(UNEXPECTED_RETURN_CODE + c);
                        }
                        return null;
                    }).retry().forReturnCodes(retryCodes)
                    .config(resilienceConfig)
                    .end();
        } catch (InternalException e) {
            throw new InternalError(e);
        }
    }

    public String callSidecarUpgrade(SidecarUpgradePayload upgradePayload) throws InternalError {
        try {
            ServiceCall upgrade = upgradeEndpoint.createServiceCall()
                    .http()
                    .post()
                    .payload(upgradePayload)
                    .noPathParameter()
                    .noQuery()
                    .enhancer(requestEnhancer)
                    .end();
            ServiceResponse response = upgrade.execute(String.class);
            return response.getPayload().orElse("");
        } catch (InternalException e) {
            throw new InternalError(e);
        } catch (ServiceException e) {
            if (e.getCause() instanceof InternalError) {
                throw (InternalError) e.getCause();
            }
            throw new InternalError(e);
        }
    }

    public Map callSidecarStatus(String jobId) throws InternalError, NotFound {
        try {
            ServiceCall getStatus = getStatusEndpoint.createServiceCall()
                    .http()
                    .get()
                    .withoutPayload()
                    .pathParameter(jobId)
                    .noQuery()
                    .enhancer(requestEnhancer)
                    .end();
            ServiceResponse response = getStatus.execute(Map.class);//NOSONAR
            if (response.getHttpStatusCode() == SC_NOT_FOUND) {
                throw new NotFound("Job id not known");
            }
            return response.getPayload().orElse(new HashMap());
        } catch (InternalException e) {
            throw new InternalError(e);
        } catch (ServiceException e) {
            if (e.getCause() instanceof InternalError) {
                throw (InternalError) e.getCause();
            }
            if (e.getCause() instanceof NotFound) {
                throw (NotFound) e.getCause();
            }
            throw new InternalError(e);
        }
    }

    public String callSidecarSubscribe(String tenantId, String jwt, SubscriptionPayload subscriptionPayload, InstanceCreationOptions instanceCreationOptions) throws InternalError {
        Map payload
                = new ObjectMapper().
                convertValue(Tools.getSubscriptionPayloadForSidecar(subscriptionPayload, instanceCreationOptions, false), Map.class);
        payload.put("subscribedTenantId", tenantId);
        payload.put("async", true);
        try {
            ServiceCall subscribe = subscribeEndpoint.createServiceCall()
                    .http()
                    .post()
                    .payload(payload)
                    .noPathParameter()
                    .noQuery()
                    .enhancer(request -> request.addHeader(HttpHeaders.AUTHORIZATION, jwt))
                    .end();
            return extractJobId(subscribe.execute(String.class));
        } catch (InternalException e) {
            throw new InternalError(e);
        } catch (ServiceException e) {
            if (e.getCause() instanceof InternalError) {
                throw (InternalError) e.getCause();
            }
            throw new InternalError(e);
        }
    }

    public String callSidecarUnsubscribe(String tenantId, String jwt, DeletePayload deletePayloadCopy)
            throws InternalError {
        Map payload = new ObjectMapper().convertValue(new SidecarUnSubscriptionPayload(deletePayloadCopy), Map.class);
        payload.put("subscribedTenantId", tenantId);
        payload.put("async", true);
        try {
            ServiceCall unsubscribe = unsubscribeEndpoint.createServiceCall()
                    .http()
                    .post()
                    .payload(payload)
                    .noPathParameter()
                    .noQuery()
                    .enhancer(request -> request.addHeader(HttpHeaders.AUTHORIZATION, jwt))
                    .end();
            return extractJobId(unsubscribe.execute(String.class));
        } catch (InternalException e) {
            throw new InternalError(e);
        } catch (ServiceException e) {
            if (e.getCause() instanceof InternalError) {
                throw (InternalError) e.getCause();
            }
            throw new InternalError(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy