2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map.Entry;
15 import org.opendaylight.mdsal.binding.api.DataObjectModification;
16 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
20 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeNode;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 * Lazily translated {@link DataTreeModification} based on {@link DataTreeCandidate}.
28 * {@link DataTreeModification} represents Data tree change event,
29 * but whole tree is not translated or resolved eagerly, but only child nodes
30 * which are directly accessed by user of data object modification.
33 class LazyDataTreeModification<T extends DataObject> implements DataTreeModification<T> {
35 private final DataTreeIdentifier<T> path;
36 private final DataObjectModification<T> rootNode;
38 LazyDataTreeModification(DataTreeIdentifier<T> path, final DataObjectModification<T> modification) {
39 this.path = Preconditions.checkNotNull(path);
40 this.rootNode = Preconditions.checkNotNull(modification);
44 public DataObjectModification<T> getRootNode() {
49 public DataTreeIdentifier<T> getRootPath() {
53 @SuppressWarnings({"unchecked", "rawtypes"})
54 static <T extends DataObject> DataTreeModification<T> create(final BindingToNormalizedNodeCodec codec, final DataTreeCandidate domChange,
55 final LogicalDatastoreType datastoreType) {
56 final Entry<InstanceIdentifier<?>, BindingCodecTreeNode<?>> codecCtx =
57 codec.getSubtreeCodec(domChange.getRootPath());
58 final DataTreeIdentifier<?> path = DataTreeIdentifier.create(datastoreType, codecCtx.getKey());
59 final DataObjectModification<?> modification =
60 LazyDataObjectModification.create(codecCtx.getValue(), domChange.getRootNode());
61 return new LazyDataTreeModification(path, modification);
64 static <T extends DataObject> Collection<DataTreeModification<T>> from(final BindingToNormalizedNodeCodec codec,
65 final Collection<DataTreeCandidate> domChanges, final LogicalDatastoreType datastoreType) {
66 final List<DataTreeModification<T>> result = new ArrayList<>(domChanges.size());
67 for (final DataTreeCandidate domChange : domChanges) {
68 result.add(LazyDataTreeModification.<T>create(codec, domChange, datastoreType));
73 static <T extends DataObject> DataTreeModification<T> create(BindingToNormalizedNodeCodec codec,
74 DOMDataTreeCandidate candidate) {
75 final Entry<InstanceIdentifier<?>, BindingCodecTreeNode<?>> codecCtx =
76 codec.getSubtreeCodec(candidate.getRootPath().getRootIdentifier());
77 final DataTreeIdentifier<?> path =
78 DataTreeIdentifier.create(candidate.getRootPath().getDatastoreType(), codecCtx.getKey());
79 final DataObjectModification<?> modification =
80 LazyDataObjectModification.create(codecCtx.getValue(), candidate.getRootNode());
81 return new LazyDataTreeModification(path, modification);