c3c5f42a7d2cf9f45268f9eb6b410c632fe627f7
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / extension / MatchExtensionHelperTest.java
1 /*
2  * Copyright (c) 2014 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.openflow.md.core.extension;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.when;
13
14 import java.util.ArrayList;
15 import java.util.List;
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.junit.MockitoJUnitRunner;
21 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
22 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
23 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
24 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
25 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
26 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
27 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
34 import org.opendaylight.yangtools.yang.binding.Augmentation;
35
36 /**
37  * Created by Martin Bobak mbobak@cisco.com on 9/19/14.
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class MatchExtensionHelperTest {
41
42     @Mock
43     private ExtensionConverterProvider extensionConverterProvider;
44     private static final int PRESET_COUNT = 7;
45     private static final MatchEntrySerializerKey<? extends OxmClassBase, ? extends MatchField> KEY =
46         new MatchEntrySerializerKey<>(OpenflowVersion.OF13.getVersion(), MockOxmClassBase.class, MockMatchField.class);
47
48     @Before
49     public void setup() {
50         OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
51         when(extensionConverterProvider.getConverter(KEY))
52             .thenReturn((input, path) -> {
53                 MockAugmentation mockAugmentation = new MockAugmentation();
54                 return new ExtensionAugment<>(MockAugmentation.class, mockAugmentation, MockExtensionKey.class);
55             });
56     }
57
58
59     @Test
60     /**
61      * Basic functionality test method for {@link MatchExtensionHelper#processAllExtensions(Collection,
62      * OpenflowVersion, MatchPath)}.
63      */
64     public void testProcessAllExtensions() {
65
66         List<MatchEntry> matchEntries = createMatchEntrieses();
67         AugmentTuple augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13,
68                 MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
69         assertNotNull(augmentTuple);
70
71         augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13,
72                 MatchPath.PACKET_RECEIVED_MATCH);
73         assertNotNull(augmentTuple);
74
75         augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13,
76                 MatchPath.SWITCH_FLOW_REMOVED_MATCH);
77         assertNotNull(augmentTuple);
78     }
79
80
81     private static List<MatchEntry> createMatchEntrieses() {
82         List<MatchEntry> matchEntries = new ArrayList<>();
83         for (int i = 0; i < PRESET_COUNT; i++) {
84             MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
85             matchEntryBuilder.setHasMask(true);
86             matchEntryBuilder.setOxmClass(MockOxmClassBase.class);
87             matchEntryBuilder.setOxmMatchField(MockMatchField.class);
88             matchEntries.add(matchEntryBuilder.build());
89         }
90         return matchEntries;
91     }
92
93     private interface MockOxmClassBase extends OxmClassBase {
94
95     }
96
97     private interface MockMatchField extends MatchField {
98
99     }
100
101     private interface MockExtensionKey extends ExtensionKey {
102
103     }
104
105     private final class MockAugmentation implements Augmentation<Extension> {
106
107     }
108 }