Added copyright and updated appropriate log levels
[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
67      @Before
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 ) ;
73
74             PowerMockito.mockStatic(MDSALUtil.class) ;
75
76             NodeKey s1Key = new NodeKey(new NodeId("openflow:1"));
77             addFlowCapableNode(s1Key);
78         }
79
80         @Test
81         public void testInstallFlow() {
82             String dpnId = "openflow:1";
83             String tableId1 = "12";
84
85             //Install Flow 1
86             FlowEntity testFlow1 = createFlowEntity(dpnId, tableId1) ;
87             mdSalMgr.installFlow(testFlow1);
88             assertEquals(1, flowFwder.getDataChgCount());
89
90             // Install FLow 2
91             String tableId2 = "13" ;
92              FlowEntity testFlow2 = createFlowEntity(dpnId, tableId2) ;
93              mdSalMgr.installFlow(testFlow2);
94              assertEquals(2, flowFwder.getDataChgCount());
95         }
96
97         @Test
98         public void testRemoveFlow() {
99             String dpnId = "openflow:1";
100             String tableId = "13" ;
101             FlowEntity testFlow = createFlowEntity(dpnId, tableId) ;
102
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());
108         }
109
110         @Test
111         public void testInstallGroup() {
112             // Install Group 1
113             String Nodeid = "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                Nodeid = "1";
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 Nodeid = "1";
133             String inport = "2" ;
134             int vlanid = 100 ;
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());
141         }
142
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);
147
148             FlowCapableNodeBuilder fcnBuilder = new FlowCapableNodeBuilder();
149             NodeBuilder nodeBuilder = new NodeBuilder();
150             nodeBuilder.setKey(nodeKey);
151             nodeBuilder.addAugmentation(FlowCapableNode.class, fcnBuilder.build());
152
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());
159         }
160
161          // Methods to test the install Flow and Group
162
163         public FlowEntity createFlowEntity(String dpnId, String tableId) {
164
165             long dpId;
166             int SERVICE_ID = 0;
167             FlowEntity terminatingServiceTableFlowEntity = null;
168
169             List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
170             listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
171                     new String[] {}));
172
173             try {
174                 dpId = Long.parseLong(dpnId.split(":")[1]);
175
176                 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
177                 BigInteger COOKIE = new BigInteger("9000000", 16);
178
179                 short s_tableId = Short.parseShort(tableId) ;
180
181                 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
182                         new BigInteger("0000000000000000", 16) }));
183
184                 List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
185                 mkInstructions.add(new InstructionInfo(InstructionType.write_actions,
186                         listActionInfo));
187
188                 terminatingServiceTableFlowEntity = MDSALUtil
189                         .buildFlowEntity(
190                                 dpId,
191                                 s_tableId,
192                                 getFlowRef(s_tableId,
193                                         SERVICE_ID), 5, "Terminating Service Flow Entry: " + SERVICE_ID,
194                                 0, 0, COOKIE
195                                         .add(BigInteger.valueOf(SERVICE_ID)),
196                                         null, null);
197                 } catch (Exception e) {
198                     //throw new Exception(e) ;
199               }
200
201             return terminatingServiceTableFlowEntity;
202         }
203
204         private String getFlowRef(short termSvcTable, int svcId) {
205             return new StringBuffer().append(termSvcTable).append(svcId).toString();
206         }
207
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>();
213             if (vlanid > 0) {
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) }));
216             }
217             listActionInfo.add(new ActionInfo(ActionType.output, new String[] { inport, "65535" }));
218             listBucketInfo.add(new BucketInfo(listActionInfo));
219
220             String groupName = "Test Group";
221             groupEntity = MDSALUtil.buildGroupEntity(Long.valueOf(Nodeid), 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);
230             Long inportL = Long.valueOf(inport);
231                 long sd_set;
232                 sd_set = nodeIdL * 10 + inportL;
233
234                 return sd_set;
235     }
236
237 }