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

com.axway.apim.apiimport.actions.ManageApiMethods Maven / Gradle / Ivy

package com.axway.apim.apiimport.actions;

import com.axway.apim.adapter.APIManagerAdapter;
import com.axway.apim.adapter.apis.APIManagerAPIMethodAdapter;
import com.axway.apim.api.model.APIMethod;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.error.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ManageApiMethods {

    private static final Logger LOG = LoggerFactory.getLogger(ManageApiMethods.class);

    public void updateApiMethods(String frontendApiId, List actualApiMethods, List desiredApiMethods) throws AppException {
        APIManagerAdapter apiManager = APIManagerAdapter.getInstance();
        if (desiredApiMethods != null) {
            // Ignore if actual and desired methods are same
            List differences = actualApiMethods.stream().filter(desiredApiMethods::contains).collect(Collectors.toList());
            desiredApiMethods.removeAll(differences);
            LOG.info("Total number of methods to be updated : {}", desiredApiMethods.size());
            if (!desiredApiMethods.isEmpty()) {
                APIManagerAPIMethodAdapter apiManagerAPIMethodAdapter = apiManager.getMethodAdapter();
                List apiMethods = apiManagerAPIMethodAdapter.getAllMethodsForAPI(frontendApiId);
                List updatedMethodNames = new ArrayList<>();
                for (APIMethod apiMethod : desiredApiMethods) {
                    for (APIMethod method : apiMethods) {
                        String operationName = method.getName();
                        if (operationName.equals(apiMethod.getName())) {
                            LOG.info("Updating API method : {}", apiMethod.getName());
                            apiMethod.setId(method.getId());
                            apiMethod.setApiId(method.getApiId());
                            apiMethod.setVirtualizedApiId(method.getVirtualizedApiId());
                            apiMethod.setApiMethodId(method.getApiMethodId());
                            apiManagerAPIMethodAdapter.updateApiMethod(apiMethod);
                            updatedMethodNames.add(operationName);
                            break;
                        }
                    }
                }
            }
        }
    }

    public void isMethodMismatch(List actualApiMethods, List desiredApiMethods) throws AppException {
        if (desiredApiMethods == null)
            return;

        if (desiredApiMethods.size() > actualApiMethods.size()) {
            LOG.error("API Methods mismatch - Number of API Methods in API Manager : {} and Number of API Methods in API config file : {}", actualApiMethods.size(), desiredApiMethods.size());
            throw new AppException("API Methods mismatch", ErrorCode.BREAKING_CHANGE_DETECTED);
        }
        List desiredApiMethodsName = desiredApiMethods.stream().map(APIMethod::getName).collect(Collectors.toList());
        List actualApiMethodsName = actualApiMethods.stream().map(APIMethod::getName).collect(Collectors.toList());
        for (String desiredApiMethodName : desiredApiMethodsName) {
            if (!actualApiMethodsName.contains(desiredApiMethodName)) {
                LOG.error("API Method mismatch - Name of API Method  - {} - in API config file is not matching with  name of API Method in API Manager", desiredApiMethodName);
                throw new AppException("API Method mismatch", ErrorCode.BREAKING_CHANGE_DETECTED);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy