Use Files.readString()
[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.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.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             .filter(conn -> conn.getConnectionStatus() == ConnectionStatus.UnableToConnect)
86             .isPresent());
87     }
88
89     @Test
90     public void testDeviceAugmentedNodePresence() throws Exception {
91         QName netconfTestLeafQname = QName.create(
92                 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
93
94         YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
95                 .node(Topology.QNAME)
96                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
97                 .node(Node.QNAME)
98                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
99                 .node(netconfTestLeafQname).build();
100
101         final Integer dataTestId = 474747;
102         final var augmentNode = ImmutableNodes.leafNode(netconfTestLeafQname, dataTestId);
103
104         DOMDataTreeWriteTransaction wtx =  domDataBroker.newWriteOnlyTransaction();
105         wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
106         wtx.commit().get(5, TimeUnit.SECONDS);
107
108         adapter.updateDeviceData(true, NetconfDeviceCapabilities.empty());
109
110         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
111             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
112             .get(2, TimeUnit.SECONDS)
113             .map(NormalizedNode::body));
114
115         adapter.setDeviceAsFailed(null);
116
117         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
118             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
119             .get(2, TimeUnit.SECONDS)
120             .map(NormalizedNode::body));
121     }
122 }