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.controller.sal.connect.netconf.sal;
10 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
13 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
14 import org.opendaylight.controller.sal.common.util.RpcErrors;
15 import org.opendaylight.controller.sal.connect.util.FailedRpcResult;
16 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
17 import org.opendaylight.controller.sal.core.api.RpcImplementation;
18 import org.opendaylight.yangtools.yang.common.RpcError;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public final class NetconfDeviceCommitHandler implements DataCommitHandler<InstanceIdentifier,CompositeNode> {
27 private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceCommitHandler.class);
29 private final RemoteDeviceId id;
30 private final RpcImplementation rpc;
31 private final boolean rollbackSupported;
33 public NetconfDeviceCommitHandler(final RemoteDeviceId id, final RpcImplementation rpc, final boolean rollbackSupported) {
36 this.rollbackSupported = rollbackSupported;
40 public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
41 final DataModification<InstanceIdentifier, CompositeNode> modification) {
43 final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit = new NetconfDeviceTwoPhaseCommitTransaction(id, rpc,
44 modification, true, rollbackSupported);
46 twoPhaseCommit.prepare();
47 } catch (final InterruptedException e) {
48 Thread.currentThread().interrupt();
49 throw new RuntimeException(id + ": Interrupted while waiting for response", e);
50 } catch (final ExecutionException e) {
51 logger.warn("{}: Error executing pre commit operation on remote device", id, e);
52 return new FailingTransaction(twoPhaseCommit, e);
55 return twoPhaseCommit;
59 * Always fail commit transaction that rolls back delegate transaction afterwards
61 private class FailingTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
62 private final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit;
63 private final ExecutionException e;
65 public FailingTransaction(final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit, final ExecutionException e) {
66 this.twoPhaseCommit = twoPhaseCommit;
71 public DataModification<InstanceIdentifier, CompositeNode> getModification() {
72 return twoPhaseCommit.getModification();
76 public RpcResult<Void> finish() throws IllegalStateException {
77 return new FailedRpcResult<>(RpcErrors.getRpcError(null, null, null, RpcError.ErrorSeverity.ERROR,
78 id + ": Unexpected operation error during pre-commit operations", RpcError.ErrorType.APPLICATION, e));
82 public RpcResult<Void> rollback() throws IllegalStateException {
83 return twoPhaseCommit.rollback();