2 * Copyright (c) 2016 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
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
14 import com.google.common.collect.Lists;
15 import java.net.InetSocketAddress;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
26 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
28 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class MountInstanceTest {
38 private static final Logger LOG = LoggerFactory.getLogger(MountInstanceTest.class);
40 private static SchemaContext SCHEMA_CONTEXT;
43 private DOMMountPointService service;
45 private DOMDataBroker broker;
47 private DOMRpcService rpcService;
49 private NetconfDeviceNotificationService notificationService;
51 private DOMMountPointService.DOMMountPointBuilder mountPointBuilder;
53 private ObjectRegistration<DOMMountPoint> registration;
55 private DOMNotification notification;
57 private NetconfDeviceSalProvider.MountInstance mountInstance;
60 public static void suiteSetUp() throws Exception {
61 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
62 moduleInfoBackedContext.addModuleInfos(Lists.newArrayList(org.opendaylight.yang.gen
63 .v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance()));
64 SCHEMA_CONTEXT = moduleInfoBackedContext.tryToCreateSchemaContext().get();
69 public void setUp() throws Exception {
70 MockitoAnnotations.initMocks(this);
71 when(service.createMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountPointBuilder);
73 when(mountPointBuilder.register()).thenReturn(registration);
74 mountInstance = new NetconfDeviceSalProvider.MountInstance(
75 service, new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830)));
80 public void testOnTopologyDeviceConnected() throws Exception {
81 mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
82 verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
83 verify(mountPointBuilder).addService(DOMDataBroker.class, broker);
84 verify(mountPointBuilder).addService(DOMRpcService.class, rpcService);
85 verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
89 public void testOnTopologyDeviceDisconnected() throws Exception {
90 mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
91 mountInstance.onTopologyDeviceDisconnected();
92 verify(registration).close();
94 mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
95 } catch (final IllegalStateException e) {
96 LOG.warn("Operation failed.", e);
97 Assert.fail("Topology registration still present after disconnect ");
102 public void testClose() throws Exception {
103 mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
104 mountInstance.close();
105 verify(registration).close();
109 public void testPublishNotification() throws Exception {
110 mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
111 verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
112 verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
113 mountInstance.publish(notification);
114 verify(notificationService).publishNotification(notification);