0ec5aa7154cbfd3224583c4cd2fd01c00947141b
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / 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.controller.md.sal.binding.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Map.Entry;
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22
23 /**
24  * Lazily translated {@link DataTreeModification} based on {@link DataTreeCandidate}.
25  *
26  * {@link DataTreeModification} represents Data tree change event,
27  * but whole tree is not translated or resolved eagerly, but only child nodes
28  * which are directly accessed by user of data object modification.
29  *
30  */
31 class LazyDataTreeModification<T extends DataObject> implements DataTreeModification<T> {
32
33     private final DataTreeIdentifier<T> path;
34     private final DataObjectModification<T> rootNode;
35
36     LazyDataTreeModification(final LogicalDatastoreType datastoreType, final InstanceIdentifier<T> path,
37             final BindingDataObjectCodecTreeNode<T> codec, final DataTreeCandidate domChange) {
38         this.path = new DataTreeIdentifier<>(datastoreType, path);
39         this.rootNode = LazyDataObjectModification.create(codec, domChange.getRootNode());
40     }
41
42     @Override
43     public DataObjectModification<T> getRootNode() {
44         return rootNode;
45     }
46
47     @Override
48     public DataTreeIdentifier<T> getRootPath() {
49         return path;
50     }
51
52     @SuppressWarnings({"unchecked", "rawtypes"})
53     static <T extends DataObject> DataTreeModification<T> create(final BindingToNormalizedNodeCodec codec,
54             final DataTreeCandidate domChange, final LogicalDatastoreType datastoreType) {
55         final Entry<InstanceIdentifier<?>, BindingDataObjectCodecTreeNode<?>> codecCtx =
56                 codec.getSubtreeCodec(domChange.getRootPath());
57         return new LazyDataTreeModification(datastoreType, codecCtx.getKey(), codecCtx.getValue(), domChange);
58     }
59
60     static <T extends DataObject> Collection<DataTreeModification<T>> from(final BindingToNormalizedNodeCodec codec,
61             final Collection<DataTreeCandidate> domChanges, final LogicalDatastoreType datastoreType) {
62         final List<DataTreeModification<T>> result = new ArrayList<>(domChanges.size());
63         for (final DataTreeCandidate domChange : domChanges) {
64             result.add(LazyDataTreeModification.<T>create(codec, domChange, datastoreType));
65         }
66         return result;
67     }
68
69     @Override
70     public String toString() {
71         return getClass().getSimpleName() + "{path = " + path + ", rootNode = " + rootNode + "}";
72     }
73 }