7d2a72d58bfa00fa1acd8f2bc24313a3ebf4b743
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / LazyDataTreeModification.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
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;
24
25 /**
26  * Lazily translated {@link DataTreeModification} based on {@link DataTreeCandidate}.
27  *
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.
31  *
32  */
33 class LazyDataTreeModification<T extends DataObject> implements DataTreeModification<T> {
34
35     private final DataTreeIdentifier<T> path;
36     private final DataObjectModification<T> rootNode;
37
38     LazyDataTreeModification(DataTreeIdentifier<T> path, final DataObjectModification<T> modification) {
39         this.path = Preconditions.checkNotNull(path);
40         this.rootNode = Preconditions.checkNotNull(modification);
41     }
42
43     @Override
44     public DataObjectModification<T> getRootNode() {
45         return rootNode;
46     }
47
48     @Override
49     public DataTreeIdentifier<T> getRootPath() {
50         return path;
51     }
52
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);
62     }
63
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));
69         }
70         return result;
71     }
72
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);
82     }
83
84 }