2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.sal.connect.netconf.sal;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.net.InetSocketAddress;
22 import java.util.EnumMap;
23 import java.util.concurrent.TimeUnit;
24 import javassist.ClassPool;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
33 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
34 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
37 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
38 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
39 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
40 import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
41 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
42 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
43 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.DataObjectSerializerGenerator;
44 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
45 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
46 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
47 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
48 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
49 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
50 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
51 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
52 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.common.QName;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
64 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
67 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
69 public class NetconfDeviceTopologyAdapterTest {
71 private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
74 private DataBroker broker;
76 private WriteTransaction writeTx;
78 private BindingTransactionChain txChain;
80 private NetconfNode data;
82 private final String txIdent = "test transaction";
84 private SchemaContext schemaContext = null;
85 private final String sessionIdForReporting = "netconf-test-session1";
87 private BindingTransactionChain transactionChain;
89 private DataBroker dataBroker;
91 private DOMDataBroker domDataBroker;
94 public void setUp() throws Exception {
95 MockitoAnnotations.initMocks(this);
96 doReturn(txChain).when(broker).createTransactionChain(any(TransactionChainListener.class));
97 doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
98 doNothing().when(writeTx)
99 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
100 doNothing().when(writeTx)
101 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
103 doReturn(txIdent).when(writeTx).getIdentifier();
105 this.schemaContext = YangParserTestUtils.parseYangResources(NetconfDeviceTopologyAdapterTest.class,
106 "/schemas/network-topology@2013-10-21.yang", "/schemas/ietf-inet-types@2013-07-15.yang",
107 "/schemas/yang-ext.yang", "/schemas/netconf-node-topology.yang",
108 "/schemas/network-topology-augment-test@2016-08-08.yang");
109 schemaContext.getModules();
110 final DOMSchemaService schemaService = createSchemaService();
112 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
113 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
115 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
116 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
117 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
119 domDataBroker = new SerializedDOMDataBroker(datastores, MoreExecutors.newDirectExecutorService());
121 final ClassPool pool = ClassPool.getDefault();
122 final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(pool));
123 final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
124 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
125 codecRegistry.onBindingRuntimeContextUpdated(
126 BindingRuntimeContext.create(moduleInfoBackedContext, schemaContext));
128 final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
129 final BindingToNormalizedNodeCodec bindingToNormalized =
130 new BindingToNormalizedNodeCodec(loading, codecRegistry);
131 bindingToNormalized.onGlobalContextUpdated(schemaContext);
132 dataBroker = new BindingDOMDataBrokerAdapter(domDataBroker, bindingToNormalized);
134 transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
136 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
137 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
142 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
150 public void testFailedDevice() throws Exception {
152 doReturn(Futures.immediateCheckedFuture(null)).when(writeTx).submit();
153 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
154 adapter.setDeviceAsFailed(null);
156 verify(txChain, times(2)).newWriteOnlyTransaction();
157 verify(writeTx, times(1))
158 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
161 adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
162 adapter.setDeviceAsFailed(null);
164 Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction().read(LogicalDatastoreType.OPERATIONAL,
165 id.getTopologyBindingPath().augmentation(NetconfNode.class)).checkedGet(5, TimeUnit.SECONDS);
167 assertEquals("Netconf node should be presented.", true, netconfNode.isPresent());
168 assertEquals("Connection status should be failed.",
169 NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect.getName(),
170 netconfNode.get().getConnectionStatus().getName());
175 public void testDeviceUpdate() throws Exception {
176 doReturn(Futures.immediateCheckedFuture(null)).when(writeTx).submit();
178 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
179 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
181 verify(txChain, times(2)).newWriteOnlyTransaction();
182 verify(writeTx, times(1))
183 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
184 verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
189 public void testDeviceAugmentedNodePresence() throws Exception {
191 Integer dataTestId = 474747;
193 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
195 QName netconfTestLeafQname = QName.create(
196 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
198 YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
199 .node(Topology.QNAME)
200 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
202 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
203 .node(netconfTestLeafQname).build();
205 NormalizedNode<?, ?> augmentNode = ImmutableLeafNodeBuilder.create().withValue(dataTestId)
206 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(netconfTestLeafQname)).build();
208 DOMDataWriteTransaction wtx = domDataBroker.newWriteOnlyTransaction();
209 wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
210 wtx.submit().get(5, TimeUnit.SECONDS);
212 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
213 Optional<NormalizedNode<?, ?>> testNode = domDataBroker.newReadOnlyTransaction()
214 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).checkedGet(2, TimeUnit.SECONDS);
216 assertEquals("Augmented node data should be still present after device update.", true, testNode.isPresent());
217 assertEquals("Augmented data should be the same as before update node.", dataTestId, testNode.get().getValue());
219 adapter.setDeviceAsFailed(null);
220 testNode = domDataBroker.newReadOnlyTransaction()
221 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).checkedGet(2, TimeUnit.SECONDS);
223 assertEquals("Augmented node data should be still present after device failed.", true, testNode.isPresent());
224 assertEquals("Augmented data should be the same as before failed device.",
225 dataTestId, testNode.get().getValue());
228 private DOMSchemaService createSchemaService() {
229 return new DOMSchemaService() {
232 public SchemaContext getSessionContext() {
233 return schemaContext;
237 public SchemaContext getGlobalContext() {
238 return schemaContext;
242 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
243 final SchemaContextListener listener) {
244 listener.onGlobalContextUpdated(getGlobalContext());
245 return new AbstractListenerRegistration<SchemaContextListener>(listener) {
247 protected void removeRegistration() {
256 public void testRemoveDeviceConfiguration() throws Exception {
257 doReturn(Futures.immediateCheckedFuture(null)).when(writeTx).submit();
259 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
262 verify(txChain, times(2)).newWriteOnlyTransaction();
263 verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath());
264 verify(writeTx, times(2)).submit();