Search in sources :

Example 1 with NodeRoleSpecifier

use of org.batfish.datamodel.NodeRoleSpecifier in project batfish by batfish.

the class Batfish method getNodeRoleSpecifier.

/* Gets the NodeRoleSpecifier that specifies the roles for each node.
     If inferred is true, it returns the inferred roles;
     otherwise it prefers the user-specified roles if they exist.
  */
@Override
public NodeRoleSpecifier getNodeRoleSpecifier(boolean inferred) {
    NodeRoleSpecifier result;
    boolean inferredRoles = false;
    TestrigSettings settings = _settings.getActiveTestrigSettings();
    Path nodeRolesPath = settings.getNodeRolesPath();
    if (!Files.exists(nodeRolesPath) || inferred) {
        inferredRoles = true;
        nodeRolesPath = settings.getInferredNodeRolesPath();
        if (!Files.exists(nodeRolesPath)) {
            return new NodeRoleSpecifier();
        }
    }
    result = parseNodeRoles(nodeRolesPath);
    result.setInferred(inferredRoles);
    return result;
}
Also used : Path(java.nio.file.Path) TestrigSettings(org.batfish.config.Settings.TestrigSettings) NodeRoleSpecifier(org.batfish.datamodel.NodeRoleSpecifier)

Example 2 with NodeRoleSpecifier

use of org.batfish.datamodel.NodeRoleSpecifier in project batfish by batfish.

the class Batfish method parseNodeRoles.

private NodeRoleSpecifier parseNodeRoles(Path nodeRolesPath) {
    _logger.infof("Parsing: \"%s\"\n", nodeRolesPath.toAbsolutePath());
    String roleFileText = CommonUtil.readFile(nodeRolesPath);
    NodeRoleSpecifier specifier;
    try {
        specifier = BatfishObjectMapper.mapper().readValue(roleFileText, new TypeReference<NodeRoleSpecifier>() {
        });
    } catch (IOException e) {
        throw new BatfishException("Failed to parse node roles", e);
    }
    return specifier;
}
Also used : CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) NodeRoleSpecifier(org.batfish.datamodel.NodeRoleSpecifier)

Example 3 with NodeRoleSpecifier

use of org.batfish.datamodel.NodeRoleSpecifier in project batfish by batfish.

the class Batfish method processNodeRoles.

/**
 * Set the roles of each configuration. Use an explicitly provided {@link NodeRoleSpecifier} if
 * one exists; otherwise use the results of our node-role inference. Also set the inferred role
 * dimensions of each node, based on its name.
 */
private void processNodeRoles(Map<String, Configuration> configurations, ValidateEnvironmentAnswerElement veae) {
    NodeRoleSpecifier specifier = getNodeRoleSpecifier(false);
    SortedMap<String, SortedSet<String>> nodeRoles = specifier.createNodeRolesMap(configurations.keySet());
    for (Entry<String, SortedSet<String>> nodeRolesEntry : nodeRoles.entrySet()) {
        String hostname = nodeRolesEntry.getKey();
        Configuration config = configurations.get(hostname);
        if (config == null) {
            veae.setValid(false);
            veae.getUndefinedNodeRoleSpecifierNodes().add(hostname);
        } else {
            SortedSet<String> roles = nodeRolesEntry.getValue();
            config.setRoles(roles);
        }
    }
    Map<String, NavigableMap<Integer, String>> roleDimensions = InferRoles.getRoleDimensions(configurations);
    for (Map.Entry<String, NavigableMap<Integer, String>> entry : roleDimensions.entrySet()) {
        String nodeName = entry.getKey();
        Configuration config = configurations.get(nodeName);
        if (config == null) {
            veae.setValid(false);
        } else {
            config.setRoleDimensions(entry.getValue());
        }
    }
}
Also used : HostConfiguration(org.batfish.representation.host.HostConfiguration) Configuration(org.batfish.datamodel.Configuration) ImmutableConfiguration(org.apache.commons.configuration2.ImmutableConfiguration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) NavigableMap(java.util.NavigableMap) NodeRoleSpecifier(org.batfish.datamodel.NodeRoleSpecifier) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) SortedSet(java.util.SortedSet) Map(java.util.Map) TreeMap(java.util.TreeMap) Collectors.toMap(java.util.stream.Collectors.toMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap)

Example 4 with NodeRoleSpecifier

use of org.batfish.datamodel.NodeRoleSpecifier in project batfish by batfish.

the class Batfish method serializeIndependentConfigs.

private Answer serializeIndependentConfigs(Path vendorConfigPath) {
    Answer answer = new Answer();
    ConvertConfigurationAnswerElement answerElement = new ConvertConfigurationAnswerElement();
    answerElement.setVersion(Version.getVersion());
    if (_settings.getVerboseParse()) {
        answer.addAnswerElement(answerElement);
    }
    Map<String, Configuration> configurations = getConfigurations(vendorConfigPath, answerElement);
    Topology testrigTopology = computeTestrigTopology(_testrigSettings.getTestRigPath(), configurations);
    serializeAsJson(_testrigSettings.getTopologyPath(), testrigTopology, "testrig topology");
    checkTopology(configurations, testrigTopology);
    org.batfish.datamodel.pojo.Topology pojoTopology = org.batfish.datamodel.pojo.Topology.create(_testrigSettings.getName(), configurations, testrigTopology);
    serializeAsJson(_testrigSettings.getPojoTopologyPath(), pojoTopology, "testrig pojo topology");
    _storage.storeConfigurations(configurations, answerElement, _testrigSettings.getName());
    applyEnvironment(configurations);
    Topology envTopology = computeEnvironmentTopology(configurations);
    serializeAsJson(_testrigSettings.getEnvironmentSettings().getSerializedTopologyPath(), envTopology, "environment topology");
    NodeRoleSpecifier roleSpecifier = inferNodeRoles(configurations);
    serializeAsJson(_testrigSettings.getInferredNodeRolesPath(), roleSpecifier, "inferred node roles");
    return answer;
}
Also used : Answer(org.batfish.datamodel.answers.Answer) HostConfiguration(org.batfish.representation.host.HostConfiguration) Configuration(org.batfish.datamodel.Configuration) ImmutableConfiguration(org.apache.commons.configuration2.ImmutableConfiguration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) ConvertConfigurationAnswerElement(org.batfish.datamodel.answers.ConvertConfigurationAnswerElement) Topology(org.batfish.datamodel.Topology) NodeRoleSpecifier(org.batfish.datamodel.NodeRoleSpecifier)

Example 5 with NodeRoleSpecifier

use of org.batfish.datamodel.NodeRoleSpecifier in project batfish by batfish.

the class InferRoles method createRoleDimensions.

private void createRoleDimensions(List<List<String>> regexes) {
    for (String node : _matchingNodes) {
        _roleDimensions.put(node, new TreeMap<>());
    }
    for (int i = 0; i < regexes.size(); i++) {
        NodeRoleSpecifier specifier = regexToRoleSpecifier(regexTokensToRegex(regexes.get(i)));
        SortedMap<String, SortedSet<String>> nodeRolesMap = specifier.createNodeRolesMap(new TreeSet<>(_matchingNodes));
        for (Map.Entry<String, SortedSet<String>> entry : nodeRolesMap.entrySet()) {
            String nodeName = entry.getKey();
            String roleName = entry.getValue().first();
            _roleDimensions.get(nodeName).put(i, roleName);
        }
    }
}
Also used : NodeRoleSpecifier(org.batfish.datamodel.NodeRoleSpecifier) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

NodeRoleSpecifier (org.batfish.datamodel.NodeRoleSpecifier)7 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 NavigableMap (java.util.NavigableMap)2 SortedMap (java.util.SortedMap)2 SortedSet (java.util.SortedSet)2 TreeMap (java.util.TreeMap)2 ImmutableConfiguration (org.apache.commons.configuration2.ImmutableConfiguration)2 Configuration (org.batfish.datamodel.Configuration)2 AwsConfiguration (org.batfish.representation.aws.AwsConfiguration)2 HostConfiguration (org.batfish.representation.host.HostConfiguration)2 IptablesVendorConfiguration (org.batfish.representation.iptables.IptablesVendorConfiguration)2 VendorConfiguration (org.batfish.vendor.VendorConfiguration)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1