Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / WriteCandidateRunningTxTest.java
1 /*
2  * Copyright (c) 2016 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.netconf.sal.connect.netconf.sal.tx;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TARGET_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
19
20 import com.google.common.util.concurrent.Futures;
21 import java.net.InetSocketAddress;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.dom.api.DOMRpcService;
28 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
29 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
30 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
31 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.copy.config.input.target.ConfigTarget;
33 import org.opendaylight.yangtools.yang.common.Empty;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42
43 public class WriteCandidateRunningTxTest {
44     @Mock
45     private DOMRpcService rpc;
46     private NetconfBaseOps netconfOps;
47     private RemoteDeviceId id;
48
49     @Before
50     public void setUp() throws Exception {
51         MockitoAnnotations.initMocks(this);
52         final SchemaContext schemaContext =
53                 YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
54         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult())).when(rpc).invokeRpc(any(), any());
55         netconfOps = new NetconfBaseOps(rpc, schemaContext);
56         id = new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830));
57     }
58
59     @Test
60     public void testSubmit() throws Exception {
61         final WriteCandidateRunningTx tx = new WriteCandidateRunningTx(id, netconfOps, true);
62         //check, if lock is called
63         final ContainerNode candidateLock =
64                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME);
65         final ContainerNode runningLock =
66                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME);
67         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), runningLock);
68         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), candidateLock);
69         tx.put(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId(), TxTestUtils.getContainerNode());
70         tx.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode());
71         //check, if both edits are called
72         verify(rpc, times(2)).invokeRpc(
73                 eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any());
74         tx.commit().get();
75         //check, if unlock is called
76         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME),
77                 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
78         final ContainerNode candidateUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
79                 NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME);
80         final ContainerNode runningUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
81                 NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME);
82         verify(rpc).invokeRpc(SchemaPath.create(true, NETCONF_UNLOCK_QNAME), candidateUnlock);
83         verify(rpc).invokeRpc(SchemaPath.create(true, NETCONF_UNLOCK_QNAME), runningUnlock);
84     }
85
86     private static ContainerNode getLockContent(final QName op, final QName datastore) {
87         final LeafNode<Object> datastoreLeaf = Builders.leafBuilder().withNodeIdentifier(toId(datastore))
88                 .withValue(Empty.getInstance()).build();
89         final ChoiceNode choice = Builders.choiceBuilder()
90                 .withNodeIdentifier(toId(ConfigTarget.QNAME))
91                 .withChild(datastoreLeaf)
92                 .build();
93         final ContainerNode target = Builders.containerBuilder()
94                 .withNodeIdentifier(toId(NETCONF_TARGET_QNAME))
95                 .withChild(choice).build();
96         return Builders.containerBuilder()
97                 .withNodeIdentifier(toId(op))
98                 .withChild(target)
99                 .build();
100     }
101
102 }