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 ;
68 public void setUp() throws Exception {
69 dataBroker = getDataBroker() ;
70 mdSalMgr = new MDSALManager( dataBroker, ppS);
71 flowFwder = new MockFlowForwarder( dataBroker );
72 grpFwder = new MockGroupForwarder( dataBroker ) ;
74 PowerMockito.mockStatic(MDSALUtil.class) ;
76 NodeKey s1Key = new NodeKey(new NodeId("openflow:1"));
77 addFlowCapableNode(s1Key);
81 public void testInstallFlow() {
82 String dpnId = "openflow:1";
83 String tableId1 = "12";
86 FlowEntity testFlow1 = createFlowEntity(dpnId, tableId1) ;
87 mdSalMgr.installFlow(testFlow1);
88 assertEquals(1, flowFwder.getDataChgCount());
91 String tableId2 = "13" ;
92 FlowEntity testFlow2 = createFlowEntity(dpnId, tableId2) ;
93 mdSalMgr.installFlow(testFlow2);
94 assertEquals(2, flowFwder.getDataChgCount());
98 public void testRemoveFlow() {
99 String dpnId = "openflow:1";
100 String tableId = "13" ;
101 FlowEntity testFlow = createFlowEntity(dpnId, tableId) ;
103 // To test RemoveFlow add and then delete Flows
104 mdSalMgr.installFlow(testFlow) ;
105 assertEquals(1, flowFwder.getDataChgCount());
106 mdSalMgr.removeFlow(testFlow);
107 assertEquals(0, flowFwder.getDataChgCount());
111 public void testInstallGroup() {
114 String inport = "2" ;
116 GroupEntity grpEntity1 = createGroupEntity(Nodeid, inport, vlanid) ;
118 mdSalMgr.installGroup(grpEntity1);
119 assertEquals(1, grpFwder.getDataChgCount());
125 GroupEntity grpEntity2 = createGroupEntity(Nodeid, inport, vlanid) ;
126 mdSalMgr.installGroup(grpEntity2);
127 assertEquals(2, grpFwder.getDataChgCount());
131 public void testRemoveGroup() {
133 String inport = "2" ;
135 GroupEntity grpEntity = createGroupEntity(Nodeid, inport, vlanid) ;
136 // To test RemoveGroup add and then delete Group
137 mdSalMgr.installGroup(grpEntity);
138 assertEquals(1, grpFwder.getDataChgCount());
139 mdSalMgr.removeGroup(grpEntity);
140 assertEquals(0, grpFwder.getDataChgCount());
143 public void addFlowCapableNode(NodeKey nodeKey) throws ExecutionException, InterruptedException {
144 Nodes nodes = new NodesBuilder().setNode(Collections.<Node>emptyList()).build();
145 InstanceIdentifier<Node> flowNodeIdentifier = InstanceIdentifier.create(Nodes.class)
146 .child(Node.class, nodeKey);
148 FlowCapableNodeBuilder fcnBuilder = new FlowCapableNodeBuilder();
149 NodeBuilder nodeBuilder = new NodeBuilder();
150 nodeBuilder.setKey(nodeKey);
151 nodeBuilder.addAugmentation(FlowCapableNode.class, fcnBuilder.build());
153 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
154 writeTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodes);
155 writeTx.put(LogicalDatastoreType.OPERATIONAL, flowNodeIdentifier, nodeBuilder.build());
156 writeTx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Nodes.class), nodes);
157 writeTx.put(LogicalDatastoreType.CONFIGURATION, flowNodeIdentifier, nodeBuilder.build());
158 assertCommit(writeTx.submit());
161 // Methods to test the install Flow and Group
163 public FlowEntity createFlowEntity(String dpnId, String tableId) {
167 FlowEntity terminatingServiceTableFlowEntity = null;
169 List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
170 listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
174 dpId = Long.parseLong(dpnId.split(":")[1]);
176 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
177 BigInteger COOKIE = new BigInteger("9000000", 16);
179 short s_tableId = Short.parseShort(tableId) ;
181 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
182 new BigInteger("0000000000000000", 16) }));
184 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
185 mkInstructions.add(new InstructionInfo(InstructionType.write_actions,
188 terminatingServiceTableFlowEntity = MDSALUtil
192 getFlowRef(s_tableId,
193 SERVICE_ID), 5, "Terminating Service Flow Entry: " + SERVICE_ID,
195 .add(BigInteger.valueOf(SERVICE_ID)),
197 } catch (Exception e) {
198 //throw new Exception(e) ;
201 return terminatingServiceTableFlowEntity;
204 private String getFlowRef(short termSvcTable, int svcId) {
205 return new StringBuffer().append(termSvcTable).append(svcId).toString();
208 public GroupEntity createGroupEntity(String Nodeid, String inport, int vlanid) {
209 GroupEntity groupEntity;
210 long id = getUniqueValue(Nodeid, inport);
211 List<BucketInfo> listBucketInfo = new ArrayList<BucketInfo>();
212 List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
214 listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] { null }));
215 listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid, new String[] { String.valueOf(vlanid) }));
217 listActionInfo.add(new ActionInfo(ActionType.output, new String[] { inport, "65535" }));
218 listBucketInfo.add(new BucketInfo(listActionInfo));
220 String groupName = "Test Group";
221 groupEntity = MDSALUtil.buildGroupEntity(Long.valueOf(Nodeid), id, groupName, GroupTypes.GroupIndirect,
227 private static long getUniqueValue(String nodeId, String inport) {
229 Long nodeIdL = Long.valueOf(nodeId);
230 Long inportL = Long.valueOf(inport);
232 sd_set = nodeIdL * 10 + inportL;