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.server.mdsal.notifications;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.verify;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.DataObjectModification;
26 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
27 import org.opendaylight.mdsal.binding.api.DataTreeModification;
28 import org.opendaylight.netconf.server.api.notifications.BaseNotificationPublisherRegistration;
29 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionEnd;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionStart;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
38 import org.opendaylight.yangtools.concepts.Registration;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class SessionNotificationProducerTest {
45 private static final Logger LOG = LoggerFactory.getLogger(SessionNotificationProducerTest.class);
48 private BaseNotificationPublisherRegistration registration;
50 private Registration listenerRegistration;
52 private NetconfNotificationCollector netconfNotificationCollector;
54 private DataBroker dataBroker;
56 private DataTreeModification<Session> treeChange;
58 private DataObjectModification<Session> changeObject;
60 private SessionNotificationProducer publisher;
64 doReturn(listenerRegistration).when(dataBroker).registerTreeChangeListener(any(), any());
65 doNothing().when(registration).onSessionStarted(any());
66 doNothing().when(registration).onSessionEnded(any());
68 doReturn(registration).when(netconfNotificationCollector).registerBaseNotificationPublisher();
70 publisher = new SessionNotificationProducer(netconfNotificationCollector, dataBroker);
74 public void testOnDataChangedSessionCreated() throws Exception {
75 final var session = createSession(Uint32.ONE);
76 final var treeMod = getTreeModification(session, ModificationType.WRITE);
77 publisher.onDataTreeChanged(List.of(treeMod));
78 final var captor = ArgumentCaptor.forClass(NetconfSessionStart.class);
79 verify(registration).onSessionStarted(captor.capture());
80 final var value = captor.getValue();
81 assertEquals(session.getSessionId(), value.getSessionId().getValue());
82 assertEquals(session.getSourceHost().getIpAddress(), value.getSourceHost());
83 assertEquals(session.getUsername(), value.getUsername());
87 public void testOnDataChangedSessionUpdated() throws Exception {
88 final var sessionBefore = createSessionWithInRpcCount(Uint32.ONE, Uint32.ZERO);
89 final var sessionAfter = createSessionWithInRpcCount(Uint32.ONE, Uint32.ONE);
90 doReturn(sessionBefore).when(changeObject).dataBefore();
91 doReturn(sessionAfter).when(changeObject).dataAfter();
92 doReturn(ModificationType.WRITE).when(changeObject).modificationType();
93 doReturn(changeObject).when(treeChange).getRootNode();
94 publisher.onDataTreeChanged(List.of(treeChange));
95 //session didn't start, only stats changed. No notification should be produced
96 verify(registration, never()).onSessionStarted(any());
97 verify(registration, never()).onSessionEnded(any());
101 public void testOnDataChangedSessionDeleted() throws Exception {
102 final var session = createSession(Uint32.ONE);
103 final var treeMod = getTreeModification(session, ModificationType.DELETE);
104 publisher.onDataTreeChanged(List.of(treeMod));
105 final var captor = ArgumentCaptor.forClass(NetconfSessionEnd.class);
106 verify(registration).onSessionEnded(captor.capture());
107 final NetconfSessionEnd value = captor.getValue();
108 assertEquals(session.getSessionId(), value.getSessionId().getValue());
109 assertEquals(session.getSourceHost().getIpAddress(), value.getSourceHost());
110 assertEquals(session.getUsername(), value.getUsername());
113 private static Session createSession(final Uint32 id) {
114 return createSessionWithInRpcCount(id, Uint32.ZERO);
117 private static Session createSessionWithInRpcCount(final Uint32 id, final Uint32 inRpc) {
118 return new SessionBuilder()
120 .setSourceHost(new Host(new IpAddress(new Ipv4Address("0.0.0.0"))))
122 .setInRpcs(new ZeroBasedCounter32(inRpc))
126 private DataTreeModification<Session> getTreeModification(final Session session, final ModificationType type) {
127 doReturn(type).when(changeObject).modificationType();
128 doReturn(changeObject).when(treeChange).getRootNode();
129 return switch (type) {
131 doReturn(null).when(changeObject).dataBefore();
132 doReturn(session).when(changeObject).dataAfter();
136 doReturn(session).when(changeObject).dataBefore();
140 LOG.debug("Received intentionally unhandled type: {}.", type);