Prevent NPE for Credentials
[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.jupiter.api.Assertions.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.jupiter.api.AfterAll;
17 import org.junit.jupiter.api.BeforeAll;
18 import org.junit.jupiter.api.BeforeEach;
19 import org.junit.jupiter.api.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.client.mdsal.NetconfDeviceCapabilities;
28 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.ConnectionOper.ConnectionStatus;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.LoginPwUnencryptedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev240611.NetconfNode;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.augment.test.rev160808.Node1;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
48
49 // FIXME: base on AbstractDataBrokerTest test?
50 class NetconfDeficeTopologyAdapterIntegrationTest {
51     private static final RemoteDeviceId ID = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
52     private static final KeyedInstanceIdentifier<Topology, TopologyKey> TEST_TOPOLOGY_ID =
53         // FIXME: do not use this constant
54         NetconfNodeUtils.DEFAULT_TOPOLOGY_IID;
55
56     private static BindingRuntimeContext RUNTIME_CONTEXT;
57
58     private DataBroker dataBroker;
59     private DOMDataBroker domDataBroker;
60
61     private NetconfDeviceTopologyAdapter adapter;
62
63     @BeforeAll
64     static void beforeClass() {
65         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(NetconfNode.class, Node1.class);
66     }
67
68     @AfterAll
69     static void afterClass() {
70         RUNTIME_CONTEXT = null;
71     }
72
73     @BeforeEach
74     void setUp() throws Exception {
75         final var customizer = new ConcurrentDataBrokerTestCustomizer(true);
76         domDataBroker = customizer.getDOMDataBroker();
77         dataBroker = customizer.createDataBroker();
78         customizer.updateSchema(RUNTIME_CONTEXT);
79
80         final var tx = dataBroker.newWriteOnlyTransaction();
81         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_TOPOLOGY_ID, new TopologyBuilder()
82             .withKey(TEST_TOPOLOGY_ID.getKey())
83             .build());
84         tx.commit().get(2, TimeUnit.SECONDS);
85
86         adapter = new NetconfDeviceTopologyAdapter(dataBroker, TEST_TOPOLOGY_ID, ID, new LoginPwUnencryptedBuilder()
87             .setLoginPasswordUnencrypted(new LoginPasswordUnencryptedBuilder()
88                 .setUsername("netconf")
89                 .setPassword("netconf")
90                 .build())
91             .build());
92     }
93
94     @Test
95     void testFailedDeviceIntegration() {
96         adapter.setDeviceAsFailed(null);
97
98         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> dataBroker.newReadWriteTransaction()
99             .read(LogicalDatastoreType.OPERATIONAL, TEST_TOPOLOGY_ID
100                 .child(Node.class, new NodeKey(new NodeId(ID.name())))
101                 .augmentation(NetconfNode.class))
102             .get(5, TimeUnit.SECONDS)
103             .filter(conn -> conn.getConnectionStatus() == ConnectionStatus.UnableToConnect)
104             .isPresent());
105     }
106
107     @Test
108     void testDeviceAugmentedNodePresence() throws Exception {
109         QName netconfTestLeafQname = QName.create(
110                 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
111
112         YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
113                 .node(Topology.QNAME)
114                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
115                 .node(Node.QNAME)
116                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
117                 .node(netconfTestLeafQname).build();
118
119         final Integer dataTestId = 474747;
120         final var augmentNode = ImmutableNodes.leafNode(netconfTestLeafQname, dataTestId);
121
122         DOMDataTreeWriteTransaction wtx =  domDataBroker.newWriteOnlyTransaction();
123         wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
124         wtx.commit().get(5, TimeUnit.SECONDS);
125
126         adapter.updateDeviceData(true, NetconfDeviceCapabilities.empty(), new SessionIdType(Uint32.ONE));
127
128         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
129             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
130             .get(2, TimeUnit.SECONDS)
131             .map(NormalizedNode::body));
132
133         adapter.setDeviceAsFailed(null);
134
135         assertEquals(Optional.of(dataTestId), domDataBroker.newReadOnlyTransaction()
136             .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf)
137             .get(2, TimeUnit.SECONDS)
138             .map(NormalizedNode::body));
139     }
140 }