Bug 5540 - PortConvertor, MatchConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / cases / OfToSalTunnelIpv4DstCase.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.openflow.md.core.sal.convertor.match.cases;
10
11 import java.nio.ByteBuffer;
12 import java.util.Optional;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorCase;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IpConversionUtil;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.data.MatchResponseConvertorData;
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.inet.types.rev130715.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv4DstCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.dst._case.Ipv4Dst;
25
26 public class OfToSalTunnelIpv4DstCase extends ConvertorCase<Ipv4DstCase, MatchBuilder, MatchResponseConvertorData> {
27     public OfToSalTunnelIpv4DstCase() {
28         super(Ipv4DstCase.class, true, OFConstants.OFP_VERSION_1_0, OFConstants.OFP_VERSION_1_3);
29     }
30
31     @Override
32     public Optional<MatchBuilder> process(@Nonnull Ipv4DstCase source, MatchResponseConvertorData data) {
33         final MatchBuilder matchBuilder = data.getMatchBuilder();
34         final Ipv4MatchBuilder ipv4MatchBuilder = data.getIpv4MatchBuilder();
35         final TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = data.getTunnelIpv4MatchBuilder();
36
37         Ipv4Dst tunnelIpv4Dst = source.getIpv4Dst();
38         if (tunnelIpv4Dst != null) {
39             String ipv4PrefixStr = tunnelIpv4Dst.getIpv4Address().getValue();
40             byte[] mask = tunnelIpv4Dst.getMask();
41             ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + ByteBuffer.wrap(tunnelIpv4Dst.getMask()).getInt();
42             final Ipv4Prefix ipv4Prefix;
43
44             if (mask != null) {
45                 ipv4Prefix = IpConversionUtil.createPrefix(new Ipv4Address(ipv4PrefixStr), mask);
46             } else {
47                 //Openflow Spec : 1.3.2
48                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
49                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
50                 // statistics response.
51                 ipv4Prefix = IpConversionUtil.createPrefix(new Ipv4Address(ipv4PrefixStr));
52             }
53
54             ipv4MatchBuilder.setIpv4Destination(ipv4Prefix);
55             matchBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
56         }
57
58         return Optional.of(matchBuilder);
59     }
60 }