Search in sources :

Example 6 with WorkItem

use of org.batfish.common.WorkItem in project batfish by batfish.

the class WorkItemBuilder method getWorkItemGenerateDataPlane.

public static WorkItem getWorkItemGenerateDataPlane(String containerName, String testrigName, String envName) {
    WorkItem wItem = new WorkItem(containerName, testrigName);
    wItem.addRequestParam(BfConsts.COMMAND_DUMP_DP, "");
    wItem.addRequestParam(BfConsts.ARG_ENVIRONMENT_NAME, envName);
    return wItem;
}
Also used : WorkItem(org.batfish.common.WorkItem)

Example 7 with WorkItem

use of org.batfish.common.WorkItem in project batfish by batfish.

the class WorkMgr method getAutoWorkQueue.

List<WorkItem> getAutoWorkQueue(String containerName, String testrigName) {
    List<WorkItem> autoWorkQueue = new LinkedList<>();
    WorkItem parseWork = WorkItemBuilder.getWorkItemParse(containerName, testrigName);
    autoWorkQueue.add(parseWork);
    Set<String> analysisNames = listAnalyses(containerName, AnalysisType.ALL);
    for (String analysis : analysisNames) {
        WorkItem analyzeWork = WorkItemBuilder.getWorkItemRunAnalysis(analysis, containerName, testrigName, BfConsts.RELPATH_DEFAULT_ENVIRONMENT_NAME, null, null, false, false);
        autoWorkQueue.add(analyzeWork);
    }
    return autoWorkQueue;
}
Also used : WorkItem(org.batfish.common.WorkItem) LinkedList(java.util.LinkedList)

Example 8 with WorkItem

use of org.batfish.common.WorkItem in project batfish by batfish.

the class WorkMgrService method getAnswer.

/**
 * Get answer to a previously asked question
 *
 * @param apiKey The API key of the client
 * @param clientVersion The version of the client
 * @param containerName The name of the container in which the question was asked
 * @param testrigName The name of the testrig on which the question was asked
 * @param baseEnv The name of the base environment on which the question was asked
 * @param deltaTestrig The name of the delta testrig on which the question was asked
 * @param deltaEnv The name of the delta environment on which the question was asked
 * @param questionName The name of the question
 * @return TODO: document JSON response
 */
@POST
@Path(CoordConsts.SVC_RSC_GET_ANSWER)
@Produces(MediaType.APPLICATION_JSON)
public JSONArray getAnswer(@FormDataParam(CoordConsts.SVC_KEY_API_KEY) String apiKey, @FormDataParam(CoordConsts.SVC_KEY_VERSION) String clientVersion, @FormDataParam(CoordConsts.SVC_KEY_CONTAINER_NAME) String containerName, @FormDataParam(CoordConsts.SVC_KEY_TESTRIG_NAME) String testrigName, @FormDataParam(CoordConsts.SVC_KEY_ENV_NAME) String baseEnv, @FormDataParam(CoordConsts.SVC_KEY_DELTA_TESTRIG_NAME) String deltaTestrig, @FormDataParam(CoordConsts.SVC_KEY_DELTA_ENV_NAME) String deltaEnv, @FormDataParam(CoordConsts.SVC_KEY_QUESTION_NAME) String questionName, @FormDataParam(CoordConsts.SVC_KEY_WORKITEM) String workItemStr) /* optional */
{
    try {
        _logger.infof("WMS:getAnswer %s %s %s %s\n", apiKey, containerName, testrigName, questionName);
        checkStringParam(apiKey, "API key");
        checkStringParam(clientVersion, "Client version");
        checkStringParam(containerName, "Container name");
        checkStringParam(testrigName, "Base testrig name");
        checkStringParam(baseEnv, "Base environment name");
        checkStringParam(questionName, "Question name");
        checkApiKeyValidity(apiKey);
        checkClientVersion(clientVersion);
        checkContainerAccessibility(apiKey, containerName);
        if (!Strings.isNullOrEmpty(workItemStr)) {
            WorkItem workItem = BatfishObjectMapper.mapper().readValue(workItemStr, WorkItem.class);
            if (!workItem.getContainerName().equals(containerName) || !workItem.getTestrigName().equals(testrigName)) {
                return failureResponse("Mismatch in parameters: WorkItem is not for the supplied container or testrig");
            }
            QueuedWork work = Main.getWorkMgr().getMatchingWork(workItem, QueueType.INCOMPLETE);
            if (work != null) {
                String taskStr = BatfishObjectMapper.writePrettyString(work.getLastTaskCheckResult());
                return successResponse(new JSONObject().put(CoordConsts.SVC_KEY_WORKID, work.getWorkItem().getId()).put(CoordConsts.SVC_KEY_WORKSTATUS, work.getStatus().toString()).put(CoordConsts.SVC_KEY_TASKSTATUS, taskStr));
            }
        }
        String answer = Main.getWorkMgr().getAnswer(containerName, testrigName, baseEnv, deltaTestrig, deltaEnv, questionName);
        return successResponse(new JSONObject().put(CoordConsts.SVC_KEY_ANSWER, answer));
    } catch (IllegalArgumentException | AccessControlException e) {
        _logger.errorf("WMS:getAnswer exception: %s\n", e.getMessage());
        return failureResponse(e.getMessage());
    } catch (Exception e) {
        String stackTrace = ExceptionUtils.getStackTrace(e);
        _logger.errorf("WMS:getAnswer exception: %s", stackTrace);
        return failureResponse(e.getMessage());
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) AccessControlException(java.security.AccessControlException) WorkItem(org.batfish.common.WorkItem) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileExistsException(org.apache.commons.io.FileExistsException) AccessControlException(java.security.AccessControlException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 9 with WorkItem

use of org.batfish.common.WorkItem in project batfish by batfish.

the class WorkMgrService method getAnalysisAnswers.

/**
 * Get answers for a previously run analysis
 *
 * @param apiKey The API key of the client
 * @param clientVersion The version of the client
 * @param containerName The name of the container in which the analysis resides
 * @param testrigName The name of the testrig on which the analysis was run
 * @param baseEnv The name of the base environment on which the analysis was run
 * @param deltaTestrig The name of the delta testrig on which the analysis was run
 * @param deltaEnv The name of the delta environment on which the analysis was run
 * @param analysisName The name of the analysis
 * @return TODO: document JSON response
 */
@POST
@Path(CoordConsts.SVC_RSC_GET_ANALYSIS_ANSWERS)
@Produces(MediaType.APPLICATION_JSON)
public JSONArray getAnalysisAnswers(@FormDataParam(CoordConsts.SVC_KEY_API_KEY) String apiKey, @FormDataParam(CoordConsts.SVC_KEY_VERSION) String clientVersion, @FormDataParam(CoordConsts.SVC_KEY_CONTAINER_NAME) String containerName, @FormDataParam(CoordConsts.SVC_KEY_TESTRIG_NAME) String testrigName, @FormDataParam(CoordConsts.SVC_KEY_ENV_NAME) String baseEnv, @FormDataParam(CoordConsts.SVC_KEY_DELTA_TESTRIG_NAME) String deltaTestrig, @FormDataParam(CoordConsts.SVC_KEY_DELTA_ENV_NAME) String deltaEnv, @FormDataParam(CoordConsts.SVC_KEY_ANALYSIS_NAME) String analysisName, @FormDataParam(CoordConsts.SVC_KEY_WORKITEM) String workItemStr) /* optional */
{
    try {
        _logger.infof("WMS:getAnalysisAnswers %s %s %s %s\n", apiKey, containerName, testrigName, analysisName);
        checkStringParam(apiKey, "API key");
        checkStringParam(clientVersion, "Client version");
        checkStringParam(containerName, "Container name");
        checkStringParam(testrigName, "Base testrig name");
        checkStringParam(baseEnv, "Base environment name");
        checkStringParam(analysisName, "Analysis name");
        checkApiKeyValidity(apiKey);
        checkClientVersion(clientVersion);
        checkContainerAccessibility(apiKey, containerName);
        JSONObject response = new JSONObject();
        if (!Strings.isNullOrEmpty(workItemStr)) {
            WorkItem workItem = BatfishObjectMapper.mapper().readValue(workItemStr, WorkItem.class);
            if (!workItem.getContainerName().equals(containerName) || !workItem.getTestrigName().equals(testrigName)) {
                return failureResponse("Mismatch in parameters: WorkItem is not for the supplied container or testrig");
            }
            QueuedWork work = Main.getWorkMgr().getMatchingWork(workItem, QueueType.INCOMPLETE);
            if (work != null) {
                String taskStr = BatfishObjectMapper.writePrettyString(work.getLastTaskCheckResult());
                response.put(CoordConsts.SVC_KEY_WORKID, work.getWorkItem().getId()).put(CoordConsts.SVC_KEY_WORKSTATUS, work.getStatus().toString()).put(CoordConsts.SVC_KEY_TASKSTATUS, taskStr);
            }
        }
        Map<String, String> answers = Main.getWorkMgr().getAnalysisAnswers(containerName, testrigName, baseEnv, deltaTestrig, deltaEnv, analysisName);
        String answersStr = BatfishObjectMapper.writePrettyString(answers);
        return successResponse(response.put(CoordConsts.SVC_KEY_ANSWERS, answersStr));
    } catch (IllegalArgumentException | AccessControlException e) {
        _logger.errorf("WMS:getAnalysisAnswers exception: %s\n", e.getMessage());
        return failureResponse(e.getMessage());
    } catch (Exception e) {
        String stackTrace = ExceptionUtils.getStackTrace(e);
        _logger.errorf("WMS:getAnalsysisAnswers exception: %s", stackTrace);
        return failureResponse(e.getMessage());
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) AccessControlException(java.security.AccessControlException) WorkItem(org.batfish.common.WorkItem) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileExistsException(org.apache.commons.io.FileExistsException) AccessControlException(java.security.AccessControlException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 10 with WorkItem

use of org.batfish.common.WorkItem in project batfish by batfish.

the class WorkMgrService method queueWork.

/**
 * Queue a new work item
 *
 * @param apiKey The API key of the client
 * @param clientVersion The version of the client
 * @param workItemStr The work item to queue
 * @return TODO: document JSON response
 */
@POST
@Path(CoordConsts.SVC_RSC_QUEUE_WORK)
@Produces(MediaType.APPLICATION_JSON)
public JSONArray queueWork(@FormDataParam(CoordConsts.SVC_KEY_API_KEY) String apiKey, @FormDataParam(CoordConsts.SVC_KEY_VERSION) String clientVersion, @FormDataParam(CoordConsts.SVC_KEY_WORKITEM) String workItemStr) {
    try {
        _logger.infof("WMS:queueWork %s %s\n", apiKey, workItemStr);
        checkStringParam(apiKey, "API key");
        checkStringParam(clientVersion, "Client version");
        checkStringParam(workItemStr, "Workitem string");
        checkApiKeyValidity(apiKey);
        checkClientVersion(clientVersion);
        WorkItem workItem = BatfishObjectMapper.mapper().readValue(workItemStr, WorkItem.class);
        checkContainerAccessibility(apiKey, workItem.getContainerName());
        QueuedWork work = Main.getWorkMgr().getMatchingWork(workItem, QueueType.INCOMPLETE);
        if (work != null) {
            return failureResponse(new JSONObject().put("Duplicate workId", work.getId()));
        }
        boolean result = Main.getWorkMgr().queueWork(workItem);
        return successResponse(new JSONObject().put("result", result));
    } catch (IllegalArgumentException | AccessControlException e) {
        _logger.errorf("WMS:queueWork exception: %s\n", e.getMessage());
        return failureResponse(e.getMessage());
    } catch (Exception e) {
        String stackTrace = ExceptionUtils.getStackTrace(e);
        _logger.errorf("WMS:queueWork exception: %s", stackTrace);
        return failureResponse(e.getMessage());
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) AccessControlException(java.security.AccessControlException) WorkItem(org.batfish.common.WorkItem) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileExistsException(org.apache.commons.io.FileExistsException) AccessControlException(java.security.AccessControlException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

WorkItem (org.batfish.common.WorkItem)44 Test (org.junit.Test)15 BatfishException (org.batfish.common.BatfishException)14 IOException (java.io.IOException)6 EnvironmentMetadata (org.batfish.datamodel.EnvironmentMetadata)5 JSONObject (org.codehaus.jettison.json.JSONObject)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 FileNotFoundException (java.io.FileNotFoundException)3 Path (java.nio.file.Path)3 AccessControlException (java.security.AccessControlException)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 FileExistsException (org.apache.commons.io.FileExistsException)3 JSONException (org.codehaus.jettison.json.JSONException)3 LinkedList (java.util.LinkedList)2 Task (org.batfish.common.Task)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayList (java.util.ArrayList)1