BUG-2560 Canonical write to remote netconf devices
[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 QName datastore, final Optional<YangInstanceIdentifier> filterPath) {
178         Preconditions.checkNotNull(callback);
179         Preconditions.checkNotNull(datastore);
180
181         final ListenableFuture<RpcResult<CompositeNode>> future;
182         if (filterPath.isPresent()) {
183             final Node<?> node = toFilterStructure(filterPath.get());
184             future = rpc.invokeRpc(NETCONF_GET_QNAME,
185                             NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, getSourceNode(datastore), node));
186         } else {
187             future = rpc.invokeRpc(NETCONF_GET_QNAME,
188                             NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, getSourceNode(datastore)));
189         }
190
191         Futures.addCallback(future, callback);
192         return future;
193     }
194
195     public ListenableFuture<RpcResult<CompositeNode>> getRunning(final FutureCallback<RpcResult<CompositeNode>> callback, final Optional<YangInstanceIdentifier> filterPath) {
196         return get(callback, NETCONF_RUNNING_QNAME, filterPath);
197     }
198
199     public ListenableFuture<RpcResult<CompositeNode>> getCandidate(final FutureCallback<RpcResult<CompositeNode>> callback, final Optional<YangInstanceIdentifier> filterPath) {
200         return get(callback, NETCONF_CANDIDATE_QNAME, filterPath);
201     }
202
203
204     public ListenableFuture<RpcResult<CompositeNode>> editConfigCandidate(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final ModifyAction modifyAction, final boolean rollback) {
205         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.of(modifyAction), rollback);
206     }
207
208     public ListenableFuture<RpcResult<CompositeNode>> editConfigCandidate(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final boolean rollback) {
209         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.<ModifyAction>absent(), rollback);
210     }
211
212     public ListenableFuture<RpcResult<CompositeNode>> editConfigRunning(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final ModifyAction modifyAction, final boolean rollback) {
213         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.of(modifyAction), rollback);
214     }
215
216     public ListenableFuture<RpcResult<CompositeNode>> editConfigRunning(final FutureCallback<? super RpcResult<CompositeNode>> callback, final CompositeNode editStructure, final boolean rollback) {
217         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.<ModifyAction>absent(), rollback);
218     }
219
220     public ListenableFuture<RpcResult<CompositeNode>> editConfig(final FutureCallback<? super RpcResult<CompositeNode>> callback, final QName datastore, final CompositeNode editStructure, final Optional<ModifyAction> modifyAction, final boolean rollback) {
221         Preconditions.checkNotNull(editStructure);
222         Preconditions.checkNotNull(callback);
223         Preconditions.checkNotNull(datastore);
224
225         final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_EDIT_CONFIG_QNAME, getEditConfigContent(datastore, editStructure, modifyAction, rollback));
226
227         Futures.addCallback(future, callback);
228         return future;
229     }
230
231     private CompositeNode getEditConfigContent(final QName datastore, final CompositeNode editStructure, final Optional<ModifyAction> defaultOperation, final boolean rollback) {
232         final CompositeNodeBuilder<ImmutableCompositeNode> ret = ImmutableCompositeNode.builder();
233
234         // Target
235         ret.add(getTargetNode(datastore));
236
237         // Default operation
238         if(defaultOperation.isPresent()) {
239             final SimpleNode<String> defOp = NodeFactory.createImmutableSimpleNode(NETCONF_DEFAULT_OPERATION_QNAME, null, NetconfMessageTransformUtil.modifyOperationToXmlString(defaultOperation.get()));
240             ret.add(defOp);
241         }
242
243         // Error option
244         if(rollback) {
245             ret.addLeaf(NETCONF_ERROR_OPTION_QNAME, ROLLBACK_ON_ERROR_OPTION);
246         }
247
248         ret.setQName(NETCONF_EDIT_CONFIG_QNAME);
249         // Edit content
250         ret.add(editStructure);
251         return ret.toInstance();
252     }
253
254     private static CompositeNode getSourceNode(final QName datastore) {
255         return NodeFactory.createImmutableCompositeNode(NETCONF_SOURCE_QNAME, null,
256                 Collections.<Node<?>> singletonList(new SimpleNodeTOImpl<>(datastore, null, null)));
257     }
258
259
260     public static CompositeNode getLockContent(final QName datastore) {
261         return NodeFactory.createImmutableCompositeNode(NETCONF_LOCK_QNAME, null, Collections.<Node<?>>singletonList(
262                 getTargetNode(datastore)));
263     }
264
265     private static CompositeNode getTargetNode(final QName datastore) {
266         return NodeFactory.createImmutableCompositeNode(NETCONF_TARGET_QNAME, null, Collections.<Node<?>>singletonList(
267                 NodeFactory.createImmutableSimpleNode(datastore, null, null)
268         ));
269     }
270
271     public static CompositeNode getCopyConfigContent(final QName source, final QName target) {
272         return NodeFactory.createImmutableCompositeNode(NETCONF_LOCK_QNAME, null,
273                 Lists.<Node<?>> newArrayList(getTargetNode(target), getSourceNode(source)));
274     }
275
276     public static CompositeNode getValidateContent(final QName source) {
277         return NodeFactory.createImmutableCompositeNode(NETCONF_VALIDATE_QNAME, null, Lists.<Node<?>> newArrayList(getSourceNode(source)));
278     }
279
280     public static CompositeNode getUnLockContent(final QName preferedDatastore) {
281         return NodeFactory.createImmutableCompositeNode(NETCONF_UNLOCK_QNAME, null, Collections.<Node<?>>singletonList(
282                 getTargetNode(preferedDatastore)));
283     }
284
285 }