2 * Copyright (c) 2014 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.controller.sample.l2switch.md.flow;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService;
13 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
14 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import static junit.framework.Assert.assertEquals;
27 import static junit.framework.Assert.fail;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
36 public class FlowWriterServiceImplTest {
37 private DataBrokerService dataBrokerService;
38 private NodeConnectorRef srcNodeConnectorRef;
39 private NodeConnectorRef destNodeConnectorRef;
40 private MacAddress destMacAddress;
41 private MacAddress srcMacAddress;
42 private DataModificationTransaction dataModificationTransaction;
43 private NetworkGraphService networkGraphService;
47 dataBrokerService = mock(DataBrokerService.class);
48 networkGraphService = mock(NetworkGraphService.class);
49 //build source node connector ref
50 InstanceIdentifier<Nodes> srcNodesInstanceIdentifier
51 = InstanceIdentifier.builder(Nodes.class)
53 InstanceIdentifier<Node> srcNodeInstanceIdentifier
54 = InstanceIdentifier.builder(srcNodesInstanceIdentifier)
55 .child(Node.class, new NodeKey(new NodeId("openflow:1")))
57 InstanceIdentifier<NodeConnector> srcNodeConnectorInstanceIdentifier
58 = InstanceIdentifier.builder(srcNodeInstanceIdentifier)
59 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:1:2")))
61 srcNodeConnectorRef = new NodeConnectorRef(srcNodeConnectorInstanceIdentifier);
63 //build dest node connector ref
64 InstanceIdentifier<Nodes> nodesInstanceIdentifier
65 = InstanceIdentifier.builder(Nodes.class)
67 InstanceIdentifier<Node> nodeInstanceIdentifier
68 = InstanceIdentifier.builder(nodesInstanceIdentifier)
69 .child(Node.class, new NodeKey(new NodeId("openflow:2")))
71 InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier
72 = InstanceIdentifier.builder(nodeInstanceIdentifier)
73 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:2:2")))
75 destNodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier);
76 destMacAddress = new MacAddress("00:0a:95:9d:68:16");
77 srcMacAddress = new MacAddress("00:0a:95:8c:97:24");
78 dataModificationTransaction = mock(DataModificationTransaction.class);
79 when(dataBrokerService.beginTransaction()).thenReturn(dataModificationTransaction);
83 public void testFlowWriterServiceImpl_NPEWhenDataBrokerServiceIsNull() throws Exception {
85 new FlowWriterServiceImpl(null, networkGraphService);
86 fail("Expected null pointer exception.");
87 } catch(NullPointerException npe) {
88 assertEquals("dataBrokerService should not be null.", npe.getMessage());
93 public void testAddMacToMacFlow_NPEWhenNullSourceMacDestMacAndNodeConnectorRef() throws Exception {
94 FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
96 flowWriterService.addMacToMacFlow(null, null, null);
97 fail("Expected null pointer exception.");
98 } catch(NullPointerException npe) {
99 assertEquals("Destination mac address should not be null.", npe.getMessage());
104 public void testAddMacToMacFlow_NPEWhenSourceMacNullMac() throws Exception {
105 FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
107 flowWriterService.addMacToMacFlow(null, null, destNodeConnectorRef);
108 fail("Expected null pointer exception.");
109 } catch(NullPointerException npe) {
110 assertEquals("Destination mac address should not be null.", npe.getMessage());
115 public void testAddMacToMacFlow_NPEWhenNullSourceMacNodeConnectorRef() throws Exception {
116 FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
118 flowWriterService.addMacToMacFlow(null, destMacAddress, null);
119 fail("Expected null pointer exception.");
120 } catch(NullPointerException npe) {
121 assertEquals("Destination port should not be null.", npe.getMessage());
126 public void testAddMacToMacFlow_WhenNullSourceMac() throws Exception {
127 FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
128 flowWriterService.addMacToMacFlow(null, destMacAddress, destNodeConnectorRef);
129 verify(dataBrokerService, times(1)).beginTransaction();
130 verify(dataModificationTransaction, times(1)).commit();
134 public void testAddMacToMacFlow_WhenSrcAndDestMacAreSame() throws Exception {
135 FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
136 flowWriterService.addMacToMacFlow(new MacAddress(destMacAddress.getValue()), destMacAddress, destNodeConnectorRef);
137 verify(dataBrokerService, never()).beginTransaction();
138 verify(dataModificationTransaction, never()).commit();
143 public void testAddMacToMacFlow_SunnyDay() throws Exception {
144 FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService);
145 flowWriterService.addMacToMacFlow(srcMacAddress, destMacAddress, destNodeConnectorRef);
146 verify(dataBrokerService, times(1)).beginTransaction();
147 verify(dataModificationTransaction, times(1)).commit();