2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.COMMIT_RPC_CONTENT;
13 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.EDIT_CONTENT_NODEID;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_NODEID;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DEFAULT_OPERATION_NODEID;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_NODEID;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_ERROR_OPTION_NODEID;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_NODEID;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_NODEID;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
30 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_SOURCE_NODEID;
31 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TARGET_NODEID;
32 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_NODEID;
33 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
34 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_VALIDATE_NODEID;
35 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_VALIDATE_QNAME;
36 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.ROLLBACK_ON_ERROR_OPTION;
37 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
38 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
40 import com.google.common.util.concurrent.FutureCallback;
41 import com.google.common.util.concurrent.Futures;
42 import com.google.common.util.concurrent.ListenableFuture;
43 import com.google.common.util.concurrent.MoreExecutors;
44 import java.util.List;
45 import java.util.Locale;
46 import java.util.Optional;
47 import org.eclipse.jdt.annotation.NonNull;
48 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
49 import org.opendaylight.mdsal.dom.api.DOMRpcService;
50 import org.opendaylight.netconf.api.ModifyAction;
51 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade.KeepaliveDOMRpcService;
52 import org.opendaylight.netconf.sal.connect.netconf.sal.SchemalessNetconfDeviceRpc;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.copy.config.input.target.ConfigTarget;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.get.config.input.source.ConfigSource;
55 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
56 import org.opendaylight.yangtools.yang.common.Empty;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
63 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
64 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
65 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
66 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
69 * Provides base operations for netconf e.g. get, get-config, edit-config, (un)lock, commit etc.
70 * According to RFC-6241
72 public final class NetconfBaseOps {
73 private static final NodeIdentifier CONFIG_SOURCE_NODEID = NodeIdentifier.create(ConfigSource.QNAME);
74 private static final NodeIdentifier CONFIG_TARGET_NODEID = NodeIdentifier.create(ConfigTarget.QNAME);
76 private final DOMRpcService rpc;
77 private final MountPointContext mountContext;
78 private final RpcStructureTransformer transformer;
80 public NetconfBaseOps(final DOMRpcService rpc, final MountPointContext mountContext) {
82 this.mountContext = mountContext;
84 if (rpc instanceof KeepaliveDOMRpcService
85 && ((KeepaliveDOMRpcService) rpc).getDeviceRpc() instanceof SchemalessNetconfDeviceRpc) {
86 this.transformer = new SchemalessRpcStructureTransformer();
88 this.transformer = new NetconfRpcStructureTransformer(mountContext);
92 public ListenableFuture<? extends DOMRpcResult> lock(final FutureCallback<DOMRpcResult> callback,
93 final QName datastore) {
94 requireNonNull(callback);
95 requireNonNull(datastore);
97 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_LOCK_QNAME,
98 getLockContent(datastore));
99 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
103 public ListenableFuture<? extends DOMRpcResult> lockCandidate(final FutureCallback<DOMRpcResult> callback) {
104 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_LOCK_QNAME,
105 getLockContent(NETCONF_CANDIDATE_QNAME));
106 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
110 public ListenableFuture<? extends DOMRpcResult> lockRunning(final FutureCallback<DOMRpcResult> callback) {
111 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_LOCK_QNAME,
112 getLockContent(NETCONF_RUNNING_QNAME));
113 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
117 public ListenableFuture<? extends DOMRpcResult> unlock(final FutureCallback<DOMRpcResult> callback,
118 final QName datastore) {
119 requireNonNull(callback);
120 requireNonNull(datastore);
122 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME,
123 getUnLockContent(datastore));
124 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
128 public ListenableFuture<? extends DOMRpcResult> unlockRunning(final FutureCallback<DOMRpcResult> callback) {
129 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME,
130 getUnLockContent(NETCONF_RUNNING_QNAME));
131 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
135 public ListenableFuture<? extends DOMRpcResult> unlockCandidate(final FutureCallback<DOMRpcResult> callback) {
136 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_UNLOCK_QNAME,
137 getUnLockContent(NETCONF_CANDIDATE_QNAME));
138 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
142 public ListenableFuture<? extends DOMRpcResult> discardChanges(final FutureCallback<DOMRpcResult> callback) {
143 requireNonNull(callback);
145 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_DISCARD_CHANGES_QNAME, null);
146 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
150 public ListenableFuture<? extends DOMRpcResult> commit(final FutureCallback<DOMRpcResult> callback) {
151 requireNonNull(callback);
153 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_COMMIT_QNAME, COMMIT_RPC_CONTENT);
154 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
158 public ListenableFuture<? extends DOMRpcResult> validate(final FutureCallback<DOMRpcResult> callback,
159 final QName datastore) {
160 requireNonNull(callback);
162 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_VALIDATE_QNAME,
163 getValidateContent(requireNonNull(datastore)));
164 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
168 public ListenableFuture<? extends DOMRpcResult> validateCandidate(final FutureCallback<DOMRpcResult> callback) {
169 return validate(callback, NETCONF_CANDIDATE_QNAME);
172 public ListenableFuture<? extends DOMRpcResult> validateRunning(final FutureCallback<DOMRpcResult> callback) {
173 return validate(callback, NETCONF_RUNNING_QNAME);
176 public ListenableFuture<? extends DOMRpcResult> copyConfig(final FutureCallback<DOMRpcResult> callback,
177 final QName source, final QName target) {
178 requireNonNull(callback);
180 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_COPY_CONFIG_QNAME,
181 getCopyConfigContent(requireNonNull(source), requireNonNull(target)));
182 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
186 public ListenableFuture<? extends DOMRpcResult> copyRunningToCandidate(
187 final FutureCallback<DOMRpcResult> callback) {
188 return copyConfig(callback, NETCONF_RUNNING_QNAME, NETCONF_CANDIDATE_QNAME);
191 public ListenableFuture<? extends DOMRpcResult> getConfig(final FutureCallback<DOMRpcResult> callback,
192 final QName datastore,
193 final Optional<YangInstanceIdentifier> filterPath) {
194 requireNonNull(callback);
195 requireNonNull(datastore);
197 final ListenableFuture<? extends DOMRpcResult> future;
198 if (isFilterPresent(filterPath)) {
199 final DataContainerChild<?, ?> node = transformer.toFilterStructure(filterPath.get());
200 future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
201 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID, getSourceNode(datastore), node));
203 future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
204 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID, getSourceNode(datastore)));
207 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
211 public ListenableFuture<Optional<NormalizedNode<?, ?>>> getConfigRunningData(
212 final FutureCallback<DOMRpcResult> callback, final Optional<YangInstanceIdentifier> filterPath) {
213 return extractData(filterPath, getConfigRunning(callback, filterPath));
216 public ListenableFuture<Optional<NormalizedNode<?, ?>>> getConfigRunningData(
217 final FutureCallback<DOMRpcResult> callback, final Optional<YangInstanceIdentifier> filterPath,
218 final List<YangInstanceIdentifier> fields) {
219 // FIXME: implement this method
220 throw new UnsupportedOperationException();
223 public ListenableFuture<Optional<NormalizedNode<?, ?>>> getData(final FutureCallback<DOMRpcResult> callback,
224 final Optional<YangInstanceIdentifier> filterPath) {
225 return extractData(filterPath, get(callback, filterPath));
228 public ListenableFuture<Optional<NormalizedNode<?, ?>>> getData(final FutureCallback<DOMRpcResult> callback,
229 final Optional<YangInstanceIdentifier> path, final List<YangInstanceIdentifier> fields) {
230 // FIXME: implement this method
231 throw new UnsupportedOperationException();
234 private ListenableFuture<Optional<NormalizedNode<?, ?>>> extractData(
235 final Optional<YangInstanceIdentifier> path, final ListenableFuture<? extends DOMRpcResult> configRunning) {
236 return Futures.transform(configRunning, result -> {
237 checkArgument(result.getErrors().isEmpty(), "Unable to read data: %s, errors: %s", path,
239 final DataContainerChild<?, ?> dataNode = ((ContainerNode) result.getResult())
240 .getChild(NetconfMessageTransformUtil.NETCONF_DATA_NODEID).get();
241 return transformer.selectFromDataStructure(dataNode, path.get());
242 }, MoreExecutors.directExecutor());
245 public ListenableFuture<? extends DOMRpcResult> getConfigRunning(final FutureCallback<DOMRpcResult> callback,
246 final Optional<YangInstanceIdentifier> filterPath) {
247 return getConfig(callback, NETCONF_RUNNING_QNAME, filterPath);
250 public ListenableFuture<? extends DOMRpcResult> getConfigCandidate(final FutureCallback<DOMRpcResult> callback,
251 final Optional<YangInstanceIdentifier> filterPath) {
252 return getConfig(callback, NETCONF_CANDIDATE_QNAME, filterPath);
255 public ListenableFuture<? extends DOMRpcResult> get(final FutureCallback<DOMRpcResult> callback,
256 final Optional<YangInstanceIdentifier> filterPath) {
257 requireNonNull(callback);
259 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_GET_QNAME,
260 isFilterPresent(filterPath)
261 ? NetconfMessageTransformUtil.wrap(NETCONF_GET_NODEID,
262 toFilterStructure(filterPath.get(), mountContext.getEffectiveModelContext()))
263 : NetconfMessageTransformUtil.GET_RPC_CONTENT);
264 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
268 private static boolean isFilterPresent(final Optional<YangInstanceIdentifier> filterPath) {
269 return filterPath.isPresent() && !filterPath.get().isEmpty();
272 public ListenableFuture<? extends DOMRpcResult> editConfigCandidate(
273 final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure,
274 final ModifyAction modifyAction, final boolean rollback) {
275 return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.of(modifyAction), rollback);
278 public ListenableFuture<? extends DOMRpcResult> editConfigCandidate(
279 final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure,
280 final boolean rollback) {
281 return editConfig(callback, NETCONF_CANDIDATE_QNAME, editStructure, Optional.empty(), rollback);
284 public ListenableFuture<? extends DOMRpcResult> editConfigRunning(
285 final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure,
286 final ModifyAction modifyAction, final boolean rollback) {
287 return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.of(modifyAction), rollback);
290 public ListenableFuture<? extends DOMRpcResult> editConfigRunning(
291 final FutureCallback<? super DOMRpcResult> callback, final DataContainerChild<?, ?> editStructure,
292 final boolean rollback) {
293 return editConfig(callback, NETCONF_RUNNING_QNAME, editStructure, Optional.empty(), rollback);
296 public ListenableFuture<? extends DOMRpcResult> editConfig(
297 final FutureCallback<? super DOMRpcResult> callback, final QName datastore,
298 final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> modifyAction,
299 final boolean rollback) {
300 requireNonNull(callback);
302 final ListenableFuture<? extends DOMRpcResult> future = rpc.invokeRpc(NETCONF_EDIT_CONFIG_QNAME,
303 getEditConfigContent(requireNonNull(datastore), requireNonNull(editStructure), modifyAction, rollback));
305 Futures.addCallback(future, callback, MoreExecutors.directExecutor());
309 public ChoiceNode createEditConfigStrcture(final Optional<NormalizedNode<?, ?>> lastChild,
310 final Optional<ModifyAction> operation,
311 final YangInstanceIdentifier dataPath) {
312 return Builders.choiceBuilder()
313 .withNodeIdentifier(EDIT_CONTENT_NODEID)
314 .withChild(transformer.createEditConfigStructure(lastChild, dataPath, operation))
318 private static ContainerNode getEditConfigContent(
319 final QName datastore, final DataContainerChild<?, ?> editStructure,
320 final Optional<ModifyAction> defaultOperation, final boolean rollback) {
321 final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> editBuilder =
322 Builders.containerBuilder().withNodeIdentifier(NETCONF_EDIT_CONFIG_NODEID);
325 editBuilder.withChild(getTargetNode(datastore));
328 if (defaultOperation.isPresent()) {
329 final String opString = defaultOperation.get().name().toLowerCase(Locale.ROOT);
330 editBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(NETCONF_DEFAULT_OPERATION_NODEID)
331 .withValue(opString).build());
336 editBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(NETCONF_ERROR_OPTION_NODEID)
337 .withValue(ROLLBACK_ON_ERROR_OPTION).build());
341 editBuilder.withChild(editStructure);
342 return editBuilder.build();
345 public static @NonNull ContainerNode getSourceNode(final QName datastore) {
346 return Builders.containerBuilder()
347 .withNodeIdentifier(NETCONF_SOURCE_NODEID)
348 .withChild(Builders.choiceBuilder()
349 .withNodeIdentifier(CONFIG_SOURCE_NODEID)
350 .withChild(ImmutableNodes.leafNode(datastore, Empty.getInstance()))
355 public static @NonNull ContainerNode getLockContent(final QName datastore) {
356 return Builders.containerBuilder().withNodeIdentifier(NETCONF_LOCK_NODEID)
357 .withChild(getTargetNode(datastore)).build();
360 public static @NonNull ContainerNode getTargetNode(final QName datastore) {
361 return Builders.containerBuilder().withNodeIdentifier(NETCONF_TARGET_NODEID)
362 .withChild(Builders.choiceBuilder().withNodeIdentifier(CONFIG_TARGET_NODEID).withChild(
363 Builders.leafBuilder().withNodeIdentifier(toId(datastore)).withValue(Empty.getInstance()).build())
367 public static @NonNull ContainerNode getCopyConfigContent(final QName source, final QName target) {
368 return Builders.containerBuilder().withNodeIdentifier(NETCONF_COPY_CONFIG_NODEID)
369 .withChild(getTargetNode(target)).withChild(getSourceNode(source)).build();
372 public static @NonNull ContainerNode getValidateContent(final QName source) {
373 return Builders.containerBuilder().withNodeIdentifier(NETCONF_VALIDATE_NODEID)
374 .withChild(getSourceNode(source)).build();
377 public static @NonNull ContainerNode getUnLockContent(final QName datastore) {
378 return Builders.containerBuilder().withNodeIdentifier(NETCONF_UNLOCK_NODEID)
379 .withChild(getTargetNode(datastore)).build();