8c804b8d242f650dedf645056022aa00710d0cd6
[vpnservice.git] / mdsalutil / mdsalutil-impl / src / test / java / org / opendaylight / vpnservice / test / MdSalUtilTest.java
1 package org.opendaylight.vpnservice.test;
2
3 import java.math.BigInteger;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Collections;
7 import java.util.concurrent.ExecutionException;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.mockito.Mock;
12
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;
47
48 import static org.junit.Assert.assertEquals;
49
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 ;
59
60      @Before
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 ) ;
66
67             PowerMockito.mockStatic(MDSALUtil.class) ;
68
69             NodeKey s1Key = new NodeKey(new NodeId("openflow:1"));
70             addFlowCapableNode(s1Key);
71         }
72
73         @Test
74         public void testInstallFlow() {
75             String dpnId = "openflow:1";
76             String tableId1 = "12";
77
78             //Install Flow 1
79             FlowEntity testFlow1 = createFlowEntity(dpnId, tableId1) ;
80             mdSalMgr.installFlow(testFlow1);
81             assertEquals(1, flowFwder.getDataChgCount());
82
83             // Install FLow 2
84             String tableId2 = "13" ;
85              FlowEntity testFlow2 = createFlowEntity(dpnId, tableId2) ;
86              mdSalMgr.installFlow(testFlow2);
87              assertEquals(2, flowFwder.getDataChgCount());
88         }
89
90         @Test
91         public void testRemoveFlow() {
92             String dpnId = "openflow:1";
93             String tableId = "13" ;
94             FlowEntity testFlow = createFlowEntity(dpnId, tableId) ;
95
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());
101         }
102
103         @Test
104         public void testInstallGroup() {
105             // Install Group 1
106             String Nodeid = "1";
107             String inport = "2" ;
108             int vlanid = 100 ;
109             GroupEntity grpEntity1 = createGroupEntity(Nodeid, inport, vlanid) ;
110
111              mdSalMgr.installGroup(grpEntity1);
112              assertEquals(1, grpFwder.getDataChgCount());
113
114              // Install Group 2
115                Nodeid = "1";
116                 inport = "3" ;
117                 vlanid = 100 ;
118                 GroupEntity grpEntity2 = createGroupEntity(Nodeid, inport, vlanid) ;
119                 mdSalMgr.installGroup(grpEntity2);
120                 assertEquals(2, grpFwder.getDataChgCount());
121         }
122
123         @Test
124         public void testRemoveGroup() {
125             String Nodeid = "1";
126             String inport = "2" ;
127             int vlanid = 100 ;
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());
134         }
135
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);
140
141             FlowCapableNodeBuilder fcnBuilder = new FlowCapableNodeBuilder();
142             NodeBuilder nodeBuilder = new NodeBuilder();
143             nodeBuilder.setKey(nodeKey);
144             nodeBuilder.addAugmentation(FlowCapableNode.class, fcnBuilder.build());
145
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());
152         }
153
154          // Methods to test the install Flow and Group
155
156         public FlowEntity createFlowEntity(String dpnId, String tableId) {
157
158             long dpId;
159             int SERVICE_ID = 0;
160             FlowEntity terminatingServiceTableFlowEntity = null;
161
162             List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
163             listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
164                     new String[] {}));
165
166             try {
167                 dpId = Long.parseLong(dpnId.split(":")[1]);
168
169                 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
170                 BigInteger COOKIE = new BigInteger("9000000", 16);
171
172                 short s_tableId = Short.parseShort(tableId) ;
173
174                 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
175                         new BigInteger("0000000000000000", 16) }));
176
177                 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
178                 mkInstructions.add(new InstructionInfo(InstructionType.write_actions,
179                         listActionInfo));
180
181                 terminatingServiceTableFlowEntity = MDSALUtil
182                         .buildFlowEntity(
183                                 dpId,
184                                 s_tableId,
185                                 getFlowRef(s_tableId,
186                                         SERVICE_ID), 5, "Terminating Service Flow Entry: " + SERVICE_ID,
187                                 0, 0, COOKIE
188                                         .add(BigInteger.valueOf(SERVICE_ID)),
189                                         null, null);
190                 } catch (Exception e) {
191                     //throw new Exception(e) ;
192               }
193
194             return terminatingServiceTableFlowEntity;
195         }
196
197         private String getFlowRef(short termSvcTable, int svcId) {
198             return new StringBuffer().append(termSvcTable).append(svcId).toString();
199         }
200
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>();
206             if (vlanid > 0) {
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) }));
209             }
210             listActionInfo.add(new ActionInfo(ActionType.output, new String[] { inport, "65535" }));
211             listBucketInfo.add(new BucketInfo(listActionInfo));
212
213             String groupName = "Test Group";
214             groupEntity = MDSALUtil.buildGroupEntity(Long.valueOf(Nodeid), id, groupName, GroupTypes.GroupIndirect,
215                     listBucketInfo);
216
217             return groupEntity;
218         }
219
220         private static long getUniqueValue(String nodeId, String inport) {
221
222             Long nodeIdL = Long.valueOf(nodeId);
223             Long inportL = Long.valueOf(inport);
224                 long sd_set;
225                 sd_set = nodeIdL * 10 + inportL;
226
227                 return sd_set;
228     }
229
230 }