Add session-id to the operational datastore
[netconf.git] / apps / netconf-topology / src / test / java / org / opendaylight / netconf / topology / spi / NetconfDeficeTopologyAdapterIntegrationTest.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.topology.spi;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.net.InetSocketAddress;
13 import java.util.Optional;
14 import java.util.concurrent.TimeUnit;
15 import org.awaitility.Awaitility;
16 import org.junit.AfterClass;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
22 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
23 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
28 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.ConnectionOper.ConnectionStatus;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.augment.test.rev160808.Node1;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.Uint32;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
45
46 // FIXME: base on AbstractDataBrokerTest test?
47 public class NetconfDeficeTopologyAdapterIntegrationTest {
48     private static final RemoteDeviceId ID = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
49     private static final KeyedInstanceIdentifier<Topology, TopologyKey> TEST_TOPOLOGY_ID =
50         // FIXME: do not use this constant
51         NetconfNodeUtils.DEFAULT_TOPOLOGY_IID;
52
53     private static BindingRuntimeContext RUNTIME_CONTEXT;
54
55     private DataBroker dataBroker;
56     private DOMDataBroker domDataBroker;
57
58     private NetconfDeviceTopologyAdapter adapter;
59
60     @BeforeClass
61     public static void beforeClass() {
62         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(NetconfNode.class, Node1.class);
63     }
64
65     @AfterClass
66     public static void afterClass() {
67         RUNTIME_CONTEXT = null;
68     }
69
70     @Before
71     public void setUp() throws Exception {
72         final var customizer = new ConcurrentDataBrokerTestCustomizer(true);
73         domDataBroker = customizer.getDOMDataBroker();
74         dataBroker = customizer.createDataBroker();
75         customizer.updateSchema(RUNTIME_CONTEXT);
76
77         final var tx = dataBroker.newWriteOnlyTransaction();
78         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_TOPOLOGY_ID, new TopologyBuilder()
79             .withKey(TEST_TOPOLOGY_ID.getKey())
80             .build());
81         tx.commit().get(2, TimeUnit.SECONDS);
82
83         adapter = new NetconfDeviceTopologyAdapter(dataBroker, TEST_TOPOLOGY_ID, ID);
84     }
85
86     @Test
87     public void testFailedDeviceIntegration() {
88         adapter.setDeviceAsFailed(null);
89
90         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> dataBroker.newReadWriteTransaction()
91             .read(LogicalDatastoreType.OPERATIONAL, TEST_TOPOLOGY_ID
92                 .child(Node.class, new NodeKey(new NodeId(ID.name())))
93                 .augmentation(NetconfNode.class))
94             .get(5, TimeUnit.SECONDS)
95             .filter(conn -> conn.getConnectionStatus() == ConnectionStatus.UnableToConnect)
96             .isPresent());
97     }
98
99     @Test
100     public void testDeviceAugmentedNodePresence() throws Exception {
101         QName netconfTestLeafQname = QName.create(
102                 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
103
104         YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
105                 .node(Topology.QNAME)
106                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
107                 .node(Node.QNAME)
108                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
109                 .node(netconfTestLeafQname).build();
110
111         final Integer dataTestId = 474747;
112         final var augmentNode = ImmutableNodes.leafNode(netconfTestLeafQname, dataTestId);
113
114         DOMDataTreeWriteTransaction wtx =  domDataBroker.newWriteOnlyTransaction();
115         wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
116         wtx.commit().get(5, TimeUnit.SECONDS);
117
118         adapter.updateDeviceData(true, NetconfDeviceCapabilities.empty(), Uint32.ONE);
119
120         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
121             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
122             .get(2, TimeUnit.SECONDS)
123             .map(NormalizedNode::body));
124
125         adapter.setDeviceAsFailed(null);
126
127         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
128             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
129             .get(2, TimeUnit.SECONDS)
130             .map(NormalizedNode::body));
131     }
132 }