@Override public InferRolesAnswerElement answer() { InferRolesQuestion question = (InferRolesQuestion) _question; InferRolesAnswerElement answerElement = new InferRolesAnswerElement(null, null); // collect relevant nodes in a list. Set<String> nodes = question.getNodeRegex().getMatchingNodes(_batfish); SortedSet<NodeRoleDimension> roleDimensions = new InferRoles(nodes, _batfish.getEnvironmentTopology(), question.getCaseSensitive()) .inferRoles(); answerElement.getRoleDimensions().addAll(roleDimensions); for (NodeRoleDimension dimension : roleDimensions) { SortedMap<String, SortedSet<String>> roleNodesMap = dimension.createRoleNodesMap(nodes); SortedSet<String> matchingNodes = new TreeSet<>(); for (SortedSet<String> nodeSet : roleNodesMap.values()) { matchingNodes.addAll(nodeSet); } answerElement.getMatchingNodesCount().put(dimension.getName(), matchingNodes.size()); } return answerElement; } }
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 inferRolesOnExampleTopology() throws JsonProcessingException { SortedSet<NodeRoleDimension> roles = new InferRoles(EXAMPLE_NODES, EXAMPLE_TOPOLOGY).inferRoles(); assertThat(BatfishObjectMapper.writePrettyString(roles), roles, hasSize(2)); NodeRoleDimension d1 = roles.first(); assertThat( d1.createRoleNodesMap(EXAMPLE_NODES), equalTo( ImmutableMap.of( "border", filterSet(EXAMPLE_NODES, s -> s.toLowerCase().contains("border")), "core", filterSet(EXAMPLE_NODES, s -> s.toLowerCase().contains("core")), "dept", filterSet(EXAMPLE_NODES, s -> s.toLowerCase().contains("dept")), "dist", filterSet(EXAMPLE_NODES, s -> s.toLowerCase().contains("dist"))))); NodeRoleDimension d2 = roles.last(); assertThat( d2.createRoleNodesMap(EXAMPLE_NODES), equalTo( ImmutableMap.of( "as", filterSet(EXAMPLE_NODES, s -> s.toLowerCase().startsWith("as"))))); }
@Test public void inferCaseSensitiveRolesOnExampleTopology() throws JsonProcessingException { SortedSet<NodeRoleDimension> roles = new InferRoles(EXAMPLE_NODES, EXAMPLE_TOPOLOGY, true).inferRoles();
@Test public void inferRolesOnExampleTopology() throws JsonProcessingException { SortedSet<NodeRoleDimension> roles = new InferRoles(EXAMPLE_NODES, EXAMPLE_TOPOLOGY).inferRoles(); assertThat(BatfishObjectMapper.writePrettyString(roles), roles, hasSize(2)); // Note: currently we do not find a "host" role because it does not match the majority // tokenization. If we had as1host1, e.g., we would. NodeRoleDimension d1 = roles.first(); assertThat( d1.createRoleNodesMap(EXAMPLE_NODES), equalTo( ImmutableMap.of( "border", filterSet(EXAMPLE_NODES, s -> s.contains("border")), "core", filterSet(EXAMPLE_NODES, s -> s.contains("core")), "dept", filterSet(EXAMPLE_NODES, s -> s.contains("dept")), "dist", filterSet(EXAMPLE_NODES, s -> s.contains("dist"))))); NodeRoleDimension d2 = roles.last(); assertThat( d2.createRoleNodesMap(EXAMPLE_NODES), equalTo(ImmutableMap.of("as", filterSet(EXAMPLE_NODES, s -> s.startsWith("as"))))); } }