Implement finding a primary based on the shard name and do basic wiring of Distribute...
[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.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;
24
25 public final class NetconfDeviceCommitHandler implements DataCommitHandler<InstanceIdentifier,CompositeNode> {
26
27     private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceCommitHandler.class);
28
29     private final RemoteDeviceId id;
30     private final RpcImplementation rpc;
31     private final boolean rollbackSupported;
32
33     public NetconfDeviceCommitHandler(final RemoteDeviceId id, final RpcImplementation rpc, final boolean rollbackSupported) {
34         this.id = id;
35         this.rpc = rpc;
36         this.rollbackSupported = rollbackSupported;
37     }
38
39     @Override
40     public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
41             final DataModification<InstanceIdentifier, CompositeNode> modification) {
42
43         final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit = new NetconfDeviceTwoPhaseCommitTransaction(id, rpc,
44                 modification, true, rollbackSupported);
45         try {
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("%s: Error executing pre commit operation on remote device", id, e);
52             return new FailingTransaction(twoPhaseCommit, e);
53         }
54
55         return twoPhaseCommit;
56     }
57
58     /**
59      * Always fail commit transaction that rolls back delegate transaction afterwards
60      */
61     private class FailingTransaction implements DataCommitTransaction<InstanceIdentifier, CompositeNode> {
62         private final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit;
63         private final ExecutionException e;
64
65         public FailingTransaction(final NetconfDeviceTwoPhaseCommitTransaction twoPhaseCommit, final ExecutionException e) {
66             this.twoPhaseCommit = twoPhaseCommit;
67             this.e = e;
68         }
69
70         @Override
71         public DataModification<InstanceIdentifier, CompositeNode> getModification() {
72             return twoPhaseCommit.getModification();
73         }
74
75         @Override
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));
79         }
80
81         @Override
82         public RpcResult<Void> rollback() throws IllegalStateException {
83             return twoPhaseCommit.rollback();
84         }
85     }
86 }