Bug 1764 - moved Session related interfaces to openflowplugin-api
[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.MockitoAnnotations.Mock;
20 import org.mockito.runners.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.ConvertorFromOFJava;
25 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
26 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
27 import org.opendaylight.openflowplugin.extension.api.path.AugmentationPath;
28 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
29 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmClassBase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
36 import org.opendaylight.yangtools.yang.binding.Augmentation;
37 import org.opendaylight.yangtools.yang.binding.DataContainer;
38
39 /**
40  * Created by Martin Bobak mbobak@cisco.com on 9/19/14.
41  */
42 @RunWith(MockitoJUnitRunner.class)
43 public class MatchExtensionHelperTest {
44
45     @Mock
46     private ExtensionConverterProvider extensionConverterProvider;
47     private static final int PRESET_COUNT = 7;
48     private static final MatchEntrySerializerKey<? extends OxmClassBase, ? extends MatchField> key = new MatchEntrySerializerKey<>(OpenflowVersion.OF13.getVersion(), MockOxmClassBase.class, MockMatchField.class);
49
50     @Before
51     public void setup() {
52         OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
53         when(extensionConverterProvider.getConverter(key)).thenReturn(new ConvertorFromOFJava<DataContainer, AugmentationPath>() {
54             @Override
55             public ExtensionAugment<? extends Augmentation<Extension>> convert(DataContainer input, AugmentationPath path) {
56                 MockAugmentation mockAugmentation  = new MockAugmentation();
57                 return new ExtensionAugment(MockAugmentation.class, mockAugmentation, MockExtensionKey.class);
58             }
59         });
60     }
61
62
63     @Test
64     /**
65      * Basic functionality test method for {@link org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper#processAllExtensions(java.util.Collection, org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion, org.opendaylight.openflowplugin.extension.api.path.MatchPath)}
66      */
67     public void testProcessAllExtensions() {
68
69         List<MatchEntries> matchEntries = createMatchEntrieses();
70         AugmentTuple augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13, MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
71         assertNotNull(augmentTuple);
72
73         augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13, MatchPath.PACKETRECEIVED_MATCH);
74         assertNotNull(augmentTuple);
75
76         augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13, MatchPath.SWITCHFLOWREMOVED_MATCH);
77         assertNotNull(augmentTuple);
78     }
79
80
81
82     private List<MatchEntries> createMatchEntrieses() {
83         List<MatchEntries> matchEntries = new ArrayList<>();
84         for (int i = 0; i < PRESET_COUNT; i++) {
85             MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
86             matchEntriesBuilder.setHasMask(true);
87             matchEntriesBuilder.setOxmClass(MockOxmClassBase.class);
88             matchEntriesBuilder.setOxmMatchField(MockMatchField.class);
89             matchEntries.add(matchEntriesBuilder.build());
90         }
91         return matchEntries;
92     }
93
94     private static final class MockOxmClassBase extends OxmClassBase {
95
96     }
97
98     private static final class MockMatchField extends MatchField {
99
100     }
101
102     private final class MockExtensionKey extends ExtensionKey {
103
104     }
105
106     private final class MockAugmentation implements Augmentation<Extension> {
107
108     }
109 }