Bug 3292: Invalid Range exception in setDpnId
[vpnservice.git] / mdsalutil / mdsalutil-impl / src / test / java / org / opendaylight / vpnservice / test / MdSalUtilTest.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.vpnservice.test;
9
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;
19
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;
54
55 import static org.junit.Assert.assertEquals;
56
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";
67
68      @Before
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 ) ;
74
75             PowerMockito.mockStatic(MDSALUtil.class) ;
76
77             NodeKey s1Key = new NodeKey(new NodeId("openflow:1"));
78             addFlowCapableNode(s1Key);
79         }
80
81         @Test
82         public void testInstallFlow() {
83             String dpnId = "openflow:1";
84             String tableId1 = "12";
85
86             //Install Flow 1
87             FlowEntity testFlow1 = createFlowEntity(dpnId, tableId1) ;
88             mdSalMgr.installFlow(testFlow1);
89             assertEquals(1, flowFwder.getDataChgCount());
90
91             // Install FLow 2
92             String tableId2 = "13" ;
93              FlowEntity testFlow2 = createFlowEntity(dpnId, tableId2) ;
94              mdSalMgr.installFlow(testFlow2);
95              assertEquals(2, flowFwder.getDataChgCount());
96         }
97
98         @Test
99         public void testRemoveFlow() {
100             String dpnId = "openflow:1";
101             String tableId = "13" ;
102             FlowEntity testFlow = createFlowEntity(dpnId, tableId) ;
103
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());
109         }
110
111         @Test
112         public void testInstallGroup() {
113             // Install Group 1
114             String inport = "2" ;
115             int vlanid = 100 ;
116             GroupEntity grpEntity1 = createGroupEntity(Nodeid, inport, vlanid) ;
117
118              mdSalMgr.installGroup(grpEntity1);
119              assertEquals(1, grpFwder.getDataChgCount());
120
121              // Install Group 2
122                 inport = "3" ;
123                 vlanid = 100 ;
124                 GroupEntity grpEntity2 = createGroupEntity(Nodeid, inport, vlanid) ;
125                 mdSalMgr.installGroup(grpEntity2);
126                 assertEquals(2, grpFwder.getDataChgCount());
127         }
128
129         @Test
130         public void testRemoveGroup() {
131             String inport = "2" ;
132             int vlanid = 100 ;
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());
139         }
140
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);
145
146             FlowCapableNodeBuilder fcnBuilder = new FlowCapableNodeBuilder();
147             NodeBuilder nodeBuilder = new NodeBuilder();
148             nodeBuilder.setKey(nodeKey);
149             nodeBuilder.addAugmentation(FlowCapableNode.class, fcnBuilder.build());
150
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());
157         }
158
159          // Methods to test the install Flow and Group
160
161         public FlowEntity createFlowEntity(String dpnId, String tableId) {
162
163             BigInteger dpId;
164             int SERVICE_ID = 0;
165             FlowEntity terminatingServiceTableFlowEntity = null;
166
167             List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
168             listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
169                     new String[] {}));
170
171             try {
172                 dpId = new BigInteger(dpnId.split(":")[1]);
173
174                 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
175                 BigInteger COOKIE = new BigInteger("9000000", 16);
176
177                 short s_tableId = Short.parseShort(tableId) ;
178
179                 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
180                         new BigInteger("0000000000000000", 16) }));
181
182                 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
183                 mkInstructions.add(new InstructionInfo(InstructionType.write_actions,
184                         listActionInfo));
185
186                 terminatingServiceTableFlowEntity = MDSALUtil
187                         .buildFlowEntity(
188                                 dpId,
189                                 s_tableId,
190                                 getFlowRef(s_tableId,
191                                         SERVICE_ID), 5, "Terminating Service Flow Entry: " + SERVICE_ID,
192                                 0, 0, COOKIE
193                                         .add(BigInteger.valueOf(SERVICE_ID)),
194                                         null, null);
195                 } catch (Exception e) {
196                     //throw new Exception(e) ;
197               }
198
199             return terminatingServiceTableFlowEntity;
200         }
201
202         private String getFlowRef(short termSvcTable, int svcId) {
203             return new StringBuffer().append(termSvcTable).append(svcId).toString();
204         }
205
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>();
211             if (vlanid > 0) {
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) }));
214             }
215             listActionInfo.add(new ActionInfo(ActionType.output, new String[] { inport, "65535" }));
216             listBucketInfo.add(new BucketInfo(listActionInfo));
217
218             String groupName = "Test Group";
219             BigInteger dpnId = new BigInteger(Nodeid.split(":")[1]);
220             groupEntity = MDSALUtil.buildGroupEntity(dpnId, id, groupName, GroupTypes.GroupIndirect,
221                     listBucketInfo);
222
223             return groupEntity;
224         }
225
226         private static long getUniqueValue(String nodeId, String inport) {
227
228             Long nodeIdL = Long.valueOf(nodeId.split(":")[1]);
229             Long inportL = Long.valueOf(inport);
230                 long sd_set;
231                 sd_set = nodeIdL * 10 + inportL;
232
233                 return sd_set;
234     }
235
236 }