2 * Copyright (c) 2015 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.impl.util;
10 import com.google.common.collect.ImmutableMap;
11 import java.util.List;
13 import java.util.Optional;
14 import java.util.function.Function;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionResolvers;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetField;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowWriteActionsSetField;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowWriteActionsSetFieldBuilder;
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.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.general.extension.list.grouping.ExtensionList;
38 public final class MatchUtil {
40 private static final MacAddress ZERO_MAC_ADDRESS = new MacAddress("00:00:00:00:00:00");
41 private static final Ipv4Address ZERO_IPV4_ADDRESS = new Ipv4Address("0.0.0.0");
43 private static final Map<Class<? extends Match>, Function<Match, Match>> TRANSFORMERS = ImmutableMap
44 .<Class<? extends Match>, Function<Match, Match>>builder()
45 .put(SetField.class, match -> {
46 final SetFieldBuilder matchBuilder = new SetFieldBuilder(match);
48 resolveExtensions(match).ifPresent(extensionLists -> matchBuilder
49 .addAugmentation(GeneralAugMatchNodesNodeTableFlowWriteActionsSetField.class,
50 new GeneralAugMatchNodesNodeTableFlowWriteActionsSetFieldBuilder()
51 .setExtensionList(extensionLists)
54 return matchBuilder.build();
56 .put(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow
57 .Match.class, (match) -> {
58 final MatchBuilder matchBuilder = new MatchBuilder(match);
60 resolveExtensions(match).ifPresent(extensionLists -> matchBuilder
61 .addAugmentation(GeneralAugMatchNotifUpdateFlowStats.class,
62 new GeneralAugMatchNodesNodeTableFlowBuilder()
63 .setExtensionList(extensionLists)
66 return matchBuilder.build();
68 .put(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed
69 .Match.class, (match) -> {
70 final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed
71 .MatchBuilder matchBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types
72 .rev131026.flow.mod.removed.MatchBuilder(match);
74 resolveExtensions(match).ifPresent(extensionLists -> matchBuilder
75 .addAugmentation(GeneralAugMatchNotifSwitchFlowRemoved.class,
76 new GeneralAugMatchNotifSwitchFlowRemovedBuilder()
77 .setExtensionList(extensionLists)
80 return matchBuilder.build();
82 .put(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received
83 .Match.class, (match) -> {
84 final org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received
85 .MatchBuilder matchBuilder =
86 new org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service
87 .rev130709.packet.received.MatchBuilder(match);
89 resolveExtensions(match).ifPresent(extensionLists -> matchBuilder
90 .addAugmentation(GeneralAugMatchNotifPacketIn.class,
91 new GeneralAugMatchNotifPacketInBuilder()
92 .setExtensionList(extensionLists)
95 return matchBuilder.build();
97 .put(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.in.message
98 .Match.class, (match) -> {
99 final org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.in.message
100 .MatchBuilder matchBuilder =
101 new org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service
102 .rev130709.packet.in.message.MatchBuilder(match);
104 resolveExtensions(match).ifPresent(extensionLists -> matchBuilder
105 .addAugmentation(GeneralAugMatchPacketInMessage.class,
106 new GeneralAugMatchPacketInMessageBuilder()
107 .setExtensionList(extensionLists)
110 return matchBuilder.build();
114 private MatchUtil() {
115 throw new IllegalStateException("This class should not be instantiated.");
119 public static MatchV10Builder createEmptyV10Match() {
121 Integer zeroInteger = 0;
122 MatchV10Builder matchV10Builder = new MatchV10Builder();
123 matchV10Builder.setDlDst(ZERO_MAC_ADDRESS);
124 matchV10Builder.setDlSrc(ZERO_MAC_ADDRESS);
125 matchV10Builder.setDlType(zeroInteger);
126 matchV10Builder.setDlVlan(zeroInteger);
127 matchV10Builder.setDlVlanPcp(zeroShort);
128 matchV10Builder.setInPort(zeroInteger);
129 matchV10Builder.setNwDst(ZERO_IPV4_ADDRESS);
130 matchV10Builder.setNwDstMask(zeroShort);
131 matchV10Builder.setNwProto(zeroShort);
132 matchV10Builder.setNwSrc(ZERO_IPV4_ADDRESS);
133 matchV10Builder.setNwSrcMask(zeroShort);
134 matchV10Builder.setNwTos(zeroShort);
135 matchV10Builder.setTpDst(zeroInteger);
136 matchV10Builder.setTpSrc(zeroInteger);
137 FlowWildcardsV10 flowWildcardsV10 =
138 new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true);
139 matchV10Builder.setWildcards(flowWildcardsV10);
140 return matchV10Builder;
144 public static <T extends Match> T transformMatch(@Nullable final Match match,
145 @NonNull final Class<T> implementedInterface) {
150 if (implementedInterface.equals(match.implementedInterface())) {
151 return implementedInterface.cast(match);
154 final Function<Match, Match> matchMatchFunction = TRANSFORMERS.get(implementedInterface);
155 return matchMatchFunction == null ? null : implementedInterface.cast(matchMatchFunction.apply(match));
158 private static Optional<List<ExtensionList>> resolveExtensions(final Match match) {
159 return ExtensionResolvers
160 .getMatchExtensionResolver()
162 .flatMap(matchExtension -> Optional.ofNullable(matchExtension.getExtensionList()));