Migrate openflowplugim-impl tests to uint types
[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.util.Collections;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.junit.MockitoJUnitRunner;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.Uint16;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42
43 /**
44  * Test of {@link AggregatedFlowStatisticsTranslator}.
45  */
46 @RunWith(MockitoJUnitRunner.class)
47 public class FlowRemovedTranslatorTest {
48
49     private FlowRemovedTranslator translator;
50
51     private FlowRemovedV10Translator translatorV10;
52
53     @Mock
54     private DeviceContext deviceContext;
55
56     @Mock
57     private DeviceState deviceState;
58
59     @Mock
60     private DeviceInfo deviceInfo;
61
62     @Mock
63     private GetFeaturesOutput features;
64
65     @Mock
66     private FlowWildcardsV10 flowWildcards;
67
68     @Before
69     public void setUp() {
70         final KeyedInstanceIdentifier<Node, NodeKey> nodeId = InstanceIdentifier.create(Nodes.class)
71                 .child(Node.class, new NodeKey(new NodeId("dummyNodeId")));
72
73         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
74         translator = new FlowRemovedTranslator(convertorManager);
75         translatorV10 = new FlowRemovedV10Translator(convertorManager);
76
77         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeId);
78     }
79
80     @Test
81     public void testTranslate() {
82         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved flowRemovedMessage =
83                 buildMessage(false);
84         final FlowRemoved flowRemoved = translator.translate(flowRemovedMessage, deviceInfo, null);
85
86         assertEquals(flowRemovedMessage.getCookie(), flowRemoved.getCookie().getValue());
87         assertEquals(flowRemovedMessage.getPriority(), flowRemoved.getPriority());
88         assertEquals(flowRemovedMessage.getTableId().getValue().toJava(), flowRemoved.getTableId().toJava());
89     }
90
91     @Test
92     public void testTranslateV10() {
93         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved flowRemovedMessage =
94                 buildMessage(true);
95         final FlowRemoved flowRemoved = translatorV10.translate(flowRemovedMessage, deviceInfo, null);
96
97         assertEquals(flowRemovedMessage.getCookie(), flowRemoved.getCookie().getValue());
98         assertEquals(flowRemovedMessage.getPriority(), flowRemoved.getPriority());
99         assertEquals((short)0, flowRemoved.getTableId().shortValue());
100     }
101
102     private org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved
103             buildMessage(final boolean isV10) {
104         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder()
105                 .setCookie(Uint64.ONE)
106                 .setReason(FlowRemovedReason.OFPRRGROUPDELETE)
107                 .setPriority(Uint16.ONE);
108
109         if (isV10) {
110             builder.setMatchV10(new MatchV10Builder().setWildcards(flowWildcards).build());
111         } else {
112             builder.setMatch(new MatchBuilder().setMatchEntry(Collections.emptyList()).build())
113                 .setTableId(new TableId(Uint32.valueOf(42)));
114         }
115
116         return builder.build();
117     }
118 }