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.Objects;
14 import java.util.Optional;
15 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
18 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
19 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
20 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketIn;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketInBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemovedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStatsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchPacketInMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchPacketInMessageBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStats;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStatsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
42 import org.opendaylight.yangtools.yang.binding.Augmentable;
43 import org.opendaylight.yangtools.yang.binding.Augmentation;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public final class MatchExtensionHelper {
49 private static final Logger LOG = LoggerFactory
50 .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 if (Objects.isNull(extBuilder)) {
72 LOG.warn("Convertor for {} for version {} with match path {} not found.",
73 matchEntry.toString(),
78 final GeneralAugMatchNodesNodeTableFlowBuilder builder = Optional
79 .ofNullable(matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class))
80 .map(GeneralAugMatchNodesNodeTableFlowBuilder::new)
81 .orElse(new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(new ArrayList<>()));
83 builder.getExtensionList().add(extBuilder.build());
84 matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, builder.build());
88 * Processes all extensions.
90 * @param matchEntries match entries
91 * @param ofVersion openflow version
92 * @param matchPath match path
93 * @param <E> extension point
94 * @return augmentation wrapper containing augmentation depending on matchPath
96 @SuppressWarnings("unchecked")
97 public static <E extends Augmentable<E>> AugmentTuple<E> processAllExtensions(Collection<MatchEntry> matchEntries,
98 OpenflowVersion ofVersion, MatchPath matchPath) {
99 List<ExtensionList> extensionsList = new ArrayList<>();
101 for (MatchEntry matchEntry : matchEntries) {
102 ExtensionListBuilder extensionListBld = processExtension(matchEntry, ofVersion.getVersion(), matchPath);
103 if (extensionListBld == null) {
107 extensionsList.add(extensionListBld.build());
110 AugmentTuple<E> augmentTuple = null;
111 if (!extensionsList.isEmpty()) {
113 case FLOWS_STATISTICS_UPDATE_MATCH:
114 GeneralAugMatchNotifUpdateFlowStatsBuilder generalExtMatchAugBld1 =
115 new GeneralAugMatchNotifUpdateFlowStatsBuilder();
116 generalExtMatchAugBld1.setExtensionList(extensionsList);
117 augmentTuple = (AugmentTuple<E>)
119 GeneralAugMatchNotifUpdateFlowStats.class, generalExtMatchAugBld1.build());
121 case PACKET_RECEIVED_MATCH:
122 GeneralAugMatchNotifPacketInBuilder generalExtMatchAugBld2 =
123 new GeneralAugMatchNotifPacketInBuilder();
124 generalExtMatchAugBld2.setExtensionList(extensionsList);
125 augmentTuple = (AugmentTuple<E>)
126 new AugmentTuple<>(GeneralAugMatchNotifPacketIn.class, generalExtMatchAugBld2.build());
128 case PACKET_IN_MESSAGE_MATCH:
129 GeneralAugMatchPacketInMessageBuilder generalExtMatchAugBld5 =
130 new GeneralAugMatchPacketInMessageBuilder();
131 generalExtMatchAugBld5.setExtensionList(extensionsList);
132 augmentTuple = (AugmentTuple<E>)
133 new AugmentTuple<>(GeneralAugMatchPacketInMessage.class, generalExtMatchAugBld5.build());
135 case SWITCH_FLOW_REMOVED_MATCH:
136 GeneralAugMatchNotifSwitchFlowRemovedBuilder generalExtMatchAugBld3 =
137 new GeneralAugMatchNotifSwitchFlowRemovedBuilder();
138 generalExtMatchAugBld3.setExtensionList(extensionsList);
139 augmentTuple = (AugmentTuple<E>)
141 GeneralAugMatchNotifSwitchFlowRemoved.class, generalExtMatchAugBld3.build());
143 case FLOWS_STATISTICS_RPC_MATCH:
144 GeneralAugMatchRpcOutputFlowStatsBuilder generalExtMatchAugBld4 =
145 new GeneralAugMatchRpcOutputFlowStatsBuilder();
146 generalExtMatchAugBld4.setExtensionList(extensionsList);
147 augmentTuple = (AugmentTuple<E>)
149 GeneralAugMatchRpcOutputFlowStats.class, generalExtMatchAugBld4.build());
152 LOG.warn("matchPath not supported: {}", matchPath);
160 * Processes an extension.
162 * @param ofVersion openflow version
163 * @param matchPath match path
164 * @param matchEntry match entry
165 * @return an ExtensionListBuilder
167 private static ExtensionListBuilder processExtension(MatchEntry matchEntry, short ofVersion, 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());
173 if (null != OFSessionUtil.getExtensionConvertorProvider()) {
174 ConvertorFromOFJava<MatchEntry, MatchPath> convertor =
175 OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
176 if (convertor != null) {
177 ExtensionAugment<? extends Augmentation<Extension>> extensionMatch =
178 convertor.convert(matchEntry, matchPath);
179 ExtensionBuilder extBld = new ExtensionBuilder();
180 extBld.addAugmentation(extensionMatch.getAugmentationClass(), extensionMatch.getAugmentationObject());
182 extListBld = new ExtensionListBuilder();
183 extListBld.setExtension(extBld.build());
184 extListBld.setExtensionKey(extensionMatch.getKey());