Fix issues related to checkstyle enforcement on openflow-impl module
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / MatchSerializerInjector.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.util.function.Consumer;
13 import java.util.function.Function;
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.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.openflowplugin.api.openflow.protocol.serialization.MatchEntrySerializer;
19 import org.opendaylight.openflowplugin.api.openflow.protocol.serialization.MatchEntrySerializerRegistry;
20 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.ArpOpEntrySerializer;
21 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.ArpSourceHardwareAddressEntrySerializer;
22 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.ArpSourceTransportAddressEntrySerializer;
23 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.ArpTargetHardwareAddressEntrySerializer;
24 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.ArpTargetTransportAddressEntrySerializer;
25 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.EthernetDestinationEntrySerializer;
26 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.EthernetSourceEntrySerializer;
27 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.EthernetTypeEntrySerializer;
28 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Icmpv4CodeEntrySerializer;
29 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Icmpv4TypeEntrySerializer;
30 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Icmpv6CodeEntrySerializer;
31 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Icmpv6TypeEntrySerializer;
32 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.InPhyPortEntrySerializer;
33 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.InPortEntrySerializer;
34 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.IpDscpEntrySerializer;
35 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.IpEcnEntrySerializer;
36 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.IpProtoEntrySerializer;
37 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv4DestinationEntrySerializer;
38 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv4SourceEntrySerializer;
39 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv6DestinationEntrySerializer;
40 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv6ExtHeaderEntrySerializer;
41 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv6LabelEntrySerializer;
42 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv6NdSllEntrySerializer;
43 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv6NdTargetEntrySerializer;
44 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv6NdTllEntrySerializer;
45 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.Ipv6SourceEntrySerializer;
46 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.MatchEntrySerializerKeyImpl;
47 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.MatchSerializer;
48 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.MetadataEntrySerializer;
49 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.MplsBosEntrySerializer;
50 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.MplsLabelEntrySerializer;
51 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.MplsTcEntrySerializer;
52 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.PbbEntrySerializer;
53 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.SctpDestinationPortEntrySerializer;
54 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.SctpSourcePortEntrySerializer;
55 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.TcpDestinationPortEntrySerializer;
56 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.TcpFlagsEntrySerializer;
57 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.TcpSourcePortEntrySerializer;
58 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.TunnelIdEntrySerializer;
59 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.TunnelIpv4DestinationEntrySerializer;
60 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.TunnelIpv4SourceEntrySerializer;
61 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.UdpDestinationPortEntrySerializer;
62 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.UdpSourcePortEntrySerializer;
63 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.VlanPcpEntrySerializer;
64 import org.opendaylight.openflowplugin.impl.protocol.serialization.match.VlanVidEntrySerializer;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
66
67 /**
68  * Util class for injecting new match serializers into OpenflowJava.
69  */
70 public final class MatchSerializerInjector {
71
72     private MatchSerializerInjector() {
73     }
74
75     /**
76      * Injects match serializers into provided
77      * {@link org.opendaylight.openflowjava.protocol.api.extensibility.SerializerExtensionProvider}.
78      *
79      * @param provider OpenflowJava serializer extension provider
80      */
81     static void injectSerializers(final SerializerExtensionProvider provider) {
82         final MatchSerializer serializer = new MatchSerializer();
83         provider.registerSerializer(
84                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Match.class),
85                 serializer);
86
87         // Inject all match entry serializers to match serializers using injector created by createInjector method
88         final Function<Integer, Function<Integer, Consumer<MatchEntrySerializer>>> injector =
89                 createInjector(serializer, EncodeConstants.OF13_VERSION_ID);
90
91         final Function<Integer, Consumer<MatchEntrySerializer>> basicInjector =
92                 injector.apply(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
93
94         final Function<Integer, Consumer<MatchEntrySerializer>> experInjector =
95                 injector.apply(OxmMatchConstants.EXPERIMENTER_CLASS);
96
97         basicInjector.apply(OxmMatchConstants.IN_PORT).accept(new InPortEntrySerializer());
98         basicInjector.apply(OxmMatchConstants.IN_PHY_PORT).accept(new InPhyPortEntrySerializer());
99         basicInjector.apply(OxmMatchConstants.METADATA).accept(new MetadataEntrySerializer());
100         basicInjector.apply(OxmMatchConstants.ETH_DST).accept(new EthernetDestinationEntrySerializer());
101         basicInjector.apply(OxmMatchConstants.ETH_SRC).accept(new EthernetSourceEntrySerializer());
102         basicInjector.apply(OxmMatchConstants.ETH_TYPE).accept(new EthernetTypeEntrySerializer());
103         basicInjector.apply(OxmMatchConstants.VLAN_VID).accept(new VlanVidEntrySerializer());
104         basicInjector.apply(OxmMatchConstants.VLAN_PCP).accept(new VlanPcpEntrySerializer());
105         basicInjector.apply(OxmMatchConstants.IP_DSCP).accept(new IpDscpEntrySerializer());
106         basicInjector.apply(OxmMatchConstants.IP_ECN).accept(new IpEcnEntrySerializer());
107         basicInjector.apply(OxmMatchConstants.IP_PROTO).accept(new IpProtoEntrySerializer());
108         basicInjector.apply(OxmMatchConstants.TCP_SRC).accept(new TcpSourcePortEntrySerializer());
109         basicInjector.apply(OxmMatchConstants.TCP_DST).accept(new TcpDestinationPortEntrySerializer());
110         basicInjector.apply(OxmMatchConstants.UDP_SRC).accept(new UdpSourcePortEntrySerializer());
111         basicInjector.apply(OxmMatchConstants.UDP_DST).accept(new UdpDestinationPortEntrySerializer());
112         basicInjector.apply(OxmMatchConstants.SCTP_SRC).accept(new SctpSourcePortEntrySerializer());
113         basicInjector.apply(OxmMatchConstants.SCTP_DST).accept(new SctpDestinationPortEntrySerializer());
114         basicInjector.apply(OxmMatchConstants.ICMPV4_TYPE).accept(new Icmpv4TypeEntrySerializer());
115         basicInjector.apply(OxmMatchConstants.ICMPV4_CODE).accept(new Icmpv4CodeEntrySerializer());
116         basicInjector.apply(OxmMatchConstants.ICMPV6_TYPE).accept(new Icmpv6TypeEntrySerializer());
117         basicInjector.apply(OxmMatchConstants.ICMPV6_CODE).accept(new Icmpv6CodeEntrySerializer());
118         basicInjector.apply(OxmMatchConstants.IPV4_SRC).accept(new Ipv4SourceEntrySerializer());
119         basicInjector.apply(OxmMatchConstants.IPV4_DST).accept(new Ipv4DestinationEntrySerializer());
120         experInjector.apply(OxmMatchConstants.NXM_NX_TUN_IPV4_SRC).accept(new TunnelIpv4SourceEntrySerializer());
121         experInjector.apply(OxmMatchConstants.NXM_NX_TUN_IPV4_DST).accept(new TunnelIpv4DestinationEntrySerializer());
122         basicInjector.apply(OxmMatchConstants.ARP_OP).accept(new ArpOpEntrySerializer());
123         basicInjector.apply(OxmMatchConstants.ARP_SPA).accept(new ArpSourceTransportAddressEntrySerializer());
124         basicInjector.apply(OxmMatchConstants.ARP_TPA).accept(new ArpTargetTransportAddressEntrySerializer());
125         basicInjector.apply(OxmMatchConstants.ARP_SHA).accept(new ArpSourceHardwareAddressEntrySerializer());
126         basicInjector.apply(OxmMatchConstants.ARP_THA).accept(new ArpTargetHardwareAddressEntrySerializer());
127         basicInjector.apply(OxmMatchConstants.IPV6_SRC).accept(new Ipv6SourceEntrySerializer());
128         basicInjector.apply(OxmMatchConstants.IPV6_DST).accept(new Ipv6DestinationEntrySerializer());
129         basicInjector.apply(OxmMatchConstants.IPV6_FLABEL).accept(new Ipv6LabelEntrySerializer());
130         basicInjector.apply(OxmMatchConstants.IPV6_ND_TARGET).accept(new Ipv6NdTargetEntrySerializer());
131         basicInjector.apply(OxmMatchConstants.IPV6_ND_SLL).accept(new Ipv6NdSllEntrySerializer());
132         basicInjector.apply(OxmMatchConstants.IPV6_ND_TLL).accept(new Ipv6NdTllEntrySerializer());
133         basicInjector.apply(OxmMatchConstants.IPV6_EXTHDR).accept(new Ipv6ExtHeaderEntrySerializer());
134         basicInjector.apply(OxmMatchConstants.MPLS_LABEL).accept(new MplsLabelEntrySerializer());
135         basicInjector.apply(OxmMatchConstants.MPLS_BOS).accept(new MplsBosEntrySerializer());
136         basicInjector.apply(OxmMatchConstants.MPLS_TC).accept(new MplsTcEntrySerializer());
137         basicInjector.apply(OxmMatchConstants.PBB_ISID).accept(new PbbEntrySerializer());
138         basicInjector.apply(OxmMatchConstants.TUNNEL_ID).accept(new TunnelIdEntrySerializer());
139         experInjector.apply(EncodeConstants.ONFOXM_ET_TCP_FLAGS).accept(new TcpFlagsEntrySerializer());
140     }
141
142     /**
143      * Create injector that will inject new match entry serializers into
144      * #{@link org.opendaylight.openflowplugin.api.openflow.protocol.serialization.MatchEntrySerializerRegistry}.
145      *
146      * @param registry Match entry serializer registry
147      * @param version  Openflow version
148      * @return injector
149      */
150     @VisibleForTesting
151     static Function<Integer, Function<Integer, Consumer<MatchEntrySerializer>>> createInjector(
152             final MatchEntrySerializerRegistry registry,
153             final byte version) {
154         return oxmClass -> oxmField -> serializer ->
155                 registry.registerEntrySerializer(
156                         new MatchEntrySerializerKeyImpl(version, oxmClass, oxmField),
157                         serializer);
158     }
159 }