2 * Copyright (c) 2016 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.genius.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.opendaylight.genius.mdsalutil.actions.ActionOutput;
22 import org.powermock.api.mockito.PowerMockito;
23 import org.powermock.core.classloader.annotations.PrepareForTest;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.genius.mdsalutil.ActionInfo;
29 import org.opendaylight.genius.mdsalutil.ActionType;
30 import org.opendaylight.genius.mdsalutil.FlowEntity;
31 import org.opendaylight.genius.mdsalutil.GroupEntity;
32 import org.opendaylight.genius.mdsalutil.BucketInfo;
33 import org.opendaylight.genius.mdsalutil.InstructionInfo;
34 import org.opendaylight.genius.mdsalutil.InstructionType;
35 import org.opendaylight.genius.mdsalutil.MDSALUtil;
36 import org.opendaylight.genius.mdsalutil.MatchFieldType;
37 import org.opendaylight.genius.mdsalutil.MatchInfo;
38 import org.opendaylight.genius.mdsalutil.internal.MDSALManager;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import static org.junit.Assert.assertEquals;
58 @RunWith(MockitoJUnitRunner.class)
59 //@RunWith(PowerMockRunner.class)
60 @PrepareForTest(MDSALUtil.class)
61 public class MdSalUtilTest extends AbstractDataBrokerTest {
62 DataBroker dataBroker;
63 @Mock PacketProcessingService ppS ;
64 MDSALManager mdSalMgr = null ;
65 MockFlowForwarder flowFwder = null ;
66 MockGroupForwarder grpFwder = null ;
67 private static final String Nodeid = "openflow:1";
70 public void setUp() throws Exception {
71 dataBroker = getDataBroker() ;
72 mdSalMgr = new MDSALManager( dataBroker, ppS);
73 flowFwder = new MockFlowForwarder( dataBroker );
74 grpFwder = new MockGroupForwarder( dataBroker ) ;
76 PowerMockito.mockStatic(MDSALUtil.class) ;
78 NodeKey s1Key = new NodeKey(new NodeId("openflow:1"));
79 addFlowCapableNode(s1Key);
83 public void testInstallFlow() {
84 String dpnId = "openflow:1";
85 String tableId1 = "12";
88 FlowEntity testFlow1 = createFlowEntity(dpnId, tableId1) ;
89 mdSalMgr.installFlow(testFlow1);
90 assertEquals(1, flowFwder.getDataChgCount());
93 String tableId2 = "13" ;
94 FlowEntity testFlow2 = createFlowEntity(dpnId, tableId2) ;
95 mdSalMgr.installFlow(testFlow2);
96 assertEquals(2, flowFwder.getDataChgCount());
100 public void testRemoveFlow() {
101 String dpnId = "openflow:1";
102 String tableId = "13" ;
103 FlowEntity testFlow = createFlowEntity(dpnId, tableId) ;
105 // To test RemoveFlow add and then delete Flows
106 mdSalMgr.installFlow(testFlow) ;
107 assertEquals(1, flowFwder.getDataChgCount());
108 mdSalMgr.removeFlow(testFlow);
109 assertEquals(0, flowFwder.getDataChgCount());
113 public void testInstallGroup() {
115 String inport = "2" ;
117 GroupEntity grpEntity1 = createGroupEntity(Nodeid, inport, vlanid) ;
119 mdSalMgr.installGroup(grpEntity1);
120 assertEquals(1, grpFwder.getDataChgCount());
125 GroupEntity grpEntity2 = createGroupEntity(Nodeid, inport, vlanid) ;
126 mdSalMgr.installGroup(grpEntity2);
127 assertEquals(2, grpFwder.getDataChgCount());
131 public void testRemoveGroup() {
132 String inport = "2" ;
134 GroupEntity grpEntity = createGroupEntity(Nodeid, inport, vlanid) ;
135 // To test RemoveGroup add and then delete Group
136 mdSalMgr.installGroup(grpEntity);
137 assertEquals(1, grpFwder.getDataChgCount());
138 mdSalMgr.removeGroup(grpEntity);
139 assertEquals(0, grpFwder.getDataChgCount());
142 public void addFlowCapableNode(NodeKey nodeKey) throws ExecutionException, InterruptedException {
143 Nodes nodes = new NodesBuilder().setNode(Collections.emptyList()).build();
144 InstanceIdentifier<Node> flowNodeIdentifier = InstanceIdentifier.create(Nodes.class)
145 .child(Node.class, nodeKey);
147 FlowCapableNodeBuilder fcnBuilder = new FlowCapableNodeBuilder();
148 NodeBuilder nodeBuilder = new NodeBuilder();
149 nodeBuilder.setKey(nodeKey);
150 nodeBuilder.addAugmentation(FlowCapableNode.class, fcnBuilder.build());
152 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
153 writeTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodes);
154 writeTx.put(LogicalDatastoreType.OPERATIONAL, flowNodeIdentifier, nodeBuilder.build());
155 writeTx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Nodes.class), nodes);
156 writeTx.put(LogicalDatastoreType.CONFIGURATION, flowNodeIdentifier, nodeBuilder.build());
157 assertCommit(writeTx.submit());
160 // Methods to test the install Flow and Group
162 public FlowEntity createFlowEntity(String dpnId, String tableId) {
166 FlowEntity terminatingServiceTableFlowEntity = null;
168 List<ActionInfo> listActionInfo = new ArrayList<>();
169 listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
173 dpId = new BigInteger(dpnId.split(":")[1]);
175 List<MatchInfo> mkMatches = new ArrayList<>();
176 BigInteger COOKIE = new BigInteger("9000000", 16);
178 short s_tableId = Short.parseShort(tableId) ;
180 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
181 new BigInteger("0000000000000000", 16) }));
183 List<InstructionInfo> mkInstructions = new ArrayList<>();
184 mkInstructions.add(new InstructionInfo(InstructionType.write_actions,
187 terminatingServiceTableFlowEntity = MDSALUtil
191 getFlowRef(s_tableId,
192 SERVICE_ID), 5, "Terminating Service Flow Entry: " + SERVICE_ID,
194 .add(BigInteger.valueOf(SERVICE_ID)),
196 } catch (Exception e) {
197 //throw new Exception(e) ;
200 return terminatingServiceTableFlowEntity;
203 private String getFlowRef(short termSvcTable, int svcId) {
204 return String.valueOf(termSvcTable) + svcId;
207 public GroupEntity createGroupEntity(String Nodeid, String inport, int vlanid) {
208 GroupEntity groupEntity;
209 long id = getUniqueValue(Nodeid, inport);
210 List<BucketInfo> listBucketInfo = new ArrayList<>();
211 List<ActionInfo> listActionInfo = new ArrayList<>();
213 listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] { null }));
214 listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid, new String[] { String.valueOf(vlanid) }));
216 listActionInfo.add(new ActionOutput(Long.parseLong(inport), 65535));
217 listBucketInfo.add(new BucketInfo(listActionInfo));
219 String groupName = "Test Group";
220 BigInteger dpnId = new BigInteger(Nodeid.split(":")[1]);
221 groupEntity = MDSALUtil.buildGroupEntity(dpnId, id, groupName, GroupTypes.GroupIndirect,
227 private static long getUniqueValue(String nodeId, String inport) {
229 Long nodeIdL = Long.valueOf(nodeId.split(":")[1]);
230 Long inportL = Long.valueOf(inport);
232 sd_set = nodeIdL * 10 + inportL;