2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.vpnservice.test;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Collections;
14 import java.util.concurrent.ExecutionException;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.powermock.api.mockito.PowerMockito;
22 import org.powermock.core.classloader.annotations.PrepareForTest;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
28 import org.opendaylight.vpnservice.mdsalutil.ActionType;
29 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
30 import org.opendaylight.vpnservice.mdsalutil.GroupEntity;
31 import org.opendaylight.vpnservice.mdsalutil.BucketInfo;
32 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
33 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
34 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
35 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
36 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
37 import org.opendaylight.vpnservice.mdsalutil.internal.MDSALManager;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import static org.junit.Assert.assertEquals;
57 @RunWith(MockitoJUnitRunner.class)
58 //@RunWith(PowerMockRunner.class)
59 @PrepareForTest(MDSALUtil.class)
60 public class MdSalUtilTest extends AbstractDataBrokerTest {
61 DataBroker dataBroker;
62 @Mock PacketProcessingService ppS ;
63 MDSALManager mdSalMgr = null ;
64 MockFlowForwarder flowFwder = null ;
65 MockGroupForwarder grpFwder = null ;
66 private static final String Nodeid = "openflow:1";
69 public void setUp() throws Exception {
70 dataBroker = getDataBroker() ;
71 mdSalMgr = new MDSALManager( dataBroker, ppS);
72 flowFwder = new MockFlowForwarder( dataBroker );
73 grpFwder = new MockGroupForwarder( dataBroker ) ;
75 PowerMockito.mockStatic(MDSALUtil.class) ;
77 NodeKey s1Key = new NodeKey(new NodeId("openflow:1"));
78 addFlowCapableNode(s1Key);
82 public void testInstallFlow() {
83 String dpnId = "openflow:1";
84 String tableId1 = "12";
87 FlowEntity testFlow1 = createFlowEntity(dpnId, tableId1) ;
88 mdSalMgr.installFlow(testFlow1);
89 assertEquals(1, flowFwder.getDataChgCount());
92 String tableId2 = "13" ;
93 FlowEntity testFlow2 = createFlowEntity(dpnId, tableId2) ;
94 mdSalMgr.installFlow(testFlow2);
95 assertEquals(2, flowFwder.getDataChgCount());
99 public void testRemoveFlow() {
100 String dpnId = "openflow:1";
101 String tableId = "13" ;
102 FlowEntity testFlow = createFlowEntity(dpnId, tableId) ;
104 // To test RemoveFlow add and then delete Flows
105 mdSalMgr.installFlow(testFlow) ;
106 assertEquals(1, flowFwder.getDataChgCount());
107 mdSalMgr.removeFlow(testFlow);
108 assertEquals(0, flowFwder.getDataChgCount());
112 public void testInstallGroup() {
114 String inport = "2" ;
116 GroupEntity grpEntity1 = createGroupEntity(Nodeid, inport, vlanid) ;
118 mdSalMgr.installGroup(grpEntity1);
119 assertEquals(1, grpFwder.getDataChgCount());
124 GroupEntity grpEntity2 = createGroupEntity(Nodeid, inport, vlanid) ;
125 mdSalMgr.installGroup(grpEntity2);
126 assertEquals(2, grpFwder.getDataChgCount());
130 public void testRemoveGroup() {
131 String inport = "2" ;
133 GroupEntity grpEntity = createGroupEntity(Nodeid, inport, vlanid) ;
134 // To test RemoveGroup add and then delete Group
135 mdSalMgr.installGroup(grpEntity);
136 assertEquals(1, grpFwder.getDataChgCount());
137 mdSalMgr.removeGroup(grpEntity);
138 assertEquals(0, grpFwder.getDataChgCount());
141 public void addFlowCapableNode(NodeKey nodeKey) throws ExecutionException, InterruptedException {
142 Nodes nodes = new NodesBuilder().setNode(Collections.<Node>emptyList()).build();
143 InstanceIdentifier<Node> flowNodeIdentifier = InstanceIdentifier.create(Nodes.class)
144 .child(Node.class, nodeKey);
146 FlowCapableNodeBuilder fcnBuilder = new FlowCapableNodeBuilder();
147 NodeBuilder nodeBuilder = new NodeBuilder();
148 nodeBuilder.setKey(nodeKey);
149 nodeBuilder.addAugmentation(FlowCapableNode.class, fcnBuilder.build());
151 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
152 writeTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodes);
153 writeTx.put(LogicalDatastoreType.OPERATIONAL, flowNodeIdentifier, nodeBuilder.build());
154 writeTx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Nodes.class), nodes);
155 writeTx.put(LogicalDatastoreType.CONFIGURATION, flowNodeIdentifier, nodeBuilder.build());
156 assertCommit(writeTx.submit());
159 // Methods to test the install Flow and Group
161 public FlowEntity createFlowEntity(String dpnId, String tableId) {
165 FlowEntity terminatingServiceTableFlowEntity = null;
167 List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
168 listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
172 dpId = new BigInteger(dpnId.split(":")[1]);
174 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
175 BigInteger COOKIE = new BigInteger("9000000", 16);
177 short s_tableId = Short.parseShort(tableId) ;
179 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
180 new BigInteger("0000000000000000", 16) }));
182 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
183 mkInstructions.add(new InstructionInfo(InstructionType.write_actions,
186 terminatingServiceTableFlowEntity = MDSALUtil
190 getFlowRef(s_tableId,
191 SERVICE_ID), 5, "Terminating Service Flow Entry: " + SERVICE_ID,
193 .add(BigInteger.valueOf(SERVICE_ID)),
195 } catch (Exception e) {
196 //throw new Exception(e) ;
199 return terminatingServiceTableFlowEntity;
202 private String getFlowRef(short termSvcTable, int svcId) {
203 return new StringBuffer().append(termSvcTable).append(svcId).toString();
206 public GroupEntity createGroupEntity(String Nodeid, String inport, int vlanid) {
207 GroupEntity groupEntity;
208 long id = getUniqueValue(Nodeid, inport);
209 List<BucketInfo> listBucketInfo = new ArrayList<BucketInfo>();
210 List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
212 listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] { null }));
213 listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid, new String[] { String.valueOf(vlanid) }));
215 listActionInfo.add(new ActionInfo(ActionType.output, new String[] { inport, "65535" }));
216 listBucketInfo.add(new BucketInfo(listActionInfo));
218 String groupName = "Test Group";
219 BigInteger dpnId = new BigInteger(Nodeid.split(":")[1]);
220 groupEntity = MDSALUtil.buildGroupEntity(dpnId, id, groupName, GroupTypes.GroupIndirect,
226 private static long getUniqueValue(String nodeId, String inport) {
228 Long nodeIdL = Long.valueOf(nodeId.split(":")[1]);
229 Long inportL = Long.valueOf(inport);
231 sd_set = nodeIdL * 10 + inportL;