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;
}
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;
}
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());
}
}
}
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;
}
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);
}
}
}
Aggregations