Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceCommitHandler.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 package org.opendaylight.controller.sal.connect.netconf.sal;
9
10 import java.util.concurrent.ExecutionException;
11
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.connect.util.RemoteDeviceId;
15 import org.opendaylight.controller.sal.core.api.RpcImplementation;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class NetconfDeviceCommitHandler implements DataCommitHandler<InstanceIdentifier,CompositeNode> {
25
26     private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceCommitHandler.class);
27
28     private final RemoteDeviceId id;
29     private final RpcImplementation rpc;
30     private final boolean rollbackSupported;
31
32     public NetconfDeviceCommitHandler(final RemoteDeviceId id, final RpcImplementation rpc, final boolean rollbackSupported) {
33         this.id = id;
34         this.rpc = rpc;
35         this.rollbackSupported = rollbackSupported;
36     }
37
38     @Override
39     public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
40             final DataModification<InstanceIdentifier, CompositeNode> modification) {
41
42         final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit = new NetconfDeviceTwoPhaseCommitTransaction(id, rpc,
43                 modification, true, rollbackSupported);
44         try {
45             twoPhaseCommit.prepare();
46         } catch (final InterruptedException e) {
47             Thread.currentThread().interrupt();
48             throw new RuntimeException(id + ": Interrupted while waiting for response", e);
49         } catch (final ExecutionException e) {
50             logger.warn("{}: Error executing pre commit operation on remote device", id, e);
51             return new FailingTransaction(twoPhaseCommit, e);
52         }
53
54         return twoPhaseCommit;
55     }
56
57     /**
58      * Always fail commit transaction that rolls back delegate transaction afterwards
59      */
60     private class FailingTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
61         private final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit;
62         private final ExecutionException e;
63
64         public FailingTransaction(final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit, final ExecutionException e) {
65             this.twoPhaseCommit = twoPhaseCommit;
66             this.e = e;
67         }
68
69         @Override
70         public DataModification<InstanceIdentifier, CompositeNode> getModification() {
71             return twoPhaseCommit.getModification();
72         }
73
74         @Override
75         public RpcResult<Void> finish() throws IllegalStateException {
76             return RpcResultBuilder.<Void>failed().withError( RpcError.ErrorType.APPLICATION,
77                     id + ": Unexpected operation error during pre-commit operations", e ).build();
78         }
79
80         @Override
81         public RpcResult<Void> rollback() throws IllegalStateException {
82             return twoPhaseCommit.rollback();
83         }
84     }
85 }