private Optional<DataTreeCandidate> readCurrentData() { final Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY); return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode( YangInstanceIdentifier.EMPTY, currentState.get())) : Optional.<DataTreeCandidate>absent(); }
private static void addToListenerChanges( final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, final YangInstanceIdentifier path, final DataTreeCandidateNode node, final Multimap<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> listenerChanges) { final DataTreeCandidate dataTreeCandidate = DataTreeCandidates.newDataTreeCandidate(path, node); for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) { listenerChanges.put(reg, dataTreeCandidate); } } }
private <L extends DOMDataTreeChangeListener> void initialDataChangeEvent( final YangInstanceIdentifier listenerPath, final L listener) { // FIXME Add support for wildcard listeners final Optional<NormalizedNode<?, ?>> preExistingData = dataTree.takeSnapshot() .readNode(YangInstanceIdentifier.create(stripShardPath(listenerPath))); final DataTreeCandidate initialCandidate; if (preExistingData.isPresent()) { final NormalizedNode<?, ?> data = preExistingData.get(); checkState(data instanceof DataContainerNode, "Expected DataContainer node, but was {}", data.getClass()); // if we are listening on root of some shard we still get // empty normalized node, root is always present if (((DataContainerNode<?>) data).getValue().isEmpty()) { initialCandidate = DataTreeCandidates.newDataTreeCandidate(listenerPath, DataTreeCandidateNodes.empty(data.getIdentifier())); } else { initialCandidate = DataTreeCandidates.fromNormalizedNode(listenerPath, translateRootShardIdentifierToListenerPath(listenerPath, preExistingData.get())); } } else { initialCandidate = DataTreeCandidates.newDataTreeCandidate(listenerPath, DataTreeCandidateNodes.empty(listenerPath.getLastPathArgument())); } listener.onDataTreeChanged(Collections.singleton(initialCandidate)); }
private DataTreeCandidate applyChanges(final Collection<DataTreeCandidate> changes) { final DataTreeModification modification = dataTree.takeSnapshot().newModification(); for (final DataTreeCandidate change : changes) { DataTreeCandidates.applyToModification(modification, change); } modification.ready(); try { dataTree.validate(modification); } catch (final DataValidationFailedException e) { LOG.error("Validation failed for built modification", e); throw new RuntimeException("Notification validation failed", e); } // strip nodes we do not need since this listener doesn't have to be registered at the root of the DataTree DataTreeCandidateNode modifiedChild = dataTree.prepare(modification).getRootNode(); for (final PathArgument pathArgument : listenerPath.getPathArguments()) { modifiedChild = modifiedChild.getModifiedChild(pathArgument); } if (modifiedChild == null) { modifiedChild = DataTreeCandidateNodes.empty(listenerPath.getLastPathArgument()); } return DataTreeCandidates.newDataTreeCandidate(listenerPath, modifiedChild); } }
private void applyRecoveryCandidate(final DataTreeCandidate candidate) throws DataValidationFailedException { final PruningDataTreeModification mod = wrapWithPruning(dataTree.takeSnapshot().newModification()); DataTreeCandidates.applyToModification(mod, candidate); mod.ready(); final DataTreeModification unwrapped = mod.delegate(); LOG.trace("{}: Applying recovery modification {}", logContext, unwrapped); try { dataTree.validate(unwrapped); dataTree.commit(dataTree.prepare(unwrapped)); } catch (Exception e) { File file = new File(System.getProperty("karaf.data", "."), "failed-recovery-payload-" + logContext + ".out"); DataTreeModificationOutput.toFile(file, unwrapped); throw new IllegalStateException(String.format( "%s: Failed to apply recovery payload. Modification data was written to file %s", logContext, file), e); } }
public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) { if (modification instanceof CursorAwareDataTreeModification) { applyToCursorAwareModification((CursorAwareDataTreeModification) modification, candidate); return;
private void applyReplicatedCandidate(final Identifier identifier, final DataTreeCandidate foreign) throws DataValidationFailedException { LOG.debug("{}: Applying foreign transaction {}", logContext, identifier); final DataTreeModification mod = dataTree.takeSnapshot().newModification(); DataTreeCandidates.applyToModification(mod, foreign); mod.ready(); LOG.trace("{}: Applying foreign modification {}", logContext, mod); dataTree.validate(mod); final DataTreeCandidate candidate = dataTree.prepare(mod); dataTree.commit(candidate); notifyListeners(candidate); }
public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) { if (modification instanceof CursorAwareDataTreeModification) { applyToCursorAwareModification((CursorAwareDataTreeModification) modification, candidate); return;
@Override protected void notifyListeners(final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, final YangInstanceIdentifier path, final DataTreeCandidateNode node) { final DataTreeCandidate candidate = DataTreeCandidates.newDataTreeCandidate(path, node); for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) { LOG.debug("Enqueueing candidate {} to registration {}", candidate, registrations); notificationManager.submitNotification(reg, candidate); } }
@Override protected synchronized void appendInitial(final State state) { // We are still starting up, so all we need to do is squash reported changes to an initial write event final DataTreeCandidate last = Iterables.getLast(state.changes); changes.clear(); final Optional<NormalizedNode<?, ?>> lastData = last.getRootNode().getDataAfter(); if (lastData.isPresent()) { changes.add(DataTreeCandidates.fromNormalizedNode(last.getRootPath(), lastData.get())); } }
@Override protected void notifyListeners(final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, final YangInstanceIdentifier path, final DataTreeCandidateNode node) { final DataTreeCandidate candidate = DataTreeCandidates.newDataTreeCandidate(path, node); for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) { LOG.debug("Enqueueing candidate {} to registration {}", candidate, registrations); notificationManager.submitNotification(reg, candidate); } }
<L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) { final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener); final Optional<NormalizedNode<?, ?>> node = snapshot.readNode(treeId); if (node.isPresent()) { final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(treeId, node.get()); notificationManager.submitNotification(reg, candidate); } return reg; }
void onDataTreeChanged(final YangInstanceIdentifier rootPath, final Collection<DataTreeCandidate> changes) { final List<DataTreeCandidate> newCandidates = changes.stream() .map(candidate -> DataTreeCandidates.newDataTreeCandidate(rootPath, candidate.getRootNode())) .collect(Collectors.toList()); delegate.onDataTreeChanged(Collections.singleton(applyChanges(newCandidates))); }
<L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener( final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) { final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener); final Optional<NormalizedNode<?, ?>> node = snapshot.readNode(YangInstanceIdentifier.EMPTY); if (node.isPresent()) { final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode( YangInstanceIdentifier.EMPTY, node.get()); InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher(notificationManager); publisher.registerTreeChangeListener(treeId, listener); publisher.publishChange(candidate); } return reg; }
@Override protected void notifyListeners(final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, final YangInstanceIdentifier path, final DataTreeCandidateNode node) { final Collection<DataTreeCandidate> changes = Collections.<DataTreeCandidate>singleton( DataTreeCandidates.newDataTreeCandidate(path, node)); for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) { reg.getInstance().onDataTreeChanged(changes); } }
final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.EMPTY, data); final InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher( notificationManager);
public static DataTreeCandidate readDataTreeCandidate(final DataInput in) throws IOException { final NormalizedNodeDataInput reader = new NormalizedNodeInputStreamReader(in); final YangInstanceIdentifier rootPath = reader.readYangInstanceIdentifier(); final byte type = reader.readByte(); final DataTreeCandidateNode rootNode; switch (type) { case DELETE: rootNode = DeletedDataTreeCandidateNode.create(); break; case SUBTREE_MODIFIED: rootNode = ModifiedDataTreeCandidateNode.create(readChildren(reader)); break; case WRITE: rootNode = DataTreeCandidateNodes.fromNormalizedNode(reader.readNormalizedNode()); break; default: throw new IllegalArgumentException("Unhandled node type " + type); } return DataTreeCandidates.newDataTreeCandidate(rootPath, rootNode); }