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