Fix odlparent-3.0.0 checkstyle issues
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / modification / LazyDataTreeModification.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.codec.modification;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map.Entry;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.mdsal.binding.javav2.api.DataTreeIdentifier;
18 import org.opendaylight.mdsal.binding.javav2.api.DataTreeModification;
19 import org.opendaylight.mdsal.binding.javav2.api.TreeNodeModification;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingTreeNodeCodec;
21 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingToNormalizedNodeCodec;
22 import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
23 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27
28 /**
29  * Lazily translated {@link DataTreeModification} based on {@link DataTreeCandidate}.
30  *
31  * <p>
32  * {@link DataTreeModification} represents Data tree change event, but whole tree is not translated or
33  * resolved eagerly, but only child nodes which are directly accessed by user of tree node modification.
34  *
35  */
36 @Beta
37 public final class LazyDataTreeModification<T extends TreeNode> implements DataTreeModification<T> {
38
39     private final DataTreeIdentifier<T> path;
40     private final TreeNodeModification<T> rootNode;
41
42     private LazyDataTreeModification(final DataTreeIdentifier<T> path, final TreeNodeModification<T> modification) {
43         this.path = Preconditions.checkNotNull(path);
44         this.rootNode = Preconditions.checkNotNull(modification);
45     }
46
47     @Nonnull
48     @Override
49     public TreeNodeModification<T> getRootNode() {
50         return rootNode;
51     }
52
53     @Nonnull
54     @Override
55     public DataTreeIdentifier<T> getRootPath() {
56         return path;
57     }
58
59     @SuppressWarnings({ "unchecked", "rawtypes" })
60     private static <T extends TreeNode> DataTreeModification<T> create(final BindingToNormalizedNodeCodec codec,
61             final DataTreeCandidate domChange, final LogicalDatastoreType datastoreType) {
62         final Entry<InstanceIdentifier<?>, BindingTreeNodeCodec<?>> codecCtx =
63                 codec.getSubtreeCodec(domChange.getRootPath());
64         final DataTreeIdentifier<?> path = DataTreeIdentifier.create(datastoreType, codecCtx.getKey());
65         final TreeNodeModification<?> modification =
66                 LazyTreeNodeModification.create(codecCtx.getValue(), domChange.getRootNode());
67         return new LazyDataTreeModification(path, modification);
68     }
69
70     /**
71      * Create instance of Binding date tree modification according to DOM candidate of changes.
72      *
73      * @param codec
74      *            - codec for modificated data
75      * @param candidate
76      *            - changted DOM data
77      * @return modificated data tree
78      */
79     @SuppressWarnings({ "unchecked", "rawtypes" })
80     public static <T extends TreeNode> DataTreeModification<T> create(final BindingToNormalizedNodeCodec codec,
81             final DOMDataTreeCandidate candidate) {
82         final Entry<InstanceIdentifier<?>, BindingTreeNodeCodec<?>> codecCtx =
83                 codec.getSubtreeCodec(candidate.getRootPath().getRootIdentifier());
84         final DataTreeIdentifier<?> path =
85                 DataTreeIdentifier.create(candidate.getRootPath().getDatastoreType(), codecCtx.getKey());
86         final TreeNodeModification<?> modification =
87                 LazyTreeNodeModification.create(codecCtx.getValue(), candidate.getRootNode());
88         return new LazyDataTreeModification(path, modification);
89     }
90
91     /**
92      * DOM data changes to new Binding data.
93      *
94      * @param codec
95      *            - Binding to DOM codec
96      * @param domChanges
97      *            - DOM data changes
98      * @param datastoreType
99      *            - datastore type
100      * @return collection of new Binding data according to DOM data changes
101      */
102     public static <T extends TreeNode> Collection<DataTreeModification<T>> from(
103             final BindingToNormalizedNodeCodec codec, final Collection<DataTreeCandidate> domChanges,
104             final LogicalDatastoreType datastoreType) {
105         final List<DataTreeModification<T>> result = new ArrayList<>(domChanges.size());
106         for (final DataTreeCandidate domChange : domChanges) {
107             result.add(LazyDataTreeModification.create(codec, domChange, datastoreType));
108         }
109         return result;
110     }
111
112 }
113