Use YangInstanceIdentifier.isEmpty()
[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.NETCONF_CANDIDATE_QNAME;
12 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_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 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
28 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
29
30 import com.google.common.base.Optional;
31 import com.google.common.base.Preconditions;
32 import com.google.common.util.concurrent.FutureCallback;
33 import com.google.common.util.concurrent.Futures;
34 import com.google.common.util.concurrent.ListenableFuture;
35 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.copy.config.input.target.ConfigTarget;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.get.config.input.source.ConfigSource;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48
49 /**
50  * Provides base operations for netconf e.g. get, get-config, edit-config, (un)lock, commit etc.
51  * According to RFC-6241
52  */
53 public final class NetconfBaseOps {
54
55     private final DOMRpcService rpc;
56     private final SchemaContext schemaContext;
57
58     public NetconfBaseOps(final DOMRpcService rpc, final SchemaContext schemaContext) {
59         this.rpc = rpc;
60         this.schemaContext = schemaContext;
61     }
62
63     public ListenableFuture<DOMRpcResult> lock(final FutureCallback<DOMRpcResult> callback, final QName datastore) {
64         Preconditions.checkNotNull(callback);
65         Preconditions.checkNotNull(datastore);
66
67         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_LOCK_QNAME), getLockContent(datastore));
68         Futures.addCallback(future, callback);
69         return future;
70     }
71
72     public ListenableFuture<DOMRpcResult> lockCandidate(final FutureCallback<DOMRpcResult> callback) {
73         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_LOCK_QNAME), getLockContent(NETCONF_CANDIDATE_QNAME));
74         Futures.addCallback(future, callback);
75         return future;
76     }
77
78
79     public ListenableFuture<DOMRpcResult> lockRunning(final FutureCallback<DOMRpcResult> callback) {
80         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_LOCK_QNAME), getLockContent(NETCONF_RUNNING_QNAME));
81         Futures.addCallback(future, callback);
82         return future;
83     }
84
85     public ListenableFuture<DOMRpcResult> unlock(final FutureCallback<DOMRpcResult> callback, final QName datastore) {
86         Preconditions.checkNotNull(callback);
87         Preconditions.checkNotNull(datastore);
88
89         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_UNLOCK_QNAME), getUnLockContent(datastore));
90         Futures.addCallback(future, callback);
91         return future;
92     }
93
94     public ListenableFuture<DOMRpcResult> unlockRunning(final FutureCallback<DOMRpcResult> callback) {
95         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_UNLOCK_QNAME), getUnLockContent(NETCONF_RUNNING_QNAME));
96         Futures.addCallback(future, callback);
97         return future;
98     }
99
100     public ListenableFuture<DOMRpcResult> unlockCandidate(final FutureCallback<DOMRpcResult> callback) {
101         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_UNLOCK_QNAME), getUnLockContent(NETCONF_CANDIDATE_QNAME));
102         Futures.addCallback(future, callback);
103         return future;
104     }
105
106     public ListenableFuture<DOMRpcResult> discardChanges(final FutureCallback<DOMRpcResult> callback) {
107         Preconditions.checkNotNull(callback);
108
109         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
110         Futures.addCallback(future, callback);
111         return future;
112     }
113
114     public ListenableFuture<DOMRpcResult> commit(final FutureCallback<DOMRpcResult> callback) {
115         Preconditions.checkNotNull(callback);
116
117         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME), NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
118         Futures.addCallback(future, callback);
119         return future;
120     }
121
122     public ListenableFuture<DOMRpcResult> validate(final FutureCallback<DOMRpcResult> callback, final QName datastore) {
123         Preconditions.checkNotNull(callback);
124         Preconditions.checkNotNull(datastore);
125
126         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_VALIDATE_QNAME), getValidateContent(datastore));
127         Futures.addCallback(future, callback);
128         return future;
129     }
130
131     public ListenableFuture<DOMRpcResult> validateCandidate(final FutureCallback<DOMRpcResult> callback) {
132         return validate(callback, NETCONF_CANDIDATE_QNAME);
133     }
134
135
136     public ListenableFuture<DOMRpcResult> validateRunning(final FutureCallback<DOMRpcResult> callback) {
137         return validate(callback, NETCONF_RUNNING_QNAME);
138     }
139
140     public ListenableFuture<DOMRpcResult> copyConfig(final FutureCallback<DOMRpcResult> callback, final QName source, final QName target) {
141         Preconditions.checkNotNull(callback);
142         Preconditions.checkNotNull(source);
143         Preconditions.checkNotNull(target);
144
145         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_QNAME), getCopyConfigContent(source, target));
146         Futures.addCallback(future, callback);
147         return future;
148     }
149
150     public ListenableFuture<DOMRpcResult> copyRunningToCandidate(final FutureCallback<DOMRpcResult> callback) {
151         return copyConfig(callback, NETCONF_RUNNING_QNAME, NETCONF_CANDIDATE_QNAME);
152     }
153
154     public ListenableFuture<DOMRpcResult> getConfig(final FutureCallback<DOMRpcResult> callback, final QName datastore, final Optional<YangInstanceIdentifier> filterPath) {
155         Preconditions.checkNotNull(callback);
156         Preconditions.checkNotNull(datastore);
157
158         final ListenableFuture<DOMRpcResult> future;
159         if (isFilterPresent(filterPath)) {
160             // FIXME the source node has to be wrapped in a choice
161             final DataContainerChild<?, ?> node = toFilterStructure(filterPath.get(), schemaContext);
162             future = rpc.invokeRpc(toPath(NETCONF_GET_CONFIG_QNAME),
163                             NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, getSourceNode(datastore), node));
164         } else {
165             future = rpc.invokeRpc(toPath(NETCONF_GET_CONFIG_QNAME),
166                             NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, getSourceNode(datastore)));
167         }
168
169         Futures.addCallback(future, callback);
170         return future;
171     }
172
173     public ListenableFuture<DOMRpcResult> getConfigRunning(final FutureCallback<DOMRpcResult> callback, final Optional<YangInstanceIdentifier> filterPath) {
174         return getConfig(callback, NETCONF_RUNNING_QNAME, filterPath);
175     }
176
177     public ListenableFuture<DOMRpcResult> getConfigCandidate(final FutureCallback<DOMRpcResult> callback, final Optional<YangInstanceIdentifier> filterPath) {
178         return getConfig(callback, NETCONF_CANDIDATE_QNAME, filterPath);
179     }
180
181     public ListenableFuture<DOMRpcResult> get(final FutureCallback<DOMRpcResult> callback, final Optional<YangInstanceIdentifier> filterPath) {
182         Preconditions.checkNotNull(callback);
183
184         final ListenableFuture<DOMRpcResult> future;
185
186         future = isFilterPresent(filterPath) ?
187                 rpc.invokeRpc(toPath(NETCONF_GET_QNAME), NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, toFilterStructure(filterPath.get(), schemaContext))) :
188                 rpc.invokeRpc(toPath(NETCONF_GET_QNAME), NetconfMessageTransformUtil.GET_RPC_CONTENT);
189
190         Futures.addCallback(future, callback);
191         return future;
192     }
193
194     private boolean isFilterPresent(final Optional<YangInstanceIdentifier> filterPath) {
195         return filterPath.isPresent() && !filterPath.get().isEmpty();
196     }
197
198     public ListenableFuture<DOMRpcResult> editConfigCandidate(final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure, final ModifyAction modifyAction, final boolean rollback) {
199         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.of(modifyAction), rollback);
200     }
201
202     public ListenableFuture<DOMRpcResult> editConfigCandidate(final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure, final boolean rollback) {
203         return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.<ModifyAction>absent(), rollback);
204     }
205
206     public ListenableFuture<DOMRpcResult> editConfigRunning(final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure, final ModifyAction modifyAction, final boolean rollback) {
207         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.of(modifyAction), rollback);
208     }
209
210     public ListenableFuture<DOMRpcResult> editConfigRunning(final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure, final boolean rollback) {
211         return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.<ModifyAction>absent(), rollback);
212     }
213
214     public ListenableFuture<DOMRpcResult> editConfig(final FutureCallback<? super DOMRpcResult> callback, final QName datastore, final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> modifyAction, final boolean rollback) {
215         Preconditions.checkNotNull(editStructure);
216         Preconditions.checkNotNull(callback);
217         Preconditions.checkNotNull(datastore);
218
219         final ListenableFuture<DOMRpcResult> future = rpc.invokeRpc(toPath(NETCONF_EDIT_CONFIG_QNAME), getEditConfigContent(datastore, editStructure, modifyAction, rollback));
220
221         Futures.addCallback(future, callback);
222         return future;
223     }
224
225     public DataContainerChild<?, ?> createEditConfigStrcture(final Optional<NormalizedNode<?, ?>> lastChild, final Optional<ModifyAction> operation, final YangInstanceIdentifier dataPath) {
226         return NetconfMessageTransformUtil.createEditConfigStructure(schemaContext, dataPath, operation, lastChild);
227     }
228
229     private ContainerNode getEditConfigContent(final QName datastore, final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation, final boolean rollback) {
230         final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> editBuilder = Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_EDIT_CONFIG_QNAME));
231
232         // Target
233         editBuilder.withChild(getTargetNode(datastore));
234
235         // Default operation
236         if(defaultOperation.isPresent()) {
237             final String opString = defaultOperation.get().name().toLowerCase();
238             editBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(toId(NETCONF_DEFAULT_OPERATION_QNAME)).withValue(opString).build());
239         }
240
241         // Error option
242         if(rollback) {
243             editBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(toId(NETCONF_ERROR_OPTION_QNAME)).withValue(ROLLBACK_ON_ERROR_OPTION).build());
244         }
245
246         // Edit content
247         editBuilder.withChild(editStructure);
248         return editBuilder.build();
249     }
250
251     public static DataContainerChild<?, ?> getSourceNode(final QName datastore) {
252         return Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_SOURCE_QNAME))
253                 .withChild(
254                         Builders.choiceBuilder().withNodeIdentifier(toId(ConfigSource.QNAME)).withChild(
255                                 Builders.leafBuilder().withNodeIdentifier(toId(datastore)).build()).build()
256                 ).build();
257     }
258
259     public static ContainerNode getLockContent(final QName datastore) {
260         return Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_LOCK_QNAME))
261                 .withChild(getTargetNode(datastore)).build();
262     }
263
264     public static DataContainerChild<?, ?> getTargetNode(final QName datastore) {
265         return Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_TARGET_QNAME))
266                 .withChild(
267                         Builders.choiceBuilder().withNodeIdentifier(toId(ConfigTarget.QNAME)).withChild(
268                                 Builders.leafBuilder().withNodeIdentifier(toId(datastore)).build()).build()
269                 ).build();
270     }
271
272     public static NormalizedNode<?, ?> getCopyConfigContent(final QName source, final QName target) {
273         return Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_COPY_CONFIG_QNAME))
274                 .withChild(getTargetNode(target)).withChild(getSourceNode(source)).build();
275     }
276
277     public static NormalizedNode<?, ?> getValidateContent(final QName source) {
278         return Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_VALIDATE_QNAME))
279                 .withChild(getSourceNode(source)).build();
280     }
281
282     public static NormalizedNode<?, ?> getUnLockContent(final QName datastore) {
283         return Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_UNLOCK_QNAME))
284                 .withChild(getTargetNode(datastore)).build();
285     }
286
287 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.