8b31a315e536c25a9829bdf0f64e7ee025662ea8
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / 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.sal.connect.netconf.sal;
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.netconf.listener.NetconfDeviceCapabilities;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
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.network.topology.Topology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40
41 // FIXME: base on AbstractDataBrokerTest test?
42 public class NetconfDeficeTopologyAdapterIntegrationTest {
43     private static final RemoteDeviceId ID = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
44
45     private static BindingRuntimeContext RUNTIME_CONTEXT;
46
47     private DataBroker dataBroker;
48     private DOMDataBroker domDataBroker;
49
50     private NetconfDeviceTopologyAdapter adapter;
51
52     @BeforeClass
53     public static void beforeClass() {
54         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(NetconfNode.class, Node1.class);
55     }
56
57     @AfterClass
58     public static void afterClass() {
59         RUNTIME_CONTEXT = null;
60     }
61
62     @Before
63     public void setUp() throws Exception {
64         final var customizer = new ConcurrentDataBrokerTestCustomizer(true);
65         domDataBroker = customizer.getDOMDataBroker();
66         dataBroker = customizer.createDataBroker();
67         customizer.updateSchema(RUNTIME_CONTEXT);
68
69         final var tx = dataBroker.newWriteOnlyTransaction();
70         tx.put(LogicalDatastoreType.OPERATIONAL, RemoteDeviceId.DEFAULT_TOPOLOGY_IID, new TopologyBuilder()
71             .withKey(RemoteDeviceId.DEFAULT_TOPOLOGY_IID.getKey())
72             .build());
73         tx.commit().get(2, TimeUnit.SECONDS);
74
75         adapter = new NetconfDeviceTopologyAdapter(dataBroker, ID);
76     }
77
78     @Test
79     public void testFailedDeviceIntegration() {
80         adapter.setDeviceAsFailed(null);
81
82         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> dataBroker.newReadWriteTransaction()
83             .read(LogicalDatastoreType.OPERATIONAL, ID.getTopologyBindingPath().augmentation(NetconfNode.class))
84             .get(5, TimeUnit.SECONDS)
85             .map(NetconfNodeConnectionStatus::getConnectionStatus)
86             .filter(status -> status == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect)
87             .isPresent());
88     }
89
90     @Test
91     public void testDeviceAugmentedNodePresence() throws Exception {
92         QName netconfTestLeafQname = QName.create(
93                 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
94
95         YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
96                 .node(Topology.QNAME)
97                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
98                 .node(Node.QNAME)
99                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
100                 .node(netconfTestLeafQname).build();
101
102         final Integer dataTestId = 474747;
103         final var augmentNode = ImmutableNodes.leafNode(netconfTestLeafQname, dataTestId);
104
105         DOMDataTreeWriteTransaction wtx =  domDataBroker.newWriteOnlyTransaction();
106         wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
107         wtx.commit().get(5, TimeUnit.SECONDS);
108
109         adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
110
111         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
112             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
113             .get(2, TimeUnit.SECONDS)
114             .map(NormalizedNode::body));
115
116         adapter.setDeviceAsFailed(null);
117
118         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
119             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
120             .get(2, TimeUnit.SECONDS)
121             .map(NormalizedNode::body));
122     }
123 }