Search in sources :

Example 6 with Container

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

the class WorkMgrTest method getEmptyContainer.

@Test
public void getEmptyContainer() {
    _manager.initContainer("container", null);
    Container container = _manager.getContainer("container");
    assertThat(container, equalTo(Container.of("container", new TreeSet<>())));
}
Also used : Container(org.batfish.common.Container) Test(org.junit.Test)

Example 7 with Container

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

the class WorkMgrTest method getNonEmptyContainer.

@Test
public void getNonEmptyContainer() {
    _manager.initContainer("container", null);
    Path containerDir = Main.getSettings().getContainersLocation().resolve("container").toAbsolutePath();
    Path testrigPath = containerDir.resolve(BfConsts.RELPATH_TESTRIGS_DIR).resolve("testrig");
    assertThat(testrigPath.toFile().mkdirs(), is(true));
    Container container = _manager.getContainer("container");
    assertThat(container, equalTo(Container.of("container", Sets.newTreeSet(Collections.singleton("testrig")))));
}
Also used : Path(java.nio.file.Path) Container(org.batfish.common.Container) Test(org.junit.Test)

Example 8 with Container

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

the class WorkMgr method getConfiguration.

/**
 * Returns a string representation of the content of configuration file {@code configName}.
 *
 * @throws BatfishException if the configuration file {@code configName} does not exist, or there
 *     are more than one file with name {@code configName}, or failed to read content from the
 *     file.
 */
public String getConfiguration(String containerName, String testrigName, String configName) {
    Path testrigPath = getdirTestrig(containerName, testrigName);
    try (Stream<Path> paths = Files.walk(testrigPath.resolve(BfConsts.RELPATH_TEST_RIG_DIR))) {
        List<Path> configPaths = paths.filter(x -> x.getFileName().toString().equals(configName)).collect(Collectors.toList());
        if (configPaths.isEmpty()) {
            throw new BatfishException(String.format("Configuration file %s does not exist in testrig %s for container %s", configName, testrigName, containerName));
        } else if (configPaths.size() > 1) {
            throw new BatfishException(String.format("More than one configuration file with name %s in testrig %s for container %s", configName, testrigName, containerName));
        }
        String configContent = "";
        try {
            configContent = new String(Files.readAllBytes(configPaths.get(0)));
        } catch (IOException e) {
            throw new BatfishException(String.format("Failed to read configuration file %s in testrig %s for container %s", configName, testrigName, containerName), e);
        }
        return configContent;
    } catch (IOException e) {
        throw new BatfishException(String.format("Failed to list directory %s", testrigPath.resolve(BfConsts.RELPATH_TEST_RIG_DIR)));
    }
}
Also used : Path(java.nio.file.Path) SortedSet(java.util.SortedSet) AnalysisType(org.batfish.coordinator.AnalysisMetadataMgr.AnalysisType) BfConsts(org.batfish.common.BfConsts) MediaType(javax.ws.rs.core.MediaType) TestrigMetadata(org.batfish.datamodel.TestrigMetadata) References(io.opentracing.References) Answer(org.batfish.datamodel.answers.Answer) Map(java.util.Map) Pair(org.batfish.common.Pair) Path(java.nio.file.Path) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) TaskStatus(org.batfish.common.BfConsts.TaskStatus) Set(java.util.Set) Settings(org.batfish.coordinator.config.Settings) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Task(org.batfish.common.Task) Executors(java.util.concurrent.Executors) UriComponent(org.glassfish.jersey.uri.UriComponent) List(java.util.List) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) Warnings(org.batfish.common.Warnings) WorkItemBuilder(org.batfish.common.util.WorkItemBuilder) Question(org.batfish.datamodel.questions.Question) Entry(java.util.Map.Entry) ProcessingException(javax.ws.rs.ProcessingException) WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) SortedMap(java.util.SortedMap) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) BatfishLogger(org.batfish.common.BatfishLogger) CommonUtil(org.batfish.common.util.CommonUtil) Client(javax.ws.rs.client.Client) WorkType(org.batfish.coordinator.WorkDetails.WorkType) BatfishException(org.batfish.common.BatfishException) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AnalysisMetadata(org.batfish.datamodel.AnalysisMetadata) BatfishObjectMapper(org.batfish.common.util.BatfishObjectMapper) AbstractCoordinator(org.batfish.common.plugin.AbstractCoordinator) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ZipUtility(org.batfish.common.util.ZipUtility) WorkItem(org.batfish.common.WorkItem) Files(java.nio.file.Files) JSONObject(org.codehaus.jettison.json.JSONObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) GlobalTracer(io.opentracing.util.GlobalTracer) IOException(java.io.IOException) JSONArray(org.codehaus.jettison.json.JSONArray) AnswerStatus(org.batfish.datamodel.answers.AnswerStatus) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) JSONException(org.codehaus.jettison.json.JSONException) ActiveSpan(io.opentracing.ActiveSpan) WebTarget(javax.ws.rs.client.WebTarget) Container(org.batfish.common.Container) Comparator(java.util.Comparator) QueueType(org.batfish.coordinator.WorkQueueMgr.QueueType) UnzipUtility(org.batfish.common.util.UnzipUtility) InputStream(java.io.InputStream) BatfishException(org.batfish.common.BatfishException) IOException(java.io.IOException)

Example 9 with Container

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

the class WorkMgr method listAnalyses.

/**
 * Returns the Analysis names which exist in the container and match the {@link AnalysisType}
 *
 * @param containerName Container name
 * @param analysisType {@link AnalysisType} requested
 * @return {@link Set} of container names
 */
public SortedSet<String> listAnalyses(String containerName, AnalysisType analysisType) {
    Path containerDir = getdirContainer(containerName);
    Path analysesDir = containerDir.resolve(BfConsts.RELPATH_ANALYSES_DIR);
    if (!Files.exists(analysesDir)) {
        return ImmutableSortedSet.of();
    }
    SortedSet<String> analyses = CommonUtil.getSubdirectories(analysesDir).stream().map(subdir -> subdir.getFileName().toString()).filter(aName -> selectAnalysis(aName, analysisType, containerName)).collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder()));
    return analyses;
}
Also used : Path(java.nio.file.Path) SortedSet(java.util.SortedSet) AnalysisType(org.batfish.coordinator.AnalysisMetadataMgr.AnalysisType) BfConsts(org.batfish.common.BfConsts) MediaType(javax.ws.rs.core.MediaType) TestrigMetadata(org.batfish.datamodel.TestrigMetadata) References(io.opentracing.References) Answer(org.batfish.datamodel.answers.Answer) Map(java.util.Map) Pair(org.batfish.common.Pair) Path(java.nio.file.Path) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) TaskStatus(org.batfish.common.BfConsts.TaskStatus) Set(java.util.Set) Settings(org.batfish.coordinator.config.Settings) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Task(org.batfish.common.Task) Executors(java.util.concurrent.Executors) UriComponent(org.glassfish.jersey.uri.UriComponent) List(java.util.List) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) Warnings(org.batfish.common.Warnings) WorkItemBuilder(org.batfish.common.util.WorkItemBuilder) Question(org.batfish.datamodel.questions.Question) Entry(java.util.Map.Entry) ProcessingException(javax.ws.rs.ProcessingException) WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) SortedMap(java.util.SortedMap) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) BatfishLogger(org.batfish.common.BatfishLogger) CommonUtil(org.batfish.common.util.CommonUtil) Client(javax.ws.rs.client.Client) WorkType(org.batfish.coordinator.WorkDetails.WorkType) BatfishException(org.batfish.common.BatfishException) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AnalysisMetadata(org.batfish.datamodel.AnalysisMetadata) BatfishObjectMapper(org.batfish.common.util.BatfishObjectMapper) AbstractCoordinator(org.batfish.common.plugin.AbstractCoordinator) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ZipUtility(org.batfish.common.util.ZipUtility) WorkItem(org.batfish.common.WorkItem) Files(java.nio.file.Files) JSONObject(org.codehaus.jettison.json.JSONObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) GlobalTracer(io.opentracing.util.GlobalTracer) IOException(java.io.IOException) JSONArray(org.codehaus.jettison.json.JSONArray) AnswerStatus(org.batfish.datamodel.answers.AnswerStatus) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) JSONException(org.codehaus.jettison.json.JSONException) ActiveSpan(io.opentracing.ActiveSpan) WebTarget(javax.ws.rs.client.WebTarget) Container(org.batfish.common.Container) Comparator(java.util.Comparator) QueueType(org.batfish.coordinator.WorkQueueMgr.QueueType) UnzipUtility(org.batfish.common.util.UnzipUtility) InputStream(java.io.InputStream)

Example 10 with Container

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

the class WorkMgrService method getContainer.

/**
 * Get information of the container
 *
 * @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
 * @return A {@link Response Response} with an entity consists either a json representation of the
 *     container {@code containerName} or an error message if: the container {@code containerName}
 *     does not exist or the {@code apiKey} has no acess to the container {@code containerName}
 */
@POST
@Path(CoordConsts.SVC_RSC_GET_CONTAINER)
@Produces(MediaType.APPLICATION_JSON)
public Response getContainer(@FormDataParam(CoordConsts.SVC_KEY_API_KEY) String apiKey, @FormDataParam(CoordConsts.SVC_KEY_VERSION) String clientVersion, @FormDataParam(CoordConsts.SVC_KEY_CONTAINER_NAME) String containerName) {
    try {
        _logger.infof("WMS:getContainer %s\n", containerName);
        checkStringParam(apiKey, "API key");
        checkStringParam(clientVersion, "Client version");
        checkStringParam(containerName, "Container name");
        checkApiKeyValidity(apiKey);
        checkClientVersion(clientVersion);
        java.nio.file.Path containerDir = Main.getSettings().getContainersLocation().resolve(containerName).toAbsolutePath();
        if (containerDir == null || !Files.exists(containerDir)) {
            return Response.status(Response.Status.NOT_FOUND).entity("Container '" + containerName + "' not found").type(MediaType.TEXT_PLAIN).build();
        }
        checkContainerAccessibility(apiKey, containerName);
        Container container = Main.getWorkMgr().getContainer(containerDir);
        String containerString = BatfishObjectMapper.writeString(container);
        return Response.ok(containerString).build();
    } catch (AccessControlException e) {
        return Response.status(Status.FORBIDDEN).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
    } catch (BatfishException e) {
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
    } catch (Exception e) {
        String stackTrace = ExceptionUtils.getStackTrace(e);
        _logger.errorf("WMS:getContainer exception: %s", stackTrace);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getCause()).type(MediaType.TEXT_PLAIN).build();
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) Container(org.batfish.common.Container) AccessControlException(java.security.AccessControlException) 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

Container (org.batfish.common.Container)12 Response (javax.ws.rs.core.Response)7 Path (java.nio.file.Path)5 BatfishException (org.batfish.common.BatfishException)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 Nullable (javax.annotation.Nullable)4 ProcessingException (javax.ws.rs.ProcessingException)4 WebTarget (javax.ws.rs.client.WebTarget)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)3 ActiveSpan (io.opentracing.ActiveSpan)3 References (io.opentracing.References)3 SpanContext (io.opentracing.SpanContext)3 GlobalTracer (io.opentracing.util.GlobalTracer)3 InputStream (java.io.InputStream)3 Files (java.nio.file.Files)3 Paths (java.nio.file.Paths)3 Instant (java.time.Instant)3