2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.core.extension;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
15 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
16 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
17 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
18 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
19 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.oxm.container.match.entry.value.ExperimenterIdCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ExperimenterClass;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketIn;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketInBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemovedBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStats;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStatsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchPacketInMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchPacketInMessageBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStats;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStatsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
43 import org.opendaylight.yangtools.yang.binding.Augmentable;
44 import org.opendaylight.yangtools.yang.binding.Augmentation;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public final class MatchExtensionHelper {
50 private static final Logger LOG = LoggerFactory.getLogger(MatchExtensionHelper.class);
52 private MatchExtensionHelper() {
53 throw new IllegalAccessError("singleton enforcement");
57 * Injects an extension.
59 * @param matchEntry match entry
60 * @param ofVersion openflow version
61 * @param matchPath match path
63 public static void injectExtension(
64 final short ofVersion,
65 final MatchEntry matchEntry,
66 final MatchBuilder matchBuilder,
67 final MatchPath matchPath) {
69 final ExtensionListBuilder extBuilder = processExtension(matchEntry, ofVersion, matchPath);
71 final GeneralAugMatchNodesNodeTableFlowBuilder builder = Optional
72 .ofNullable(matchBuilder.augmentation(GeneralAugMatchNodesNodeTableFlow.class))
73 .map(GeneralAugMatchNodesNodeTableFlowBuilder::new)
74 .orElse(new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(new ArrayList<>()));
76 if (extBuilder != null) {
77 builder.getExtensionList().add(extBuilder.build());
79 LOG.warn("Convertor for {} for version {} with match path {} not found.",
80 matchEntry, ofVersion, matchPath.name());
83 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, builder.build());
87 * Processes all extensions.
89 * @param matchEntries match entries
90 * @param ofVersion openflow version
91 * @param matchPath match path
92 * @param <E> extension point
93 * @return augmentation wrapper containing augmentation depending on matchPath
95 @SuppressWarnings("unchecked")
96 public static <E extends Augmentable<E>> AugmentTuple<E> processAllExtensions(
97 final Collection<MatchEntry> matchEntries, final OpenflowVersion ofVersion, final MatchPath matchPath) {
98 List<ExtensionList> extensionsList = new ArrayList<>();
100 for (MatchEntry matchEntry : matchEntries) {
101 ExtensionListBuilder extensionListBld = processExtension(matchEntry, ofVersion.getVersion(), matchPath);
102 if (extensionListBld == null) {
106 extensionsList.add(extensionListBld.build());
109 AugmentTuple<E> augmentTuple = null;
110 if (!extensionsList.isEmpty()) {
112 case FLOWS_STATISTICS_UPDATE_MATCH:
113 GeneralAugMatchNotifUpdateFlowStatsBuilder generalExtMatchAugBld1 =
114 new GeneralAugMatchNotifUpdateFlowStatsBuilder();
115 generalExtMatchAugBld1.setExtensionList(extensionsList);
116 augmentTuple = (AugmentTuple<E>)
118 GeneralAugMatchNotifUpdateFlowStats.class, generalExtMatchAugBld1.build());
120 case PACKET_RECEIVED_MATCH:
121 GeneralAugMatchNotifPacketInBuilder generalExtMatchAugBld2 =
122 new GeneralAugMatchNotifPacketInBuilder();
123 generalExtMatchAugBld2.setExtensionList(extensionsList);
124 augmentTuple = (AugmentTuple<E>)
125 new AugmentTuple<>(GeneralAugMatchNotifPacketIn.class, generalExtMatchAugBld2.build());
127 case PACKET_IN_MESSAGE_MATCH:
128 GeneralAugMatchPacketInMessageBuilder generalExtMatchAugBld5 =
129 new GeneralAugMatchPacketInMessageBuilder();
130 generalExtMatchAugBld5.setExtensionList(extensionsList);
131 augmentTuple = (AugmentTuple<E>)
132 new AugmentTuple<>(GeneralAugMatchPacketInMessage.class, generalExtMatchAugBld5.build());
134 case SWITCH_FLOW_REMOVED_MATCH:
135 GeneralAugMatchNotifSwitchFlowRemovedBuilder generalExtMatchAugBld3 =
136 new GeneralAugMatchNotifSwitchFlowRemovedBuilder();
137 generalExtMatchAugBld3.setExtensionList(extensionsList);
138 augmentTuple = (AugmentTuple<E>)
140 GeneralAugMatchNotifSwitchFlowRemoved.class, generalExtMatchAugBld3.build());
142 case FLOWS_STATISTICS_RPC_MATCH:
143 GeneralAugMatchRpcOutputFlowStatsBuilder generalExtMatchAugBld4 =
144 new GeneralAugMatchRpcOutputFlowStatsBuilder();
145 generalExtMatchAugBld4.setExtensionList(extensionsList);
146 augmentTuple = (AugmentTuple<E>)
148 GeneralAugMatchRpcOutputFlowStats.class, generalExtMatchAugBld4.build());
151 LOG.warn("matchPath not supported: {}", matchPath);
159 * Processes an extension.
161 * @param ofVersion openflow version
162 * @param matchPath match path
163 * @param matchEntry match entry
164 * @return an ExtensionListBuilder
166 private static ExtensionListBuilder processExtension(final MatchEntry matchEntry, final short ofVersion,
167 final MatchPath matchPath) {
168 ExtensionListBuilder extListBld = null;
170 // TODO: EXTENSION PROPOSAL (match, OFJava to MD-SAL)
171 MatchEntrySerializerKey<? extends OxmClassBase, ? extends MatchField> key = new MatchEntrySerializerKey<>(
172 ofVersion, matchEntry.getOxmClass(), matchEntry.getOxmMatchField());
174 // If entry is experimenter, set experimenter ID to key
175 if (matchEntry.getOxmClass().equals(ExperimenterClass.class)) {
176 ExperimenterIdCase experimenterIdCase = (ExperimenterIdCase) matchEntry.getMatchEntryValue();
177 key.setExperimenterId(experimenterIdCase.getExperimenter().getExperimenter().getValue());
180 if (null != OFSessionUtil.getExtensionConvertorProvider()) {
181 ConvertorFromOFJava<MatchEntry, MatchPath> convertor =
182 OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
183 if (convertor != null) {
184 ExtensionAugment<? extends Augmentation<Extension>> extensionMatch =
185 convertor.convert(matchEntry, matchPath);
186 ExtensionBuilder extBld = new ExtensionBuilder();
187 extBld.addAugmentation(extensionMatch.getAugmentationClass(), extensionMatch.getAugmentationObject());
189 extListBld = new ExtensionListBuilder();
190 extListBld.setExtension(extBld.build());
191 extListBld.setExtensionKey(extensionMatch.getKey());