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