a695ef097017dbdde2df959cea31b6d0ac9061cd
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / translator / FlowRemovedTranslatorTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.openflowplugin.impl.translator;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.when;
13
14 import java.math.BigInteger;
15 import java.util.Collections;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
38
39 /**
40  * Test of {@link AggregatedFlowStatisticsTranslator}
41  */
42 @RunWith(MockitoJUnitRunner.class)
43 public class FlowRemovedTranslatorTest {
44
45     private FlowRemovedTranslator translator;
46
47     private FlowRemovedV10Translator translatorV10;
48
49     @Mock
50     private DeviceContext deviceContext;
51
52     @Mock
53     private DeviceState deviceState;
54
55     @Mock
56     private GetFeaturesOutput features;
57
58     @Mock
59     private FlowWildcardsV10 flowWildcards;
60
61     private KeyedInstanceIdentifier<Node, NodeKey> nodeId;
62
63     @Before
64     public void setUp() throws Exception {
65         nodeId = InstanceIdentifier.create(Nodes.class)
66                 .child(Node.class, new NodeKey(new NodeId("dummyNodeId")));
67
68         translator = new FlowRemovedTranslator();
69         translatorV10 = new FlowRemovedV10Translator();
70
71         when(deviceContext.getDeviceState()).thenReturn(deviceState);
72         when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeId);
73         when(deviceState.getFeatures()).thenReturn(features);
74         when(features.getDatapathId()).thenReturn(BigInteger.TEN);
75     }
76
77     @Test
78     public void testTranslate() throws Exception {
79         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved flowRemovedMessage = buildMessage(false);
80         final FlowRemoved flowRemoved = translator.translate(flowRemovedMessage, deviceState, null);
81
82         assertEquals(flowRemovedMessage.getCookie(), flowRemoved.getCookie().getValue());
83         assertEquals(flowRemovedMessage.getPriority(), flowRemoved.getPriority());
84         assertEquals((long)flowRemovedMessage.getTableId().getValue(), (long)flowRemoved.getTableId());
85     }
86
87     @Test
88     public void testTranslateV10() throws Exception {
89         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved flowRemovedMessage = buildMessage(true);
90         final FlowRemoved flowRemoved = translatorV10.translate(flowRemovedMessage, deviceState, null);
91
92         assertEquals(flowRemovedMessage.getCookie(), flowRemoved.getCookie().getValue());
93         assertEquals(flowRemovedMessage.getPriority(), flowRemoved.getPriority());
94         assertEquals((long)flowRemovedMessage.getTableId().getValue(), (long)flowRemoved.getTableId());
95     }
96
97     private org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved buildMessage(boolean isV10) {
98         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder()
99                 .setCookie(BigInteger.ONE)
100                 .setPriority(1)
101                 .setTableId(new TableId(42l));
102
103         if (isV10) {
104             builder.setMatchV10(new MatchV10Builder().setWildcards(flowWildcards).build());
105         } else {
106             builder.setMatch(new MatchBuilder().setMatchEntry(Collections.<MatchEntry>emptyList()).build());
107         }
108
109         return builder.build();
110     }
111 }