1 package org.opendaylight.vpnservice.test;
3 import java.math.BigInteger;
4 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.concurrent.ExecutionException;
8 import org.junit.Before;
10 import org.junit.runner.RunWith;
11 import org.mockito.Mock;
13 import org.mockito.runners.MockitoJUnitRunner;
14 import org.powermock.api.mockito.PowerMockito;
15 import org.powermock.core.classloader.annotations.PrepareForTest;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
21 import org.opendaylight.vpnservice.mdsalutil.ActionType;
22 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
23 import org.opendaylight.vpnservice.mdsalutil.GroupEntity;
24 import org.opendaylight.vpnservice.mdsalutil.BucketInfo;
25 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
26 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
27 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
28 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
29 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
30 import org.opendaylight.vpnservice.mdsalutil.internal.MDSALManager;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import static org.junit.Assert.assertEquals;
50 @RunWith(MockitoJUnitRunner.class)
51 //@RunWith(PowerMockRunner.class)
52 @PrepareForTest(MDSALUtil.class)
53 public class MdSalUtilTest extends AbstractDataBrokerTest {
54 DataBroker dataBroker;
55 @Mock PacketProcessingService ppS ;
56 MDSALManager mdSalMgr = null ;
57 MockFlowForwarder flowFwder = null ;
58 MockGroupForwarder grpFwder = null ;
61 public void setUp() throws Exception {
62 dataBroker = getDataBroker() ;
63 mdSalMgr = new MDSALManager( dataBroker, ppS);
64 flowFwder = new MockFlowForwarder( dataBroker );
65 grpFwder = new MockGroupForwarder( dataBroker ) ;
67 PowerMockito.mockStatic(MDSALUtil.class) ;
69 NodeKey s1Key = new NodeKey(new NodeId("openflow:1"));
70 addFlowCapableNode(s1Key);
74 public void testInstallFlow() {
75 String dpnId = "openflow:1";
76 String tableId1 = "12";
79 FlowEntity testFlow1 = createFlowEntity(dpnId, tableId1) ;
80 mdSalMgr.installFlow(testFlow1);
81 assertEquals(1, flowFwder.getDataChgCount());
84 String tableId2 = "13" ;
85 FlowEntity testFlow2 = createFlowEntity(dpnId, tableId2) ;
86 mdSalMgr.installFlow(testFlow2);
87 assertEquals(2, flowFwder.getDataChgCount());
91 public void testRemoveFlow() {
92 String dpnId = "openflow:1";
93 String tableId = "13" ;
94 FlowEntity testFlow = createFlowEntity(dpnId, tableId) ;
96 // To test RemoveFlow add and then delete Flows
97 mdSalMgr.installFlow(testFlow) ;
98 assertEquals(1, flowFwder.getDataChgCount());
99 mdSalMgr.removeFlow(testFlow);
100 assertEquals(0, flowFwder.getDataChgCount());
104 public void testInstallGroup() {
107 String inport = "2" ;
109 GroupEntity grpEntity1 = createGroupEntity(Nodeid, inport, vlanid) ;
111 mdSalMgr.installGroup(grpEntity1);
112 assertEquals(1, grpFwder.getDataChgCount());
118 GroupEntity grpEntity2 = createGroupEntity(Nodeid, inport, vlanid) ;
119 mdSalMgr.installGroup(grpEntity2);
120 assertEquals(2, grpFwder.getDataChgCount());
124 public void testRemoveGroup() {
126 String inport = "2" ;
128 GroupEntity grpEntity = createGroupEntity(Nodeid, inport, vlanid) ;
129 // To test RemoveGroup add and then delete Group
130 mdSalMgr.installGroup(grpEntity);
131 assertEquals(1, grpFwder.getDataChgCount());
132 mdSalMgr.removeGroup(grpEntity);
133 assertEquals(0, grpFwder.getDataChgCount());
136 public void addFlowCapableNode(NodeKey nodeKey) throws ExecutionException, InterruptedException {
137 Nodes nodes = new NodesBuilder().setNode(Collections.<Node>emptyList()).build();
138 InstanceIdentifier<Node> flowNodeIdentifier = InstanceIdentifier.create(Nodes.class)
139 .child(Node.class, nodeKey);
141 FlowCapableNodeBuilder fcnBuilder = new FlowCapableNodeBuilder();
142 NodeBuilder nodeBuilder = new NodeBuilder();
143 nodeBuilder.setKey(nodeKey);
144 nodeBuilder.addAugmentation(FlowCapableNode.class, fcnBuilder.build());
146 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
147 writeTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodes);
148 writeTx.put(LogicalDatastoreType.OPERATIONAL, flowNodeIdentifier, nodeBuilder.build());
149 writeTx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Nodes.class), nodes);
150 writeTx.put(LogicalDatastoreType.CONFIGURATION, flowNodeIdentifier, nodeBuilder.build());
151 assertCommit(writeTx.submit());
154 // Methods to test the install Flow and Group
156 public FlowEntity createFlowEntity(String dpnId, String tableId) {
160 FlowEntity terminatingServiceTableFlowEntity = null;
162 List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
163 listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
167 dpId = Long.parseLong(dpnId.split(":")[1]);
169 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
170 BigInteger COOKIE = new BigInteger("9000000", 16);
172 short s_tableId = Short.parseShort(tableId) ;
174 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
175 new BigInteger("0000000000000000", 16) }));
177 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
178 mkInstructions.add(new InstructionInfo(InstructionType.write_actions,
181 terminatingServiceTableFlowEntity = MDSALUtil
185 getFlowRef(s_tableId,
186 SERVICE_ID), 5, "Terminating Service Flow Entry: " + SERVICE_ID,
188 .add(BigInteger.valueOf(SERVICE_ID)),
190 } catch (Exception e) {
191 //throw new Exception(e) ;
194 return terminatingServiceTableFlowEntity;
197 private String getFlowRef(short termSvcTable, int svcId) {
198 return new StringBuffer().append(termSvcTable).append(svcId).toString();
201 public GroupEntity createGroupEntity(String Nodeid, String inport, int vlanid) {
202 GroupEntity groupEntity;
203 long id = getUniqueValue(Nodeid, inport);
204 List<BucketInfo> listBucketInfo = new ArrayList<BucketInfo>();
205 List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
207 listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] { null }));
208 listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid, new String[] { String.valueOf(vlanid) }));
210 listActionInfo.add(new ActionInfo(ActionType.output, new String[] { inport, "65535" }));
211 listBucketInfo.add(new BucketInfo(listActionInfo));
213 String groupName = "Test Group";
214 groupEntity = MDSALUtil.buildGroupEntity(Long.valueOf(Nodeid), id, groupName, GroupTypes.GroupIndirect,
220 private static long getUniqueValue(String nodeId, String inport) {
222 Long nodeIdL = Long.valueOf(nodeId);
223 Long inportL = Long.valueOf(inport);
225 sd_set = nodeIdL * 10 + inportL;