+ /**
+ * Extract the instance identifier of removed instances of {@code clazz} from the given set of modifications.
+ *
+ * @param changes The changes to process.
+ * @param clazz The class we're interested in.
+ * @param <T> The type of changes we're interested in.
+ * @param <U> The type of changes to process.
+ * @return The instance identifiers of removed instances.
+ */
+ public static <T extends DataObject, U extends DataObject> Set<InstanceIdentifier<T>> extractRemoved(
+ Collection<DataTreeModification<U>> changes, Class<T> clazz) {
+ return extractDataObjectModifications(changes, clazz, modificationIsDeletion()).keySet();
+ }
+
+ /**
+ * Extract all the modifications affecting instances of {@code clazz} which are present in the given set of
+ * modifications and satisfy the given filter.
+ *
+ * @param changes The changes to process.
+ * @param clazz The class we're interested in.
+ * @param filter The filter the changes must satisfy.
+ * @param <T> The type of changes we're interested in.
+ * @param <U> The type of changes to process.
+ * @return The modifications, mapped by instance identifier.
+ */
+ private static <T extends DataObject, U extends DataObject> Map<InstanceIdentifier<T>, DataObjectModification<T>>
+ extractDataObjectModifications(Collection<DataTreeModification<U>> changes, Class<T> clazz,
+ Predicate<DataObjectModification<T>> filter) {
+ List<DataObjectModification<? extends DataObject>> dataObjectModifications = new ArrayList<>();
+ List<InstanceIdentifier<? extends DataObject>> paths = new ArrayList<>();
+ if (changes != null) {
+ for (DataTreeModification<? extends DataObject> change : changes) {
+ dataObjectModifications.add(change.getRootNode());
+ paths.add(change.getRootPath().getRootIdentifier());
+ }
+ }
+ return extractDataObjectModifications(dataObjectModifications, paths, clazz, filter);
+ }
+
+ /**
+ * Extract all the modifications affecting instances of {@code clazz} which are present in the given set of
+ * modifications and satisfy the given filter.
+ *
+ * @param changes The changes to process.
+ * @param paths The paths of the changes.
+ * @param clazz The class we're interested in.
+ * @param filter The filter the changes must satisfy.
+ * @param <T> The type of changes we're interested in.
+ * @return The modifications, mapped by instance identifier.
+ */
+ private static <T extends DataObject> Map<InstanceIdentifier<T>, DataObjectModification<T>>
+ extractDataObjectModifications(
+ Collection<DataObjectModification<? extends DataObject>> changes,
+ Collection<InstanceIdentifier<? extends DataObject>> paths, Class<T> clazz,
+ Predicate<DataObjectModification<T>> filter) {
+ Map<InstanceIdentifier<T>, DataObjectModification<T>> result = new HashMap<>();
+ Queue<DataObjectModification<? extends DataObject>> remainingChanges = new LinkedList<>(changes);
+ Queue<InstanceIdentifier<? extends DataObject>> remainingPaths = new LinkedList<>(paths);
+ while (!remainingChanges.isEmpty()) {
+ DataObjectModification<? extends DataObject> change = remainingChanges.remove();
+ InstanceIdentifier<? extends DataObject> path = remainingPaths.remove();
+ // Is the change relevant?
+ if (clazz.isAssignableFrom(change.getDataType()) && filter.apply((DataObjectModification<T>) change)) {
+ result.put((InstanceIdentifier<T>) path, (DataObjectModification<T>) change);
+ }
+ // Add any children to the queue
+ for (DataObjectModification<? extends DataObject> child : change.getModifiedChildren()) {
+ remainingChanges.add(child);
+ remainingPaths.add(extendPath(path, child));
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Extends the given instance identifier path to include the given child. Augmentations are treated in the same way
+ * as children; keyed children are handled correctly.
+ *
+ * @param path The current path.
+ * @param child The child modification to include.
+ * @return The extended path.
+ */
+ private static <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>, T extends DataObject>
+ InstanceIdentifier<? extends DataObject> extendPath(
+ InstanceIdentifier path,
+ DataObjectModification child) {
+ Class<N> item = (Class<N>) child.getDataType();
+ if (child.getIdentifier() instanceof InstanceIdentifier.IdentifiableItem) {
+ K key = (K) ((InstanceIdentifier.IdentifiableItem) child.getIdentifier()).getKey();
+ KeyedInstanceIdentifier<N, K> extendedPath = path.child(item, key);
+ return extendedPath;
+ } else {
+ InstanceIdentifier<N> extendedPath = path.child(item);
+ return extendedPath;
+ }
+ }
+