configurations.put(hostname, config); if (_warningsByHost.containsKey(hostname) && !_warningsByHost.get(hostname).isEmpty()) { answerElement.getWarnings().put(hostname, _warningsByHost.get(hostname)); answerElement.getConvertStatus().put(_name, ConvertStatus.WARNINGS); } else { answerElement.getConvertStatus().put(_name, ConvertStatus.PASSED); answerElement.getDefinedStructures().putAll(_answerElement.getDefinedStructures()); answerElement.getUndefinedReferences().putAll(_answerElement.getUndefinedReferences()); answerElement.getReferencedStructures().putAll(_answerElement.getReferencedStructures()); answerElement.getConvertStatus().put(_name, ConvertStatus.FAILED); answerElement .getErrors() .put(_name, ((BatfishException) _failureCause).getBatfishStackTrace());
@Override public ConvertConfigurationAnswerElement loadConvertConfigurationAnswerElementOrReparse() { ConvertConfigurationAnswerElement ccae = new ConvertConfigurationAnswerElement(); ccae.getConvertStatus().put("n1", ConvertStatus.PASSED); ccae.getConvertStatus().put("n2", ConvertStatus.WARNINGS); ccae.getConvertStatus().put("n3", ConvertStatus.FAILED); return ccae; } }
private boolean cachedConfigsAreCompatible(NetworkId network, SnapshotId snapshot) { try { ConvertConfigurationAnswerElement ccae = loadConvertConfigurationAnswerElement(network, snapshot); return ccae != null && Version.isCompatibleVersion( FileBasedStorage.class.getCanonicalName(), "Old processed configurations", ccae.getVersion()); } catch (BatfishException e) { _logger.warnf( "Unexpected exception caught while deserializing configs for snapshot %s: %s", snapshot, Throwables.getStackTraceAsString(e)); return false; } }
@Override public ConvertConfigurationAnswerElement loadConvertConfigurationAnswerElementOrReparse() { ConvertConfigurationAnswerElement ccae = new ConvertConfigurationAnswerElement(); ccae.setDefinedStructures(BASIC_DEFINED_STRUCTS_MAP); return ccae; }
@Override public ConvertConfigurationAnswerElement loadConvertConfigurationAnswerElementOrReparse() { ConvertConfigurationAnswerElement ccae = new ConvertConfigurationAnswerElement(); ccae.setUndefinedReferences(BASIC_UNDEFINED_REFS_MAP); return ccae; }
@Test public void loadOldConfigurationsReturnsNull() { ConvertConfigurationAnswerElement oldConvertAnswer = new ConvertConfigurationAnswerElement(); oldConvertAnswer.setVersion(INCOMPATIBLE_VERSION); assertThat( "should not be compatible with current code", Version.isCompatibleVersion("current", "old test", oldConvertAnswer.getVersion()), equalTo(false)); NetworkId network = new NetworkId("network"); SnapshotId snapshot = new SnapshotId("snapshot"); Map<String, Configuration> configs = new HashMap<>(); configs.put("node1", new Configuration("node1", ConfigurationFormat.CISCO_IOS)); _storage.storeConfigurations(configs, oldConvertAnswer, network, snapshot); assertThat(_storage.loadConfigurations(network, snapshot), nullValue()); }
public void undefined(StructureType structureType, String name, StructureUsage usage, int line) { addStructureReference( _answerElement.getUndefinedReferences(), structureType, name, usage, line); }
@Override public TableAnswerElement answer() { UnusedStructuresQuestion question = (UnusedStructuresQuestion) _question; // Find all the filenames that produced the queried nodes. This might have false positives if // a file produced multiple nodes, but that was already mis-handled before. Need to rewrite // this question as a TableAnswerElement. Set<String> includeNodes = question.getNodes().getMatchingNodes(_batfish); Multimap<String, String> hostnameFilenameMap = _batfish.loadParseVendorConfigurationAnswerElement().getFileMap(); Set<String> includeFiles = hostnameFilenameMap.entries().stream() .filter(e -> includeNodes.contains(e.getKey())) .map(Entry::getValue) .collect(Collectors.toSet()); Multiset<Row> rows = LinkedHashMultiset.create(); SortedMap<String, SortedMap<String, SortedMap<String, DefinedStructureInfo>>> definedStructures = _batfish.loadConvertConfigurationAnswerElementOrReparse().getDefinedStructures(); definedStructures.entrySet().stream() .filter(e -> includeFiles.contains(e.getKey())) .forEach(e -> rows.addAll(processEntryToRows(e))); TableAnswerElement table = new TableAnswerElement(createMetadata(question)); table.postProcessAnswer(_question, rows); return table; }
public final void setAnswerElement(ConvertConfigurationAnswerElement answerElement) { _answerElement = answerElement; _answerElement.getDefinedStructures().put(getFilename(), _structureDefinitions); _structureReferences.forEach( (structType, byType) -> byType.forEach( (name, byUsage) -> byUsage.forEach( (usage, lines) -> lines.forEach( line -> addStructureReference( _answerElement.getReferencedStructures(), structType, name, usage, line))))); }
@Test public void testConvertStatus() { assertThat(_element.getConvertStatusProp(), anEmptyMap()); _element.getConvertStatus().put("node", ConvertStatus.PASSED); assertThat(_element.getConvertStatusProp(), hasEntry("node", ConvertStatus.PASSED)); }
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); _storage.storeConfigurations( configurations, answerElement, _settings.getContainer(), _testrigSettings.getName()); postProcessSnapshot(configurations); return answer; }
private void mergeConvertAnswer( boolean summary, boolean verboseError, InitInfoAnswerElement answerElement) { ConvertConfigurationAnswerElement convertAnswer = loadConvertConfigurationAnswerElementOrReparse(); mergeInitStepAnswer(answerElement, convertAnswer, summary, verboseError); convertAnswer.getConvertStatus().entrySet().stream() .filter(s -> s.getValue() == ConvertStatus.FAILED) .forEach(s -> answerElement.getParseStatus().put(s.getKey(), ParseStatus.FAILED)); }
@Before public void setUp() { _element = new ConvertConfigurationAnswerElement(); }
@Override public ConvertConfigurationAnswerElement loadConvertConfigurationAnswerElementOrReparse() { ConvertConfigurationAnswerElement ccae = new ConvertConfigurationAnswerElement(); Warnings warnings1 = new Warnings(true, true, true); warnings1.unimplemented("unimplemented1"); Warnings warnings2 = new Warnings(true, true, true); warnings2.redFlag("redflag2"); ccae.setWarnings(ImmutableSortedMap.of("node1", warnings1, "node2", warnings2)); return ccae; } }
@Override protected boolean matchesSafely( ConvertConfigurationAnswerElement item, Description mismatchDescription) { Warnings warnings = item.getWarnings().get(_hostname); if (warnings == null) { mismatchDescription.appendText(String.format("No warnings for host '%s'", _hostname)); return false; } if (warnings.getRedFlagWarnings().stream() .map(Warning::getText) .noneMatch(_subMatcher::matches)) { mismatchDescription.appendText( String.format("No red-flag warnings for host '%s' match", _hostname)); return false; } return true; } }
@Test public void testPrettyPrint() { BatfishException exception = new BatfishException("sample exception"); BatfishStackTrace stackTrace = new BatfishStackTrace(exception); _element.getErrors().put("sampleError", stackTrace); StringBuilder expected = new StringBuilder(); expected.append("Results from converting vendor configurations\n"); expected.append("\n sampleError[Conversion errors]\n"); for (String line : _element.getErrors().get("sampleError").getLineMap()) { expected.append(" " + line + "\n"); } assertThat(_element.prettyPrint(), equalTo(expected.toString())); }
@Test public void testSetErrors() { BatfishException exception = new BatfishException("sample exception"); BatfishStackTrace stackTrace = new BatfishStackTrace(exception); SortedMap<String, BatfishStackTrace> errors = new TreeMap<>(); errors.put("error", stackTrace); _element.setErrors(errors); assertThat(_element.getErrors().get("error"), is(stackTrace)); } }
@Test public void checkEmptyErrors() { assertThat(_element.getErrors().size(), is(0)); }
_batfish .loadConvertConfigurationAnswerElementOrReparse() .getReferencedStructures() .forEach( (filename, value) -> {
@Override public TableAnswerElement answer() { UndefinedReferencesQuestion question = (UndefinedReferencesQuestion) _question; // Find all the filenames that produced the queried nodes. This might have false positives if // a file produced multiple nodes, but that was already mis-handled before. Need to rewrite // this question as a TableAnswerElement. Set<String> includeNodes = question.getNodes().getMatchingNodes(_batfish); Multimap<String, String> hostnameFilenameMap = _batfish.loadParseVendorConfigurationAnswerElement().getFileMap(); Set<String> includeFiles = hostnameFilenameMap.entries().stream() .filter(e -> includeNodes.contains(e.getKey())) .map(Entry::getValue) .collect(Collectors.toSet()); Multiset<Row> rows = LinkedHashMultiset.create(); SortedMap<String, SortedMap<String, SortedMap<String, SortedMap<String, SortedSet<Integer>>>>> undefinedReferences = _batfish.loadConvertConfigurationAnswerElementOrReparse().getUndefinedReferences(); undefinedReferences.entrySet().stream() .filter(e -> includeFiles.contains(e.getKey())) .forEach(e -> rows.addAll(processEntryToRows(e))); TableAnswerElement table = new TableAnswerElement(createMetadata()); table.postProcessAnswer(_question, rows); return table; }