Search in sources :

Example 21 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIProviderImpl method changeAPILCCheckListItems.

@Override
public boolean changeAPILCCheckListItems(APIIdentifier apiIdentifier, int checkItem, boolean checkItemValue) throws APIManagementException {
    String providerTenantMode = apiIdentifier.getProviderName();
    boolean success = false;
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        }
        GenericArtifact apiArtifact = getAPIArtifact(apiIdentifier);
        String status = null;
        try {
            if (apiArtifact != null) {
                if (checkItemValue && !apiArtifact.isLCItemChecked(checkItem, APIConstants.API_LIFE_CYCLE)) {
                    apiArtifact.checkLCItem(checkItem, APIConstants.API_LIFE_CYCLE);
                } else if (!checkItemValue && apiArtifact.isLCItemChecked(checkItem, APIConstants.API_LIFE_CYCLE)) {
                    apiArtifact.uncheckLCItem(checkItem, APIConstants.API_LIFE_CYCLE);
                }
                success = true;
            }
        } catch (GovernanceException e) {
            handleException("Error while setting registry lifecycle checklist items for the API: " + apiIdentifier.getApiName(), e);
        }
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return success;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException)

Example 22 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIProviderImpl method getAPILifeCycleData.

@Override
public /*
    * This method returns the lifecycle data for an API including current state,next states.
    *
    * @param apiId APIIdentifier
    * @return Map<String,Object> a map with lifecycle data
    */
Map<String, Object> getAPILifeCycleData(APIIdentifier apiId) throws APIManagementException {
    String path = APIUtil.getAPIPath(apiId);
    Map<String, Object> lcData = new HashMap<String, Object>();
    String providerTenantMode = apiId.getProviderName();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        }
        Resource apiSourceArtifact = registry.get(path);
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when getting lifecycle data for API " + apiId;
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(apiSourceArtifact.getUUID());
        // Get all the actions corresponding to current state of the api artifact
        String[] actions = artifact.getAllLifecycleActions(APIConstants.API_LIFE_CYCLE);
        // Put next states into map
        lcData.put(APIConstants.LC_NEXT_STATES, actions);
        String lifeCycleState = artifact.getLifecycleState();
        lcData.put(APIConstants.LC_STATUS, lifeCycleState);
        LifecycleBean bean;
        bean = LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) registry, configRegistry);
        if (bean != null) {
            ArrayList<CheckListItem> checkListItems = new ArrayList<CheckListItem>();
            ArrayList<String> permissionList = new ArrayList<String>();
            // Get lc properties
            Property[] lifecycleProps = bean.getLifecycleProperties();
            // Get roles of the current session holder
            String[] roleNames = bean.getRolesOfUser();
            for (Property property : lifecycleProps) {
                String propName = property.getKey();
                String[] propValues = property.getValues();
                // Check for permission properties if any exists
                if (propValues != null && propValues.length != 0) {
                    if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
                        for (String role : roleNames) {
                            for (String propValue : propValues) {
                                String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX, "");
                                if (propValue.equals(role)) {
                                    permissionList.add(key);
                                } else if (propValue.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propValue.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX)) {
                                    permissionList.add(key);
                                }
                            }
                        }
                    }
                }
            }
            // Check for lifecycle checklist item properties defined
            for (Property property : lifecycleProps) {
                String propName = property.getKey();
                String[] propValues = property.getValues();
                if (propValues != null && propValues.length != 0) {
                    CheckListItem checkListItem = new CheckListItem();
                    checkListItem.setVisible("false");
                    if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_ITEM_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
                        if (propValues.length > 2) {
                            for (String param : propValues) {
                                if (param.startsWith(APIConstants.LC_STATUS)) {
                                    checkListItem.setLifeCycleStatus(param.substring(7));
                                } else if (param.startsWith(APIConstants.LC_CHECK_ITEM_NAME)) {
                                    checkListItem.setName(param.substring(5));
                                } else if (param.startsWith(APIConstants.LC_CHECK_ITEM_VALUE)) {
                                    checkListItem.setValue(param.substring(6));
                                } else if (param.startsWith(APIConstants.LC_CHECK_ITEM_ORDER)) {
                                    checkListItem.setOrder(param.substring(6));
                                }
                            }
                        }
                        String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_ITEM_SUFFIX, "");
                        if (permissionList.contains(key)) {
                            // Set visible to true if the checklist item permits
                            checkListItem.setVisible("true");
                        }
                    }
                    if (checkListItem.matchLifeCycleStatus(lifeCycleState)) {
                        checkListItems.add(checkListItem);
                    }
                }
            }
            lcData.put("items", checkListItems);
        }
    } catch (Exception e) {
        handleException(e.getMessage(), e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return lcData;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CheckListItem(org.wso2.carbon.governance.custom.lifecycles.checklist.util.CheckListItem) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) ArrayList(java.util.ArrayList) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) OMException(org.apache.axiom.om.OMException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) LifecycleBean(org.wso2.carbon.governance.custom.lifecycles.checklist.beans.LifecycleBean) JSONObject(org.json.simple.JSONObject) Property(org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property)

Example 23 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIProviderImpl method getApiOrApiProductLifecycleData.

private Map<String, Object> getApiOrApiProductLifecycleData(String status) throws APIManagementException {
    Map<String, Object> lcData = new HashMap<String, Object>();
    List<String> actionsList;
    try {
        actionsList = LCManagerFactory.getInstance().getLCManager().getAllowedActionsForState(status);
        if (actionsList != null) {
            String[] actionsArray = new String[actionsList.size()];
            actionsArray = actionsList.toArray(actionsArray);
            lcData.put(APIConstants.LC_NEXT_STATES, actionsArray);
        }
        ArrayList<CheckListItem> checkListItems = new ArrayList<CheckListItem>();
        List<String> checklistItemsList = LCManagerFactory.getInstance().getLCManager().getCheckListItemsForState(status);
        if (checklistItemsList != null) {
            for (String name : checklistItemsList) {
                CheckListItem item = new CheckListItem();
                item.setName(name);
                item.setValue("false");
                checkListItems.add(item);
            }
        }
        lcData.put("items", checkListItems);
    } catch (PersistenceException e) {
        throw new APIManagementException("Error while parsing the lifecycle ", e);
    }
    // First letter capital
    status = status.substring(0, 1).toUpperCase() + status.substring(1).toLowerCase();
    lcData.put(APIConstants.LC_STATUS, status);
    return lcData;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CheckListItem(org.wso2.carbon.governance.custom.lifecycles.checklist.util.CheckListItem) ArrayList(java.util.ArrayList) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) JSONObject(org.json.simple.JSONObject)

Example 24 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIProviderImpl method setAPIStateWorkflowDTOParameters.

/**
 * Set API or API Product state change workflow parameters
 *
 * @param currentStatus Current state of the API or API Product
 * @param action        LC state change action
 * @param name          Name of the API or API Product
 * @param context       Context of the API or API Product
 * @param apiType       API or API Product
 * @param version       Version of API or API Product
 * @param providerName  Owner of the API or API Product
 * @param apiOrApiProductId Unique ID of the API or API Product
 * @param uuid              Unique UUID of the API or API Product
 * @param gatewayVendor     Gateway Vendor
 * @param workflowType      Workflow Type
 * @param apiStateWFExecutor    WorkflowExecutor
 * @return APIStateWorkflowDTO Object
 */
private APIStateWorkflowDTO setAPIStateWorkflowDTOParameters(String currentStatus, String action, String name, String context, String apiType, String version, String providerName, int apiOrApiProductId, String uuid, String gatewayVendor, String workflowType, WorkflowExecutor apiStateWFExecutor) {
    WorkflowProperties workflowProperties = getAPIManagerConfiguration().getWorkflowProperties();
    APIStateWorkflowDTO stateWorkflowDTO = new APIStateWorkflowDTO();
    stateWorkflowDTO.setApiCurrentState(currentStatus);
    stateWorkflowDTO.setApiLCAction(action);
    stateWorkflowDTO.setApiName(name);
    stateWorkflowDTO.setApiContext(context);
    stateWorkflowDTO.setApiType(apiType);
    stateWorkflowDTO.setApiVersion(version);
    stateWorkflowDTO.setApiProvider(providerName);
    stateWorkflowDTO.setGatewayVendor(gatewayVendor);
    stateWorkflowDTO.setCallbackUrl(workflowProperties.getWorkflowCallbackAPI());
    stateWorkflowDTO.setExternalWorkflowReference(apiStateWFExecutor.generateUUID());
    stateWorkflowDTO.setTenantId(tenantId);
    stateWorkflowDTO.setTenantDomain(this.tenantDomain);
    stateWorkflowDTO.setWorkflowType(workflowType);
    stateWorkflowDTO.setStatus(WorkflowStatus.CREATED);
    stateWorkflowDTO.setCreatedTime(System.currentTimeMillis());
    stateWorkflowDTO.setWorkflowReference(Integer.toString(apiOrApiProductId));
    stateWorkflowDTO.setInvoker(this.username);
    stateWorkflowDTO.setApiUUID(uuid);
    String workflowDescription = "Pending lifecycle state change action: " + action;
    stateWorkflowDTO.setWorkflowDescription(workflowDescription);
    return stateWorkflowDTO;
}
Also used : APIStateWorkflowDTO(org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties)

Example 25 with LifeCycle

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.

the class APIProviderImpl method getAPILifeCycleStatus.

@Override
public String getAPILifeCycleStatus(APIIdentifier apiIdentifier) throws APIManagementException {
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
        GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
        if (apiArtifact == null) {
            String errorMessage = "API artifact is null when retrieving lifecycle status of API " + apiIdentifier.getApiName();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        return apiArtifact.getLifecycleState();
    } catch (GovernanceException e) {
        handleException("Failed to get the life cycle status : " + e.getMessage(), e);
        return null;
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException)

Aggregations

Test (org.testng.annotations.Test)20 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)20 API (org.wso2.carbon.apimgt.core.models.API)20 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)11 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)10 LifecycleException (org.wso2.carbon.lcm.core.exception.LifecycleException)10 JSONObject (org.json.simple.JSONObject)8 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)7 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7 List (java.util.List)6 IOException (java.io.IOException)5 Map (java.util.Map)5 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)4