Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / sal / tx / NetconfDeviceWriteOnlyTxTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.sal.tx;
10
11 import static org.junit.Assert.fail;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.atMost;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.inOrder;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
20 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
21 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
22 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
23
24 import com.google.common.util.concurrent.CheckedFuture;
25 import com.google.common.util.concurrent.Futures;
26 import java.net.InetSocketAddress;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InOrder;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
35 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
36 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
37 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
38 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
39 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49
50 public class NetconfDeviceWriteOnlyTxTest {
51
52     private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
53
54     @Mock
55     private DOMRpcService rpc;
56     private YangInstanceIdentifier yangIId;
57
58     @Before
59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61
62         final CheckedFuture<DefaultDOMRpcResult, Exception> successFuture =
63                 Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null)));
64
65         doReturn(successFuture)
66                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
67                 .doReturn(successFuture)
68                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
69
70         yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
71     }
72
73     @Test
74     public void testIgnoreNonVisibleData() {
75         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
76                 false, 60000L);
77         final MapNode emptyList = ImmutableNodes.mapNodeBuilder(NETCONF_FILTER_QNAME).build();
78         tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
79         tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
80
81         verify(rpc, atMost(1)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
82     }
83
84     @Test
85     public void testDiscardChanges() {
86         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
87                 false, 60000L);
88         final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
89         try {
90             submitFuture.checkedGet();
91         } catch (final TransactionCommitFailedException e) {
92             // verify discard changes was sent
93             final InOrder inOrder = inOrder(rpc);
94             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
95             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME), NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
96             inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME)), any(NormalizedNode.class));
97             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
98             return;
99         }
100
101         fail("Submit should fail");
102     }
103
104     @Test
105     public void testFailedCommit() throws Exception {
106         final CheckedFuture<DefaultDOMRpcResult, Exception> rpcErrorFuture =
107                 Futures.immediateCheckedFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "a", "m")));
108
109         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null))))
110         .doReturn(rpcErrorFuture).when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
111
112         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
113                 false, 60000L);
114
115         final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
116         try {
117             submitFuture.checkedGet();
118         } catch (final TransactionCommitFailedException e) {
119             return;
120         }
121
122         fail("Submit should fail");
123     }
124
125     @Test
126     public void testDiscardChangesNotSentWithoutCandidate() {
127         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null))))
128                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
129                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
130
131         final WriteRunningTx tx = new WriteRunningTx(id, new NetconfBaseOps(rpc, NetconfDevice.INIT_SCHEMA_CTX),
132                 false, 60000L);
133         try {
134             tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
135         } catch (final Exception e) {
136             // verify discard changes was sent
137             final InOrder inOrder = inOrder(rpc);
138             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
139             inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any(NormalizedNode.class));
140             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
141             return;
142         }
143
144         fail("Delete should fail");
145     }
146
147 }