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<>())));
}
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")))));
}
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)));
}
}
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;
}
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();
}
}
Aggregations