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(); }
@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(); }
private void updateSnapshotNodeRoles() { // Compute new auto role data and updates existing auto data with it NetworkId networkId = _settings.getContainer(); SnapshotId snapshotId = _settings.getTestrig(); NodeRolesId snapshotNodeRolesId = _idResolver.getSnapshotNodeRolesId(networkId, snapshotId); Set<String> nodeNames = loadConfigurations().keySet(); Topology rawLayer3Topology = _topologyProvider.getRawLayer3Topology(getNetworkSnapshot()); SortedSet<NodeRoleDimension> autoRoles = new InferRoles(nodeNames, rawLayer3Topology).inferRoles(); NodeRolesData.Builder snapshotNodeRoles = NodeRolesData.builder(); try { if (!autoRoles.isEmpty()) { snapshotNodeRoles.setDefaultDimension(autoRoles.first().getName()); snapshotNodeRoles.setRoleDimensions(autoRoles); } _storage.storeNodeRoles(snapshotNodeRoles.build(), snapshotNodeRolesId); } catch (IOException e) { _logger.warnf("Could not update node roles: %s", e); } }
@Test public void testPutNodeRolesSuccess() throws IOException { String network = "someContainer"; Main.getWorkMgr().initNetwork(network, null); NodeRolesData nodeRolesData = NodeRolesData.builder() .setDefaultDimension("a") .setRoleDimensions( ImmutableSortedSet.of(NodeRoleDimension.builder().setName("a").build())) .build(); NodeRolesDataBean nodeRolesDataBean = new NodeRolesDataBean(nodeRolesData, null, ImmutableSet.of()); Response response = getNodeRolesTarget(network) .put(Entity.entity(nodeRolesDataBean, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat(Main.getWorkMgr().getNetworkNodeRoles(network), equalTo(nodeRolesData)); } }
@Test public void testGetNodeRolesPresent() throws IOException { String network = "someContainer"; Main.getWorkMgr().initNetwork(network, null); NodeRolesData nodeRolesData = NodeRolesData.builder() .setDefaultDimension("a") .setRoleDimensions( ImmutableSortedSet.of(NodeRoleDimension.builder().setName("a").build())) .build(); Main.getWorkMgr().putNetworkNodeRoles(nodeRolesData, network); Response response = getNodeRolesTarget(network).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( response.readEntity(NodeRolesDataBean.class).toNodeRolesData(), equalTo(nodeRolesData)); }