DeviceState changes
[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.DeviceInfo;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
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
40 /**
41  * Test of {@link AggregatedFlowStatisticsTranslator}
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class FlowRemovedTranslatorTest {
45
46     private FlowRemovedTranslator translator;
47
48     private FlowRemovedV10Translator translatorV10;
49
50     @Mock
51     private DeviceContext deviceContext;
52
53     @Mock
54     private DeviceState deviceState;
55
56     @Mock
57     private DeviceInfo deviceInfo;
58
59     @Mock
60     private GetFeaturesOutput features;
61
62     @Mock
63     private FlowWildcardsV10 flowWildcards;
64
65     private KeyedInstanceIdentifier<Node, NodeKey> nodeId;
66
67     @Before
68     public void setUp() throws Exception {
69         nodeId = InstanceIdentifier.create(Nodes.class)
70                 .child(Node.class, new NodeKey(new NodeId("dummyNodeId")));
71
72         translator = new FlowRemovedTranslator();
73         translatorV10 = new FlowRemovedV10Translator();
74
75         when(deviceContext.getDeviceState()).thenReturn(deviceState);
76         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeId);
77         when(features.getDatapathId()).thenReturn(BigInteger.TEN);
78     }
79
80     @Test
81     public void testTranslate() throws Exception {
82         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved flowRemovedMessage = buildMessage(false);
83         final FlowRemoved flowRemoved = translator.translate(flowRemovedMessage, deviceInfo, null);
84
85         assertEquals(flowRemovedMessage.getCookie(), flowRemoved.getCookie().getValue());
86         assertEquals(flowRemovedMessage.getPriority(), flowRemoved.getPriority());
87         assertEquals((long)flowRemovedMessage.getTableId().getValue(), (long)flowRemoved.getTableId());
88     }
89
90     @Test
91     public void testTranslateV10() throws Exception {
92         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved flowRemovedMessage = buildMessage(true);
93         final FlowRemoved flowRemoved = translatorV10.translate(flowRemovedMessage, deviceInfo, null);
94
95         assertEquals(flowRemovedMessage.getCookie(), flowRemoved.getCookie().getValue());
96         assertEquals(flowRemovedMessage.getPriority(), flowRemoved.getPriority());
97         assertEquals((short)0, flowRemoved.getTableId().shortValue());
98     }
99
100     private org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved buildMessage(boolean isV10) {
101         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder()
102                 .setCookie(BigInteger.ONE)
103                 .setPriority(1);
104
105         if (isV10) {
106             builder.setMatchV10(new MatchV10Builder().setWildcards(flowWildcards).build());
107         } else {
108             builder.setMatch(new MatchBuilder().setMatchEntry(Collections.<MatchEntry>emptyList()).build())
109                 .setTableId(new TableId(42l));
110         }
111
112         return builder.build();
113     }
114 }