Merge branch 'blueprint' from controller
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / shard / DelegatingWriteCursorStrategy.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.dom.spi.shard;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.collect.ForwardingObject;
13 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17
18 /**
19  * Delegating implementation of a {@link WriteCursorStrategy}.
20  */
21 @Beta
22 public abstract class DelegatingWriteCursorStrategy extends ForwardingObject implements WriteCursorStrategy {
23
24     @Override
25     protected abstract DOMDataTreeWriteCursor delegate();
26
27     /**
28      * Returns strategy to be used on child nodes. Default implementation is to reuse same instance.
29      *
30      * @return Strategy to be used for child nodes.
31      */
32     protected DelegatingWriteCursorStrategy childStrategy() {
33         return this;
34     }
35
36     @Override
37     public WriteCursorStrategy enter(final PathArgument arg) {
38         delegate().enter(arg);
39         return childStrategy();
40     }
41
42     @Override
43     public void delete(final PathArgument arg) {
44         delegate().delete(arg);
45     }
46
47     @Override
48     public void merge(final PathArgument arg, final NormalizedNode<?, ?> data) {
49         delegate().merge(arg, data);
50     }
51
52     @Override
53     public void write(final PathArgument arg, final NormalizedNode<?, ?> data) {
54         delegate().write(arg, data);
55     }
56
57     @Override
58     public void mergeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
59         for (NormalizedNode<?, ?> child : data.getValue()) {
60             delegate().merge(child.getIdentifier(), child);
61         }
62     }
63
64     @Override
65     public void writeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
66         for (NormalizedNode<?, ?> child : data.getValue()) {
67             delegate().write(child.getIdentifier(), child);
68         }
69     }
70
71     /**
72      * Operation performed to exit current logical level, default implementation calls
73      * {@link DOMDataTreeWriteCursor#exit()} on underlaying cursor.
74      *
75      *<p>
76      * Subclasses may override this to customize exit strategy.
77      *
78      */
79     @Override
80     public void exit() {
81         delegate().exit();
82     }
83 }