Search in sources :

Example 1 with Container

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

the class BfCoordWorkHelper method getContainer.

/**
 * Returns a {@link Container Container} that contains information of '{@code containerName}',
 * returns null if container '{@code containerName}' does not exist or the api key that is using
 * has no access to the container
 */
@Nullable
public Container getContainer(String containerName) {
    try {
        WebTarget webTarget = getTargetV2(Lists.newArrayList(CoordConsts.SVC_KEY_CONTAINERS, containerName));
        Response response = webTarget.request(MediaType.APPLICATION_JSON).header(CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey()).header(CoordConsts.SVC_KEY_VERSION, Version.getVersion()).get();
        _logger.debug(response.getStatus() + " " + response.getStatusInfo() + " " + response + "\n");
        if (response.getStatus() != Response.Status.OK.getStatusCode()) {
            _logger.error("GetContainer: Did not get the expected response\n");
            _logger.error(response.readEntity(String.class) + "\n");
            return null;
        }
        String containerStr = response.readEntity(String.class);
        Container container = BatfishObjectMapper.mapper().readValue(containerStr, Container.class);
        return container;
    } catch (Exception e) {
        _logger.errorf("Exception in getContainer from %s for %s\n", _coordWorkMgrV2, containerName);
        _logger.error(ExceptionUtils.getStackTrace(e) + "\n");
        return null;
    }
}
Also used : Response(javax.ws.rs.core.Response) Container(org.batfish.common.Container) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Nullable(javax.annotation.Nullable)

Example 2 with Container

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

the class WorkMgr method listContainers.

public SortedSet<String> listContainers(String apiKey) {
    Path containersDir = Main.getSettings().getContainersLocation();
    if (!Files.exists(containersDir)) {
        containersDir.toFile().mkdirs();
    }
    SortedSet<String> authorizedContainers = new TreeSet<>(CommonUtil.getSubdirectories(containersDir).stream().map(dir -> dir.getFileName().toString()).filter(container -> Main.getAuthorizer().isAccessibleContainer(apiKey, container, false)).collect(Collectors.toSet()));
    return authorizedContainers;
}
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) TreeSet(java.util.TreeSet)

Example 3 with Container

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

the class WorkMgrServiceTest method getNonEmptyContainer.

@Test
public void getNonEmptyContainer() throws Exception {
    initContainerEnvironment();
    Path containerPath = _containersFolder.getRoot().toPath().resolve(_containerName);
    Path testrigPath = containerPath.resolve(BfConsts.RELPATH_TESTRIGS_DIR).resolve("testrig");
    assertThat(testrigPath.toFile().mkdirs(), is(true));
    Response response = _service.getContainer("100", "0.0.0", _containerName);
    Container container = BatfishObjectMapper.mapper().readValue(response.getEntity().toString(), Container.class);
    Container expected = Container.of(_containerName, Sets.newTreeSet(Collections.singleton("testrig")));
    assertThat(container, equalTo(expected));
}
Also used : Path(java.nio.file.Path) Response(javax.ws.rs.core.Response) Container(org.batfish.common.Container) Test(org.junit.Test)

Example 4 with Container

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

the class WorkMgrServiceV2Test method testGetContainer.

@Test
public void testGetContainer() {
    String containerName = "someContainer";
    Main.getWorkMgr().initContainer(containerName, null);
    Response response = getContainersTarget().path(containerName).request().get();
    assertThat(response.getStatus(), equalTo(OK.getStatusCode()));
    assertThat(response.readEntity(new GenericType<Container>() {
    }).getName(), equalTo(containerName));
}
Also used : Response(javax.ws.rs.core.Response) Container(org.batfish.common.Container) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 5 with Container

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

the class WorkMgrServiceV2Test method getContainers.

@Test
public void getContainers() {
    Response response = getContainersTarget().request().get();
    assertThat(response.getStatus(), equalTo(OK.getStatusCode()));
    assertThat(response.readEntity(new GenericType<List<Container>>() {
    }), empty());
    Main.getWorkMgr().initContainer("someContainer", null);
    response = getContainersTarget().request().get();
    assertThat(response.getStatus(), equalTo(OK.getStatusCode()));
    assertThat(response.readEntity(new GenericType<List<Container>>() {
    }), hasSize(1));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Container(org.batfish.common.Container) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

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