Fix some checkstyle and other issues
[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.binding.dom.codec.api.BindingCodecTreeNode;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
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  * <p>
29  * {@link DataTreeModification} represents Data tree change event,
30  * but whole tree is not translated or resolved eagerly, but only child nodes
31  * which are directly accessed by user of data object modification.
32  *
33  */
34 class LazyDataTreeModification<T extends DataObject> implements DataTreeModification<T> {
35
36     private final DataTreeIdentifier<T> path;
37     private final DataObjectModification<T> rootNode;
38
39     LazyDataTreeModification(DataTreeIdentifier<T> path, final DataObjectModification<T> modification) {
40         this.path = Preconditions.checkNotNull(path);
41         this.rootNode = Preconditions.checkNotNull(modification);
42     }
43
44     @Override
45     public DataObjectModification<T> getRootNode() {
46         return rootNode;
47     }
48
49     @Override
50     public DataTreeIdentifier<T> getRootPath() {
51         return path;
52     }
53
54     @SuppressWarnings({"unchecked", "rawtypes"})
55     static <T extends DataObject> DataTreeModification<T> create(final BindingToNormalizedNodeCodec codec,
56             final DataTreeCandidate domChange, final LogicalDatastoreType datastoreType) {
57         final Entry<InstanceIdentifier<?>, BindingCodecTreeNode<?>> codecCtx =
58                 codec.getSubtreeCodec(domChange.getRootPath());
59         final DataTreeIdentifier<?> path = DataTreeIdentifier.create(datastoreType, codecCtx.getKey());
60         final DataObjectModification<?> modification =
61                 LazyDataObjectModification.create(codecCtx.getValue(), domChange.getRootNode());
62         return new LazyDataTreeModification(path, modification);
63     }
64
65     @SuppressWarnings({"unchecked", "rawtypes"})
66     static <T extends DataObject> DataTreeModification<T> create(BindingToNormalizedNodeCodec codec,
67             DOMDataTreeCandidate candidate) {
68         final Entry<InstanceIdentifier<?>, BindingCodecTreeNode<?>> codecCtx =
69                 codec.getSubtreeCodec(candidate.getRootPath().getRootIdentifier());
70         final DataTreeIdentifier<?> path =
71                 DataTreeIdentifier.create(candidate.getRootPath().getDatastoreType(), codecCtx.getKey());
72         final DataObjectModification<?> modification =
73                 LazyDataObjectModification.create(codecCtx.getValue(), candidate.getRootNode());
74         return new LazyDataTreeModification(path, modification);
75     }
76
77     static <T extends DataObject> Collection<DataTreeModification<T>> from(final BindingToNormalizedNodeCodec codec,
78             final Collection<DataTreeCandidate> domChanges, final LogicalDatastoreType datastoreType) {
79         final List<DataTreeModification<T>> result = new ArrayList<>(domChanges.size());
80         for (final DataTreeCandidate domChange : domChanges) {
81             result.add(LazyDataTreeModification.create(codec, domChange, datastoreType));
82         }
83         return result;
84     }
85
86 }