Merge "Bug 2538: Remove redundant Augmentation checks and tests"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / util / NetconfBaseOps.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.connect.netconf.util;
10
11 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT;
12 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
13 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DEFAULT_OPERATION_QNAME;
14 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
15 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
16 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_ERROR_OPTION_QNAME;
17 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
18 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
19 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
20 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
21 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_SOURCE_QNAME;
22 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TARGET_QNAME;
23 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
24 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_VALIDATE_QNAME;
25 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.ROLLBACK_ON_ERROR_OPTION;
26 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
27
28 import com.google.common.base.Optional;
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Lists;
31 import com.google.common.util.concurrent.FutureCallback;
32 import com.google.common.util.concurrent.Futures;
33 import com.google.common.util.concurrent.ListenableFuture;
34 import java.util.Collections;
35 import org.opendaylight.controller.sal.core.api.RpcImplementation;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
39 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
40 import org.opendaylight.yangtools.yang.data.api.Node;
41 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
44 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
45 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
46 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
47
48 /**
49  * Provides base operations for netconf e.g. get, get-config, edit-config, (un)lock, commit etc.
50  * According to RFC-6241
51  */
52 public final class NetconfBaseOps {
53
54     private final RpcImplementation rpc;
55
56     public NetconfBaseOps(final RpcImplementation rpc) {
57         this.rpc = rpc;
58     }
59
60     public ListenableFuture<RpcResult<CompositeNode>> lock(final FutureCallback<RpcResult<CompositeNode>> callback, final QName datastore) {
61         Preconditions.checkNotNull(callback);
62         Preconditions.checkNotNull(datastore);
63
64         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_LOCK_QNAME, getLockContent(datastore));
65         Futures.addCallback(future, callback);
66         return future;
67     }
68
69     public ListenableFuture<RpcResult<CompositeNode>> lockCandidate(final FutureCallback<RpcResult<CompositeNode>> callback) {
70         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_LOCK_QNAME, getLockContent(NETCONF_CANDIDATE_QNAME));
71         Futures.addCallback(future, callback);
72         return future;
73     }
74
75
76     public ListenableFuture<RpcResult<CompositeNode>> lockRunning(final FutureCallback<RpcResult<CompositeNode>> callback) {
77         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_LOCK_QNAME, getLockContent(NETCONF_RUNNING_QNAME));
78         Futures.addCallback(future, callback);
79         return future;
80     }
81
82     public ListenableFuture<RpcResult<CompositeNode>> unlock(final FutureCallback<RpcResult<CompositeNode>> callback, final QName datastore) {
83         Preconditions.checkNotNull(callback);
84         Preconditions.checkNotNull(datastore);
85
86         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME, getUnLockContent(datastore));
87         Futures.addCallback(future, callback);
88         return future;
89     }
90
91     public ListenableFuture<RpcResult<CompositeNode>> unlockRunning(final FutureCallback<RpcResult<CompositeNode>> callback) {
92         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME, getUnLockContent(NETCONF_RUNNING_QNAME));
93         Futures.addCallback(future, callback);
94         return future;
95     }
96
97     public ListenableFuture<RpcResult<CompositeNode>> unlockCandidate(final FutureCallback<RpcResult<CompositeNode>> callback) {
98         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME, getUnLockContent(NETCONF_CANDIDATE_QNAME));
99         Futures.addCallback(future, callback);
100         return future;
101     }
102
103     public ListenableFuture<RpcResult<CompositeNode>> discardChanges(final FutureCallback<RpcResult<CompositeNode>> callback) {
104         Preconditions.checkNotNull(callback);
105
106         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_DISCARD_CHANGES_QNAME, DISCARD_CHANGES_RPC_CONTENT);
107         Futures.addCallback(future, callback);
108         return future;
109     }
110
111     public ListenableFuture<RpcResult<CompositeNode>> commit(final FutureCallback<RpcResult<CompositeNode>> callback) {
112         Preconditions.checkNotNull(callback);
113
114         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME, NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
115         Futures.addCallback(future, callback);
116         return future;
117     }
118
119     public ListenableFuture<RpcResult<CompositeNode>> validate(final FutureCallback<RpcResult<CompositeNode>> callback, final QName datastore) {
120         Preconditions.checkNotNull(callback);
121         Preconditions.checkNotNull(datastore);
122
123         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NetconfMessageTransformUtil.NETCONF_VALIDATE_QNAME, getValidateContent(datastore));
124         Futures.addCallback(future, callback);
125         return future;
126     }
127
128     public ListenableFuture<RpcResult<CompositeNode>> validateCandidate(final FutureCallback<RpcResult<CompositeNode>> callback) {
129         return validate(callback, NETCONF_CANDIDATE_QNAME);
130     }
131
132
133     public ListenableFuture<RpcResult<CompositeNode>> validateRunning(final FutureCallback<RpcResult<CompositeNode>> callback) {
134         return validate(callback, NETCONF_RUNNING_QNAME);
135     }
136
137     public ListenableFuture<RpcResult<CompositeNode>> copyConfig(final FutureCallback<RpcResult<CompositeNode>> callback, final QName source, final QName target) {
138         Preconditions.checkNotNull(callback);
139         Preconditions.checkNotNull(source);
140         Preconditions.checkNotNull(target);
141
142         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_QNAME, getCopyConfigContent(source, target));
143         Futures.addCallback(future, callback);
144         return future;
145     }
146
147     public ListenableFuture<RpcResult<CompositeNode>> copyRunningToCandidate(final FutureCallback<RpcResult<CompositeNode>> callback) {
148         return copyConfig(callback, NETCONF_RUNNING_QNAME, NETCONF_CANDIDATE_QNAME);
149     }
150
151     public ListenableFuture<RpcResult<CompositeNode>> getConfig(final FutureCallback<RpcResult<CompositeNode>> callback, final QName datastore, final Optional<YangInstanceIdentifier> filterPath) {
152         Preconditions.checkNotNull(callback);
153         Preconditions.checkNotNull(datastore);
154
155         final ListenableFuture<RpcResult<CompositeNode>> future;
156         if (filterPath.isPresent()) {
157             final Node<?> node = toFilterStructure(filterPath.get());
158             future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
159                             NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, getSourceNode(datastore), node));
160         } else {
161             future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
162                             NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, getSourceNode(datastore)));
163         }
164
165         Futures.addCallback(future, callback);
166         return future;
167     }
168
169     public ListenableFuture<RpcResult<CompositeNode>> getConfigRunning(final FutureCallback<RpcResult<CompositeNode>> callback, final Optional<YangInstanceIdentifier> filterPath) {
170         return getConfig(callback, NETCONF_RUNNING_QNAME, filterPath);
171     }
172
173     public ListenableFuture<RpcResult<CompositeNode>> getConfigCandidate(final FutureCallback<RpcResult<CompositeNode>> callback, final Optional<YangInstanceIdentifier> filterPath) {
174         return getConfig(callback, NETCONF_CANDIDATE_QNAME, filterPath);
175     }
176
177     public ListenableFuture<RpcResult<CompositeNode>> get(final FutureCallback<RpcResult<CompositeNode>> callback, final Optional<YangInstanceIdentifier> filterPath) {
178         Preconditions.checkNotNull(callback);
179
180         final ListenableFuture<RpcResult<CompositeNode>> future;
181         final Node<?> node = filterPath.isPresent() ? toFilterStructure(filterPath.get()) : NetconfMessageTransformUtil.GET_RPC_CONTENT;
182         future = rpc.invokeRpc(NETCONF_GET_QNAME, NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, node));
183
184         Futures.addCallback(future, callback);
185         return future;
186     }
187
188     public ListenableFuture<RpcResult<CompositeNode>> editConfigCandidate(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final ModifyAction modifyAction, final boolean rollback) {
189         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.of(modifyAction), rollback);
190     }
191
192     public ListenableFuture<RpcResult<CompositeNode>> editConfigCandidate(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final boolean rollback) {
193         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.<ModifyAction>absent(), rollback);
194     }
195
196     public ListenableFuture<RpcResult<CompositeNode>> editConfigRunning(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final ModifyAction modifyAction, final boolean rollback) {
197         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.of(modifyAction), rollback);
198     }
199
200     public ListenableFuture<RpcResult<CompositeNode>> editConfigRunning(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final boolean rollback) {
201         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.<ModifyAction>absent(), rollback);
202     }
203
204     public ListenableFuture<RpcResult<CompositeNode>> editConfig(final FutureCallback<? super RpcResult<CompositeNode>> callback, final QName datastore, final CompositeNode editStructure, final Optional<ModifyAction> modifyAction, final boolean rollback) {
205         Preconditions.checkNotNull(editStructure);
206         Preconditions.checkNotNull(callback);
207         Preconditions.checkNotNull(datastore);
208
209         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, getEditConfigContent(datastore, editStructure, modifyAction, rollback));
210
211         Futures.addCallback(future, callback);
212         return future;
213     }
214
215     private CompositeNode getEditConfigContent(final QName datastore, final CompositeNode editStructure, final Optional<ModifyAction> defaultOperation, final boolean rollback) {
216         final CompositeNodeBuilder<ImmutableCompositeNode> ret = ImmutableCompositeNode.builder();
217
218         // Target
219         ret.add(getTargetNode(datastore));
220
221         // Default operation
222         if(defaultOperation.isPresent()) {
223             final SimpleNode<String> defOp = NodeFactory.createImmutableSimpleNode(NETCONF_DEFAULT_OPERATION_QNAME, null, NetconfMessageTransformUtil.modifyOperationToXmlString(defaultOperation.get()));
224             ret.add(defOp);
225         }
226
227         // Error option
228         if(rollback) {
229             ret.addLeaf(NETCONF_ERROR_OPTION_QNAME, ROLLBACK_ON_ERROR_OPTION);
230         }
231
232         ret.setQName(NETCONF_EDIT_CONFIG_QNAME);
233         // Edit content
234         ret.add(editStructure);
235         return ret.toInstance();
236     }
237
238     private static CompositeNode getSourceNode(final QName datastore) {
239         return NodeFactory.createImmutableCompositeNode(NETCONF_SOURCE_QNAME, null,
240                 Collections.<Node<?>> singletonList(new SimpleNodeTOImpl<>(datastore, null, null)));
241     }
242
243
244     public static CompositeNode getLockContent(final QName datastore) {
245         return NodeFactory.createImmutableCompositeNode(NETCONF_LOCK_QNAME, null, Collections.<Node<?>>singletonList(
246                 getTargetNode(datastore)));
247     }
248
249     private static CompositeNode getTargetNode(final QName datastore) {
250         return NodeFactory.createImmutableCompositeNode(NETCONF_TARGET_QNAME, null, Collections.<Node<?>>singletonList(
251                 NodeFactory.createImmutableSimpleNode(datastore, null, null)
252         ));
253     }
254
255     public static CompositeNode getCopyConfigContent(final QName source, final QName target) {
256         return NodeFactory.createImmutableCompositeNode(NETCONF_LOCK_QNAME, null,
257                 Lists.<Node<?>> newArrayList(getTargetNode(target), getSourceNode(source)));
258     }
259
260     public static CompositeNode getValidateContent(final QName source) {
261         return NodeFactory.createImmutableCompositeNode(NETCONF_VALIDATE_QNAME, null, Lists.<Node<?>> newArrayList(getSourceNode(source)));
262     }
263
264     public static CompositeNode getUnLockContent(final QName preferedDatastore) {
265         return NodeFactory.createImmutableCompositeNode(NETCONF_UNLOCK_QNAME, null, Collections.<Node<?>>singletonList(
266                 getTargetNode(preferedDatastore)));
267     }
268
269 }