public @Nonnull NodeRolesData toNodeRolesData() { return NodeRolesData.builder() .setDefaultDimension(defaultDimension) .setRoleDimensions( roleDimensions.stream() .map(NodeRoleDimensionBean::toNodeRoleDimension) .collect(ImmutableSortedSet.toImmutableSortedSet(Ordering.natural()))) .build(); }
@DELETE public Response delNodeRoleDimension() throws IOException { NodeRolesData nodeRolesData = Main.getWorkMgr().getNetworkNodeRoles(_network); if (nodeRolesData == null) { return Response.status(Status.NOT_FOUND).build(); } Optional<NodeRoleDimension> dimension = nodeRolesData.getNodeRoleDimension(_dimension); if (!dimension.isPresent()) { return Response.status(Status.NOT_FOUND).build(); } if (!Main.getWorkMgr() .putNetworkNodeRoles( NodeRolesData.builder() .setDefaultDimension(nodeRolesData.getDefaultDimension()) .setRoleDimensions( nodeRolesData.getNodeRoleDimensions().stream() .filter(dim -> !dim.getName().equalsIgnoreCase(dimension.get().getName())) .collect( ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder()))) .build(), _network)) { // if network was deleted while we were working return Response.status(Status.NOT_FOUND).build(); } return Response.ok().build(); }
public @Nonnull NodeRolesData build() { return new NodeRolesData(_defaultDimension, _roleDimensions); }
/** * Get some "reasonable" {@link NodeRoleDimension} object for analysis. Preference order: the * default dimension if set and exists, the auto-inferred primary dimension if it exists, the * dimension that is lexicographically first, and null if no dimensions exist. * * @throws IOException If the contents of the file could not be cast to {@link NodeRolesData} */ @Nonnull private Optional<NodeRoleDimension> getNodeRoleDimension() throws IOException { // check default if (getDefaultDimension() != null) { Optional<NodeRoleDimension> opt = getNodeRoleDimension(getDefaultDimension()); if (opt.isPresent()) { return opt; } } // check auto primary Optional<NodeRoleDimension> optAuto = getNodeRoleDimension(NodeRoleDimension.AUTO_DIMENSION_PRIMARY); if (optAuto.isPresent()) { return optAuto; } // check first return getNodeRoleDimensions().stream().min(Comparator.comparing(NodeRoleDimension::getName)); }
public NodeRolesDataBean( @Nonnull NodeRolesData nodeRolesData, @Nullable String snapshot, @Nonnull Set<String> nodes) { defaultDimension = nodeRolesData.getDefaultDimension(); roleDimensions = nodeRolesData.getNodeRoleDimensions().stream() .map(dim -> new NodeRoleDimensionBean(dim, snapshot, nodes)) .collect(Collectors.toSet()); }
@GET public Response getNodeRoleDimension() throws IOException { NodeRolesData nodeRolesData = Main.getWorkMgr().getNetworkNodeRoles(_network); if (nodeRolesData == null) { return Response.status(Status.NOT_FOUND).build(); } Optional<NodeRoleDimension> dimension = nodeRolesData.getNodeRoleDimension(_dimension); if (!dimension.isPresent()) { return Response.status(Status.NOT_FOUND).build(); } return Response.ok() .entity(new NodeRoleDimensionBean(dimension.get(), null, ImmutableSet.of())) .build(); } }
@Test public void testDelNodeRoleDimensionSuccess() throws IOException { String network = "network1"; String dimension = "dimension1"; Main.getWorkMgr().initNetwork(network, null); NodeRoleDimension nodeRoleDimension = NodeRoleDimension.builder().setName(dimension).build(); Main.getWorkMgr() .putNetworkNodeRoles( NodeRolesData.builder() .setRoleDimensions(ImmutableSortedSet.of(nodeRoleDimension)) .build(), network); Response response = getNodeRoleDimensionTarget(network, dimension).delete(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( Main.getWorkMgr().getNetworkNodeRoles(network).getNodeRoleDimension(dimension).isPresent(), equalTo(false)); // deleting again should fail Response response2 = getNodeRoleDimensionTarget(network, dimension).delete(); assertThat(response2.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
String roleDimension = parts[1]; Optional<NodeRoleDimension> optDimension = nodeRoleData.getNodeRoleDimensions().stream() .filter(dim -> dim.getName().equals(roleDimension)) .findAny(); String roleDimPrefix = finalQuery.equalsIgnoreCase("ROLE:") ? "" : parts[1]; suggestions.addAll( nodeRoleData.getNodeRoleDimensions().stream() .filter(dim -> dim.getName().startsWith(roleDimPrefix)) .map(
/** * Get the {@link NodeRoleDimension} object for the specified dimension. If dimension is null, * returns {@link #getNodeRoleDimension()}. * * @param dimension The name of the dimension to fetch * @return The {@link NodeRoleDimension} object if one exists or throws {@link * java.util.NoSuchElementException} if {@code dimension} is non-null and not found. * @throws IOException If the contents of the file could not be cast to {@link NodeRolesData} */ public @Nonnull Optional<NodeRoleDimension> getNodeRoleDimension(@Nullable String dimension) throws IOException { if (dimension == null) { return getNodeRoleDimension(); } return _roleDimensions.stream() .filter(d -> d.getName().equalsIgnoreCase(dimension)) .findFirst(); }
PropertySpecifier.baseAutoComplete( query, getNetworkNodeRoles(network).getNodeRoleDimensions().stream() .map(NodeRoleDimension::getName) .collect(Collectors.toSet()));
/** * Reads the {@link NodeRolesData} object for the provided network. If none previously set for * this network, first initialize node roles to the inferred roles of the earliest completed * snapshot if one exists, and return them. If no suitable snapshot exists, return empty node * roles. Returns {@code null} if network does not exist. * * @throws IOException If there is an error */ public @Nullable NodeRolesData getNetworkNodeRoles(@Nonnull String network) throws IOException { if (!_idManager.hasNetworkId(network)) { return null; } NetworkId networkId = _idManager.getNetworkId(network); if (!_idManager.hasNetworkNodeRolesId(networkId)) { initializeNetworkNodeRolesFromEarliestSnapshot(network); } if (!_idManager.hasNetworkNodeRolesId(networkId)) { return NodeRolesData.builder().build(); } NodeRolesId networkNodeRolesId = _idManager.getNetworkNodeRolesId(networkId); try { return BatfishObjectMapper.mapper() .readValue(_storage.loadNodeRoles(networkNodeRolesId), NodeRolesData.class); } catch (IOException e) { throw new IOException("Failed to read network node roles", e); } }
@POST public Response addNodeRoleDimension(NodeRoleDimensionBean dimBean) throws IOException { checkClientArgument(dimBean.name != null, "Node role dimension must have a name"); NodeRolesData nodeRolesData = Main.getWorkMgr().getNetworkNodeRoles(_network); if (nodeRolesData == null) { return Response.status(Status.NOT_FOUND).build(); } Optional<NodeRoleDimension> dimension = nodeRolesData.getNodeRoleDimension(dimBean.name); checkClientArgument(!dimension.isPresent(), "Duplicate dimension specified: %s", dimBean.name); if (!Main.getWorkMgr() .putNetworkNodeRoles( NodeRolesData.builder() .setDefaultDimension(nodeRolesData.getDefaultDimension()) .setRoleDimensions( ImmutableSortedSet.<NodeRoleDimension>naturalOrder() .addAll(nodeRolesData.getNodeRoleDimensions()) .add(dimBean.toNodeRoleDimension()) .build()) .build(), _network)) { // if network was deleted while we were working return Response.status(Status.NOT_FOUND).build(); } return Response.ok().build(); }
/** * Gets the {@link NodeRoleDimension} object given dimension name. If {@code dimension} is null, * returns the default dimension. * * @param dimension The dimension name * @return An {@link Optional} that has the requested NodeRoleDimension or empty otherwise. */ @Override public Optional<NodeRoleDimension> getNodeRoleDimension(@Nullable String dimension) { try { NodeRolesData nodeRolesData = getNodeRolesData(); return nodeRolesData.getNodeRoleDimension(dimension); } catch (IOException e) { _logger.errorf("Could not read roles data: %s", e); return Optional.empty(); } }
@JsonCreator private static @Nonnull NodeRolesData create( @JsonProperty(PROP_DEFAULT_DIMENSION) @Nullable String defaultDimension, @JsonProperty(PROP_ROLE_DIMENSIONS) @Nullable Set<NodeRoleDimension> roleDimensions) { return new NodeRolesData( defaultDimension, ImmutableSortedSet.copyOf(firstNonNull(roleDimensions, ImmutableSortedSet.of()))); }
@Test public void testPutNodeRolesMissingNetwork() { String network = "someContainer"; NodeRolesDataBean nodeRolesDataBean = new NodeRolesDataBean(NodeRolesData.builder().build(), null, ImmutableSet.of()); Response response = getNodeRolesTarget(network) .put(Entity.entity(nodeRolesDataBean, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
@Path("/{dimension}") @Produces(MediaType.APPLICATION_JSON) @GET public Response getSnapshotNodeRolesDimension(@PathParam("dimension") String dimension) throws IOException { NodeRolesData data = _inferred ? Main.getWorkMgr().getSnapshotNodeRoles(_network, _snapshot) : Main.getWorkMgr().getNetworkNodeRoles(_network); Optional<NodeRoleDimension> nodeRolesDimension = data.getNodeRoleDimension(dimension); if (!nodeRolesDimension.isPresent()) { return Response.status(Status.NOT_FOUND).build(); } return Response.ok() .entity( new NodeRoleDimensionBean( nodeRolesDimension.get(), _snapshot, Main.getWorkMgr().getNodes(_network, _snapshot))) .build(); } }
@Test public void testGetNodeRolesDefault() throws IOException { String network = "someContainer"; Main.getWorkMgr().initNetwork(network, null); NodeRolesData nodeRolesData = NodeRolesData.builder().build(); Response response = getNodeRolesTarget(network).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( response.readEntity(NodeRolesDataBean.class).toNodeRolesData(), equalTo(nodeRolesData)); }
@Test public void testAddNodeRoleDimension() throws IOException { String container = "someContainer"; Main.getWorkMgr().initNetwork(container, null); NodeRoleDimensionBean dimBean = new NodeRoleDimensionBean("dimension1", null, null, null); Response response = getNodeRolesTarget(container).post(Entity.entity(dimBean, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); NodeRolesData nrData = Main.getWorkMgr().getNetworkNodeRoles(container); assertThat(nrData.getNodeRoleDimension("dimension1").isPresent(), equalTo(true)); Response response2 = getNodeRolesTarget(container).post(Entity.entity(dimBean, MediaType.APPLICATION_JSON)); assertThat(response2.getStatus(), equalTo(BAD_REQUEST.getStatusCode())); }
@Test public void testNetworkGetPresent() throws IOException { String network = "network1"; String snapshot = "snapshot1"; Main.getWorkMgr().initNetwork(network, null); Set<String> nodes = ImmutableSet.of(); WorkMgrTestUtils.initSnapshotWithTopology(network, snapshot, nodes); NodeRolesData nodeRolesData = NodeRolesData.builder().build(); Response response = getNodeRolesTarget(network, snapshot, false).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( response.readEntity(NodeRolesDataBean.class), equalTo(new NodeRolesDataBean(nodeRolesData, snapshot, nodes))); } }
@Test public void testGetNodeRoleDimensionMissingDimension() throws IOException { String network = "network1"; String dimension = "dimension1"; Main.getWorkMgr().initNetwork(network, null); Main.getWorkMgr().putNetworkNodeRoles(NodeRolesData.builder().build(), network); Response response = getNodeRoleDimensionTarget(network, dimension).get(); assertThat(response.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }