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