RPC opendaylight-direct-statistics:get-flow-statistics not taking nicira
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / extension / MatchExtensionHelper.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 java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
16 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
17 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
18 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketIn;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketInBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemovedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStats;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStatsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStats;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStatsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match;
36 import org.opendaylight.yangtools.yang.binding.Augmentable;
37 import org.opendaylight.yangtools.yang.binding.Augmentation;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  *
43  */
44 public final class MatchExtensionHelper {
45
46     private static final Logger LOG = LoggerFactory
47             .getLogger(MatchExtensionHelper.class);
48
49     private MatchExtensionHelper() {
50         throw new IllegalAccessError("singleton enforcement");
51     }
52
53     /**
54      * @param matchEntries match entries
55      * @param ofVersion openflow version
56      * @param matchPath match path
57      * @param <EXT_POINT> extension point
58      * @return augmentation wrapper containing augmentation depending on matchPath
59      */
60     @SuppressWarnings("unchecked")
61     public static <EXT_POINT extends Augmentable<EXT_POINT>>
62     AugmentTuple<EXT_POINT> processAllExtensions(Collection<MatchEntry> matchEntries,
63                                                  OpenflowVersion ofVersion, MatchPath matchPath) {
64         List<ExtensionList> extensionsList = new ArrayList<>();
65
66         for (MatchEntry matchEntry : matchEntries) {
67             ExtensionListBuilder extensionListBld = processExtension(matchEntry, ofVersion, matchPath);
68             if (extensionListBld == null) {
69                 continue;
70             }
71
72             extensionsList.add(extensionListBld.build());
73         }
74
75         AugmentTuple<EXT_POINT> augmentTuple = null;
76         if (!extensionsList.isEmpty()) {
77             switch (matchPath) {
78                 case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH:
79                     GeneralAugMatchNotifUpdateFlowStatsBuilder generalExtMatchAugBld1 = new GeneralAugMatchNotifUpdateFlowStatsBuilder();
80                     generalExtMatchAugBld1.setExtensionList(extensionsList);
81                     augmentTuple = (AugmentTuple<EXT_POINT>)
82                             new AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match>(
83                                     GeneralAugMatchNotifUpdateFlowStats.class, generalExtMatchAugBld1.build());
84                     break;
85                 case PACKETRECEIVED_MATCH:
86                     GeneralAugMatchNotifPacketInBuilder generalExtMatchAugBld2 = new GeneralAugMatchNotifPacketInBuilder();
87                     generalExtMatchAugBld2.setExtensionList(extensionsList);
88                     augmentTuple = (AugmentTuple<EXT_POINT>)
89                             new AugmentTuple<Match>(GeneralAugMatchNotifPacketIn.class, generalExtMatchAugBld2.build());
90                     break;
91                 case SWITCHFLOWREMOVED_MATCH:
92                     GeneralAugMatchNotifSwitchFlowRemovedBuilder generalExtMatchAugBld3 = new GeneralAugMatchNotifSwitchFlowRemovedBuilder();
93                     generalExtMatchAugBld3.setExtensionList(extensionsList);
94                     augmentTuple = (AugmentTuple<EXT_POINT>)
95                             new AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.Match>(
96                                     GeneralAugMatchNotifSwitchFlowRemoved.class, generalExtMatchAugBld3.build());
97                     break;
98                 case RPCFLOWSSTATISTICS_FLOWANDSTATISTICSMAPLIST_MATCH:
99                     GeneralAugMatchRpcOutputFlowStatsBuilder generalExtMatchAugBld4 = new GeneralAugMatchRpcOutputFlowStatsBuilder();
100                     generalExtMatchAugBld4.setExtensionList(extensionsList);
101                     augmentTuple = (AugmentTuple<EXT_POINT>)
102                             new AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match>(
103                                     GeneralAugMatchRpcOutputFlowStats.class, generalExtMatchAugBld4.build());
104                     break;
105                 default:
106                     LOG.warn("matchPath not supported: {}", matchPath);
107             }
108         }
109
110         return augmentTuple;
111     }
112
113     /**
114      * @param ofVersion
115      * @param matchPath
116      * @param matchEntry
117      * @return
118      */
119     private static ExtensionListBuilder processExtension(MatchEntry matchEntry, OpenflowVersion ofVersion, MatchPath matchPath) {
120         ExtensionListBuilder extListBld = null;
121
122         /** TODO: EXTENSION PROPOSAL (match, OFJava to MD-SAL) */
123         MatchEntrySerializerKey<? extends OxmClassBase, ? extends MatchField> key = new MatchEntrySerializerKey<>(
124                 ofVersion.getVersion(), matchEntry.getOxmClass(), matchEntry.getOxmMatchField());
125         if (null != OFSessionUtil.getExtensionConvertorProvider()) {
126             ConvertorFromOFJava<MatchEntry, MatchPath> convertor = OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
127             if (convertor != null) {
128                 ExtensionAugment<? extends Augmentation<Extension>> extensionMatch =
129                         convertor.convert(matchEntry, matchPath);
130                 ExtensionBuilder extBld = new ExtensionBuilder();
131                 extBld.addAugmentation(extensionMatch.getAugmentationClass(), extensionMatch.getAugmentationObject());
132
133                 extListBld = new ExtensionListBuilder();
134                 extListBld.setExtension(extBld.build());
135                 extListBld.setExtensionKey(extensionMatch.getKey());
136             }
137         }
138         return extListBld;
139     }
140
141 }