2 * Copyright (c) 2014 Pacnet 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.openflowplugin.applications.lldpspeaker;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.verifyNoInteractions;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.DELETE;
15 import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.SUBTREE_MODIFIED;
16 import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.WRITE;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
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.DataTreeIdentifier;
28 import org.opendaylight.mdsal.binding.api.DataTreeModification;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 * Tests for {@link NodeConnectorInventoryEventTranslator}.
38 @RunWith(MockitoJUnitRunner.class)
39 public class NodeConnectorInventoryEventTranslatorTest {
40 private static final InstanceIdentifier<NodeConnector> ID = TestUtils
41 .createNodeConnectorId("openflow:1", "openflow:1:1");
42 private static final InstanceIdentifier<FlowCapableNodeConnector> NODE_CONNECTOR_INSTANCE_IDENTIFIER = ID
43 .augmentation(FlowCapableNodeConnector.class);
44 private static final FlowCapableNodeConnector FLOW_CAPABLE_NODE_CONNECTOR = TestUtils
45 .createFlowCapableNodeConnector().build();
48 private NodeConnectorEventsObserver eventsObserver;
50 private NodeConnectorEventsObserver eventsObserver2;
52 private NodeConnectorInventoryEventTranslator translator;
56 translator = new NodeConnectorInventoryEventTranslator(mock(DataBroker.class), eventsObserver, eventsObserver2);
60 * Test that checks if {@link NodeConnectorEventsObserver#nodeConnectorAdded} is called
61 * for each FlowCapableNodeConnector item added in
62 * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}.
65 public void testNodeConnectorCreation() {
66 DataTreeModification dataTreeModification = setupDataTreeChange(WRITE, NODE_CONNECTOR_INSTANCE_IDENTIFIER,
67 FLOW_CAPABLE_NODE_CONNECTOR);
68 translator.onDataTreeChanged(List.of(dataTreeModification));
69 verify(eventsObserver).nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
73 * Test that checks that nothing is called when port appeared in inventory in link down state.
76 public void testNodeConnectorCreationLinkDown() {
77 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(true, false).build();
78 DataTreeModification dataTreeModification = setupDataTreeChange(WRITE, ID, fcnc);
79 translator.onDataTreeChanged(List.of(dataTreeModification));
80 verifyNoInteractions(eventsObserver);
84 * Test that checks that nothing is called when port appeared in inventory in admin down state.
87 public void testNodeConnectorCreationAdminDown() {
88 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(false, true).build();
89 DataTreeModification dataTreeModification = setupDataTreeChange(WRITE, ID, fcnc);
90 translator.onDataTreeChanged(List.of(dataTreeModification));
91 verifyNoInteractions(eventsObserver);
95 * Test that checks if {@link NodeConnectorEventsObserver#nodeConnectorRemoved} is called
96 * for each FlowCapableNodeConnector item that have link down state removed in
97 * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}.
100 public void testNodeConnectorUpdateToLinkDown() {
101 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(true, false).build();
102 DataTreeModification dataTreeModification = setupDataTreeChange(SUBTREE_MODIFIED,
103 NODE_CONNECTOR_INSTANCE_IDENTIFIER, fcnc);
104 translator.onDataTreeChanged(List.of(dataTreeModification));
105 verify(eventsObserver).nodeConnectorRemoved(ID);
109 * Test that checks if {@link NodeConnectorEventsObserver#nodeConnectorRemoved} is called
110 * for each FlowCapableNodeConnector item with administrative down state removed in
111 * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}.
114 public void testNodeConnectorUpdateToAdminDown() {
115 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(false, true).build();
116 DataTreeModification dataTreeModification = setupDataTreeChange(SUBTREE_MODIFIED,
117 NODE_CONNECTOR_INSTANCE_IDENTIFIER, fcnc);
118 translator.onDataTreeChanged(List.of(dataTreeModification));
119 verify(eventsObserver).nodeConnectorRemoved(ID);
123 * Test that checks if {@link NodeConnectorEventsObserver#nodeConnectorAdded} is called
124 * for each FlowCapableNodeConnector item with administrative up and link up state added in
125 * {@link org.opendaylight.md}.
128 public void testNodeConnectorUpdateToUp() {
129 DataTreeModification dataTreeModification = setupDataTreeChange(SUBTREE_MODIFIED,
130 NODE_CONNECTOR_INSTANCE_IDENTIFIER,
131 FLOW_CAPABLE_NODE_CONNECTOR);
132 translator.onDataTreeChanged(List.of(dataTreeModification));
133 verify(eventsObserver).nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
137 * Test that checks if {@link NodeConnectorEventsObserver#nodeConnectorRemoved} is called
138 * for each FlowCapableNodeConnector path that
139 * {@link org.opendaylight.mdsal.binding.api.DataTreeModification} return.
142 public void testNodeConnectorRemoval() {
143 DataTreeModification dataTreeModification = setupDataTreeChange(DELETE, NODE_CONNECTOR_INSTANCE_IDENTIFIER,
145 // Invoke NodeConnectorInventoryEventTranslator and check result
146 translator.onDataTreeChanged(List.of(dataTreeModification));
147 verify(eventsObserver).nodeConnectorRemoved(ID);
151 * Test that if {@link NodeConnectorEventsObserver#nodeConnectorAdded} and.
152 * @{NodeConnectorEventsObserver#nodeConnectorRemoved} are called for each observer when multiple
153 * observers are registered for notifications.
156 public void testMultipleObserversNotified() {
157 // Create prerequisites
158 InstanceIdentifier<NodeConnector> id2 = TestUtils.createNodeConnectorId("openflow:1", "openflow:1:2");
159 InstanceIdentifier<FlowCapableNodeConnector> iiToConnector2 = id2.augmentation(FlowCapableNodeConnector.class);
160 // Invoke onDataTreeChanged and check that both observers notified
161 translator.onDataTreeChanged(List.of(
162 setupDataTreeChange(WRITE, NODE_CONNECTOR_INSTANCE_IDENTIFIER, FLOW_CAPABLE_NODE_CONNECTOR),
163 setupDataTreeChange(DELETE, iiToConnector2, null)));
164 verify(eventsObserver).nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
165 verify(eventsObserver).nodeConnectorRemoved(id2);
166 verify(eventsObserver2).nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
167 verify(eventsObserver2).nodeConnectorRemoved(id2);
171 public void tearDown() {
175 private static <T extends DataObject> DataTreeModification setupDataTreeChange(final ModificationType type,
176 final InstanceIdentifier<T> ii, final FlowCapableNodeConnector connector) {
177 final DataTreeModification dataTreeModification = mock(DataTreeModification.class);
178 when(dataTreeModification.getRootNode()).thenReturn(mock(DataObjectModification.class));
179 DataTreeIdentifier<T> identifier = DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, ii);
180 when(dataTreeModification.getRootNode().modificationType()).thenReturn(type);
181 when(dataTreeModification.getRootPath()).thenReturn(identifier);
182 when(dataTreeModification.getRootNode().dataAfter()).thenReturn(connector);
183 return dataTreeModification;