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