@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(); } }
/** * 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)); }
@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(); }
/** * 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(); }
@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(); } }
@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 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 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())); }