Search in sources :

Example 21 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateAPIStatus.

/**
 * @see org.wso2.carbon.apimgt.core.api.APIPublisher#updateAPIStatus(String, String, Map)
 */
@Override
public WorkflowResponse updateAPIStatus(String apiId, String status, Map<String, Boolean> checkListItemMap) throws APIManagementException {
    WorkflowResponse workflowResponse = null;
    try {
        API api = getApiDAO().getAPI(apiId);
        if (api != null && !APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
            API.APIBuilder apiBuilder = new API.APIBuilder(api);
            apiBuilder.lastUpdatedTime(LocalDateTime.now());
            apiBuilder.updatedBy(getUsername());
            LifecycleState currentState = getApiLifecycleManager().getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus());
            apiBuilder.lifecycleState(currentState);
            for (Map.Entry<String, Boolean> checkListItem : checkListItemMap.entrySet()) {
                getApiLifecycleManager().checkListItemEvent(api.getLifecycleInstanceId(), api.getLifeCycleStatus(), checkListItem.getKey(), checkListItem.getValue());
            }
            API originalAPI = apiBuilder.build();
            WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
            APIStateChangeWorkflow workflow = new APIStateChangeWorkflow(getApiDAO(), getApiSubscriptionDAO(), getWorkflowDAO(), getApiLifecycleManager(), getApiGateway(), getLabelDAO());
            workflow.setApiName(originalAPI.getName());
            workflow.setApiProvider(originalAPI.getProvider());
            workflow.setApiVersion(originalAPI.getVersion());
            workflow.setCurrentState(currentState.getState());
            workflow.setTransitionState(status);
            workflow.setWorkflowReference(originalAPI.getId());
            workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
            workflow.setCreatedTime(LocalDateTime.now());
            workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_API_STATE);
            workflow.setInvoker(getUsername());
            // setting attributes for internal use. These are set to use from outside the executor's method
            // these will be saved in the AM_WORKFLOW table so these can be retrieved later
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_CUR_STATE, currentState.getState());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_TARGET_STATE, status);
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_LC_INVOKER, getUsername());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_NAME, originalAPI.getId());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_HAS_OWN_GATEWAY, String.valueOf(originalAPI.hasOwnGateway()));
            if (originalAPI.hasOwnGateway()) {
                List<String> gwLabels = originalAPI.getLabels();
                for (String label : gwLabels) {
                    if (label.contains(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX)) {
                        workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_AUTOGEN_LABEL, label);
                        break;
                    }
                }
            }
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_HAS_OWN_GATEWAY, String.valueOf(originalAPI.hasOwnGateway()));
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_LAST_UPTIME, originalAPI.getLastUpdatedTime().toString());
            String workflowDescription = "API [" + workflow.getApiName() + " - " + workflow.getApiVersion() + "] state change [" + workflow.getCurrentState() + " to " + workflow.getTransitionState() + "] request from " + getUsername();
            workflow.setWorkflowDescription(workflowDescription);
            workflowResponse = executor.execute(workflow);
            workflow.setStatus(workflowResponse.getWorkflowStatus());
            if (WorkflowStatus.CREATED != workflowResponse.getWorkflowStatus()) {
                completeWorkflow(executor, workflow);
            } else {
                // add entry to workflow table if it is only in pending state
                addWorkflowEntries(workflow);
                getApiDAO().updateAPIWorkflowStatus(api.getId(), APILCWorkflowStatus.PENDING);
            }
        } else if (api != null && APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
            String message = "Pending state transition for api :" + api.getName();
            log.error(message);
            throw new APIManagementException(message, ExceptionCodes.WORKFLOW_PENDING);
        } else {
            throw new APIMgtResourceNotFoundException("Requested API " + apiId + " Not Available");
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't change the status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (LifecycleException e) {
        String errorMsg = "Couldn't change the status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
    }
    return workflowResponse;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) APIStateChangeWorkflow(org.wso2.carbon.apimgt.core.workflow.APIStateChangeWorkflow) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) API(org.wso2.carbon.apimgt.core.models.API) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateDedicatedGateway.

/**
 * @see APIPublisherImpl#updateDedicatedGateway(DedicatedGateway)
 */
@Override
public void updateDedicatedGateway(DedicatedGateway dedicatedGateway) throws APIManagementException {
    String apiId = dedicatedGateway.getApiId();
    API api = getAPIbyUUID(apiId);
    try {
        validateChangingDedicatedGateway(api);
        // label generation for the API
        String autoGenLabelName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + apiId;
        List<String> labelSet = new ArrayList<>();
        if (dedicatedGateway.isEnabled()) {
            Label label = getLabelDAO().getLabelByName(autoGenLabelName);
            if (label == null) {
                // A new label is created with auto generated label name value for per api gateway
                List<Label> labelList = new ArrayList<>();
                List<String> accessUrls = new ArrayList<>();
                accessUrls.add(APIMgtConstants.HTTPS + APIMgtConstants.WEB_PROTOCOL_SUFFIX + autoGenLabelName);
                Label autoGenLabel = new Label.Builder().id(autoGenLabelName).name(autoGenLabelName).accessUrls(accessUrls).type(APIMgtConstants.LABEL_TYPE_GATEWAY).build();
                labelList.add(autoGenLabel);
                getLabelDAO().addLabels(labelList);
                log.debug("New label: {} added.", autoGenLabelName);
            }
            labelSet.add(autoGenLabelName);
            labelSet.add(getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_STORE));
            getApiDAO().updateDedicatedGateway(dedicatedGateway, labelSet);
        } else {
            if (api.hasOwnGateway()) {
                labelSet.add(getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_GATEWAY));
                labelSet.add(getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_STORE));
                getApiDAO().updateDedicatedGateway(dedicatedGateway, labelSet);
            }
        }
    } catch (APIMgtDAOException e) {
        throw new APIManagementException("Error occurred while updating dedicatedGateway details of API with id " + apiId, e, ExceptionCodes.ERROR_WHILE_UPDATING_DEDICATED_CONTAINER_BASED_GATEWAY);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.core.models.Label) API(org.wso2.carbon.apimgt.core.models.API)

Example 23 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class APIStoreImpl method getLabelInfo.

@Override
public List<Label> getLabelInfo(List<String> labels, String username) throws LabelException {
    List<Label> filteredLabels;
    String labelExtractorClassName = getConfig().getLabelExtractorImplClass();
    try {
        List<Label> availableLabels = getLabelDAO().getLabelsByName(labels);
        LabelExtractor labelExtractor = (LabelExtractor) Class.forName(labelExtractorClassName).newInstance();
        filteredLabels = labelExtractor.filterLabels(username, availableLabels);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while retrieving label information";
        log.error(errorMsg, e);
        throw new LabelException(errorMsg, e, ExceptionCodes.LABEL_EXCEPTION);
    } catch (ClassNotFoundException e) {
        String errorMsg = "Error occurred while loading the class [class name] " + labelExtractorClassName;
        log.error(errorMsg, e);
        throw new LabelException(errorMsg, e, ExceptionCodes.LABEL_EXCEPTION);
    } catch (IllegalAccessException | InstantiationException e) {
        String errorMsg = "Error occurred while creating an instance of the class [class name] " + labelExtractorClassName;
        log.error(errorMsg, e);
        throw new LabelException(errorMsg, e, ExceptionCodes.LABEL_EXCEPTION);
    }
    return filteredLabels;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) LabelExtractor(org.wso2.carbon.apimgt.core.api.LabelExtractor) Label(org.wso2.carbon.apimgt.core.models.Label) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException)

Example 24 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class APIStoreImpl method updateDedicatedGateway.

/**
 * @see APIStoreImpl#updateDedicatedGateway(DedicatedGateway)
 */
@Override
public void updateDedicatedGateway(DedicatedGateway dedicatedGateway) throws APIManagementException {
    String apiId = dedicatedGateway.getApiId();
    API api = getAPIbyUUID(apiId);
    try {
        // label generation for the API
        String autoGenLabelName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + apiId;
        List<String> labelSet = new ArrayList<>();
        if (dedicatedGateway.isEnabled()) {
            Label label = getLabelDAO().getLabelByName(autoGenLabelName);
            if (label == null) {
                // A new label is created with auto generated label name value for per api gateway
                List<Label> labelList = new ArrayList<>();
                List<String> accessUrls = new ArrayList<>();
                accessUrls.add(APIMgtConstants.HTTPS + APIMgtConstants.WEB_PROTOCOL_SUFFIX + autoGenLabelName);
                Label autoGenLabel = new Label.Builder().id(UUID.randomUUID().toString()).name(autoGenLabelName).accessUrls(accessUrls).type(APIMgtConstants.LABEL_TYPE_GATEWAY).build();
                labelList.add(autoGenLabel);
                getLabelDAO().addLabels(labelList);
                log.debug("New label: {} added.", autoGenLabelName);
            }
            labelSet.add(autoGenLabelName);
            getApiDAO().updateDedicatedGateway(dedicatedGateway, labelSet);
        } else {
            if (api.hasOwnGateway()) {
                labelSet.add(APIMgtConstants.DEFAULT_LABEL_NAME);
                getApiDAO().updateDedicatedGateway(dedicatedGateway, labelSet);
            }
        }
    } catch (APIMgtDAOException e) {
        throw new APIManagementException("Error occurred while updating dedicatedGateway details of API with id " + apiId, e, ExceptionCodes.ERROR_WHILE_UPDATING_DEDICATED_CONTAINER_BASED_GATEWAY);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.core.models.Label) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API)

Example 25 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class LabelDAOImplIT method testDeleteLabel.

@Test
public void testDeleteLabel() throws Exception {
    LabelDAO labelDAO = DAOFactory.getLabelDAO();
    Label label = SampleTestObjectCreator.createLabel("public", SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    LabelDAOImpl.addLabel(label);
    labelDAO.deleteLabel(label.getId());
    Label labelFromDb = labelDAO.getLabelByID(label.getId());
    Assert.assertNull(labelFromDb);
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) Test(org.testng.annotations.Test)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)52 Test (org.testng.annotations.Test)45 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)32 API (org.wso2.carbon.apimgt.core.models.API)29 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)28 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)20 HashMap (java.util.HashMap)16 Connection (java.sql.Connection)15 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 Test (org.junit.Test)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 Map (java.util.Map)10 BeforeTest (org.testng.annotations.BeforeTest)9 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 Response (javax.ws.rs.core.Response)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8