Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / MultipartMatchFieldSerializerInjector.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.impl.protocol.serialization;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import java.util.function.Consumer;
12 import java.util.function.Function;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerExtensionProvider;
15 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.ArpOpMatchFieldSerializer;
18 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.ArpShaMatchFieldSerializer;
19 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.ArpSpaMatchFieldSerializer;
20 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.ArpThaMatchFieldSerializer;
21 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.ArpTpaMatchFieldSerializer;
22 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.EthDstMatchFieldSerializer;
23 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.EthSrcMatchFieldSerializer;
24 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.EthTypeMatchFieldSerializer;
25 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Icmpv4CodeMatchFieldSerializer;
26 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Icmpv4TypeMatchFieldSerializer;
27 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Icmpv6CodeMatchFieldSerializer;
28 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Icmpv6TypeMatchFieldSerializer;
29 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.InPhyPortMatchFieldSerializer;
30 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.InPortMatchFieldSerializer;
31 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.IpDscpMatchFieldSerializer;
32 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.IpEcnMatchFieldSerializer;
33 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.IpProtoMatchFieldSerializer;
34 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv4DstMatchFieldSerializer;
35 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv4SrcMatchFieldSerializer;
36 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv6DstMatchFieldSerializer;
37 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv6ExtHdrMatchFieldSerializer;
38 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv6FlabelMatchFieldSerializer;
39 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv6NdSllMatchFieldSerializer;
40 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv6NdTargetMatchFieldSerializer;
41 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv6NdTllMatchFieldSerializer;
42 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.Ipv6SrcMatchFieldSerializer;
43 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.MetadataMatchFieldSerializer;
44 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.MplsBosMatchFieldSerializer;
45 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.MplsLabelMatchFieldSerializer;
46 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.MplsTcMatchFieldSerializer;
47 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.PbbIsidMatchFieldSerializer;
48 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.SctpDstMatchFieldSerializer;
49 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.SctpSrcMatchFieldSerializer;
50 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.TcpDstMatchFieldSerializer;
51 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.TcpFlagsMatchFieldSerializer;
52 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.TcpSrcMatchFieldSerializer;
53 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.TunnelIdMatchFieldSerializer;
54 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.UdpDstMatchFieldSerializer;
55 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.UdpSrcMatchFieldSerializer;
56 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.VlanPcpMatchFieldSerializer;
57 import org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield.VlanVidMatchFieldSerializer;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpOp;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpSha;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpSpa;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpTha;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpTpa;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthDst;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthSrc;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthType;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv4Code;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv4Type;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv6Code;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv6Type;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.InPhyPort;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.InPort;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpDscp;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpEcn;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpProto;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv4Dst;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv4Src;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Dst;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Exthdr;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Flabel;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdSll;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdTarget;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdTll;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Src;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MatchField;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Metadata;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsBos;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsLabel;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsTc;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.PbbIsid;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.SctpDst;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.SctpSrc;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TcpDst;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TcpFlags;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TcpSrc;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TunnelId;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TunnelIpv4Dst;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TunnelIpv4Src;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.UdpDst;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.UdpSrc;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.VlanPcp;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.VlanVid;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.set.field.match.SetFieldMatch;
103 import org.opendaylight.yangtools.yang.common.Uint8;
104
105 /**
106  * Util class for injecting new multipart match field serializers into OpenflowJava.
107  */
108 final class MultipartMatchFieldSerializerInjector {
109
110     private MultipartMatchFieldSerializerInjector() {
111     }
112
113     /**
114      * Injects multipart match field serializers into provided
115      * {@link org.opendaylight.openflowjava.protocol.api.extensibility.SerializerExtensionProvider}.
116      *
117      * @param provider OpenflowJava serializer extension provider
118      */
119     static void injectSerializers(final SerializerExtensionProvider provider) {
120         // Inject new message serializers here using injector created by createInjector method
121         final Function<MatchField, Consumer<OFSerializer<SetFieldMatch>>> injector =
122                 createInjector(provider, EncodeConstants.OF_VERSION_1_3);
123
124         injector.apply(ArpOp.VALUE).accept(new ArpOpMatchFieldSerializer());
125         injector.apply(ArpSha.VALUE).accept(new ArpShaMatchFieldSerializer());
126         injector.apply(ArpSpa.VALUE).accept(new ArpSpaMatchFieldSerializer());
127         injector.apply(ArpTha.VALUE).accept(new ArpThaMatchFieldSerializer());
128         injector.apply(ArpTpa.VALUE).accept(new ArpTpaMatchFieldSerializer());
129         injector.apply(EthDst.VALUE).accept(new EthDstMatchFieldSerializer());
130         injector.apply(EthSrc.VALUE).accept(new EthSrcMatchFieldSerializer());
131         injector.apply(EthType.VALUE).accept(new EthTypeMatchFieldSerializer());
132         injector.apply(Icmpv4Code.VALUE).accept(new Icmpv4CodeMatchFieldSerializer());
133         injector.apply(Icmpv4Type.VALUE).accept(new Icmpv4TypeMatchFieldSerializer());
134         injector.apply(Icmpv6Code.VALUE).accept(new Icmpv6CodeMatchFieldSerializer());
135         injector.apply(Icmpv6Type.VALUE).accept(new Icmpv6TypeMatchFieldSerializer());
136         injector.apply(InPhyPort.VALUE).accept(new InPhyPortMatchFieldSerializer());
137         injector.apply(InPort.VALUE).accept(new InPortMatchFieldSerializer());
138         injector.apply(IpDscp.VALUE).accept(new IpDscpMatchFieldSerializer());
139         injector.apply(IpEcn.VALUE).accept(new IpEcnMatchFieldSerializer());
140         injector.apply(IpProto.VALUE).accept(new IpProtoMatchFieldSerializer());
141         injector.apply(Ipv4Dst.VALUE).accept(new Ipv4DstMatchFieldSerializer());
142         injector.apply(Ipv4Src.VALUE).accept(new Ipv4SrcMatchFieldSerializer());
143         injector.apply(Ipv6Dst.VALUE).accept(new Ipv6DstMatchFieldSerializer());
144         injector.apply(Ipv6Exthdr.VALUE).accept(new Ipv6ExtHdrMatchFieldSerializer());
145         injector.apply(Ipv6Flabel.VALUE).accept(new Ipv6FlabelMatchFieldSerializer());
146         injector.apply(Ipv6NdSll.VALUE).accept(new Ipv6NdSllMatchFieldSerializer());
147         injector.apply(Ipv6NdTarget.VALUE).accept(new Ipv6NdTargetMatchFieldSerializer());
148         injector.apply(Ipv6NdTll.VALUE).accept(new Ipv6NdTllMatchFieldSerializer());
149         injector.apply(Ipv6Src.VALUE).accept(new Ipv6SrcMatchFieldSerializer());
150         injector.apply(Metadata.VALUE).accept(new MetadataMatchFieldSerializer());
151         injector.apply(MplsBos.VALUE).accept(new MplsBosMatchFieldSerializer());
152         injector.apply(MplsLabel.VALUE).accept(new MplsLabelMatchFieldSerializer());
153         injector.apply(MplsTc.VALUE).accept(new MplsTcMatchFieldSerializer());
154         injector.apply(PbbIsid.VALUE).accept(new PbbIsidMatchFieldSerializer());
155         injector.apply(SctpDst.VALUE).accept(new SctpDstMatchFieldSerializer());
156         injector.apply(SctpSrc.VALUE).accept(new SctpSrcMatchFieldSerializer());
157         injector.apply(TcpDst.VALUE).accept(new TcpDstMatchFieldSerializer());
158         injector.apply(TcpFlags.VALUE).accept(new TcpFlagsMatchFieldSerializer());
159         injector.apply(TcpSrc.VALUE).accept(new TcpSrcMatchFieldSerializer());
160         injector.apply(TunnelId.VALUE).accept(new TunnelIdMatchFieldSerializer());
161         // TODO: Finish implementation of Tunnel Ipv4 src and dst
162         injector.apply(TunnelIpv4Dst.VALUE).accept(new Ipv4DstMatchFieldSerializer());
163         injector.apply(TunnelIpv4Src.VALUE).accept(new Ipv4SrcMatchFieldSerializer());
164         injector.apply(UdpDst.VALUE).accept(new UdpDstMatchFieldSerializer());
165         injector.apply(UdpSrc.VALUE).accept(new UdpSrcMatchFieldSerializer());
166         injector.apply(VlanPcp.VALUE).accept(new VlanPcpMatchFieldSerializer());
167         injector.apply(VlanVid.VALUE).accept(new VlanVidMatchFieldSerializer());
168     }
169
170     /**
171      * Create injector that will inject new multipart match field features serializers into
172      * #{@link org.opendaylight.openflowjava.protocol.api.extensibility.SerializerExtensionProvider}.
173      *
174      * @param provider OpenflowJava serializer extension provider
175      * @param version  Openflow version
176      * @return injector
177      */
178     @VisibleForTesting
179     static Function<MatchField, Consumer<OFSerializer<SetFieldMatch>>> createInjector(
180             final SerializerExtensionProvider provider, final Uint8 version) {
181         return type -> serializer ->
182                 provider.registerSerializer(new MessageTypeKey<>(version, type.implementedInterface()), serializer);
183     }
184 }