Bug 5540 - PortConvertor, MatchConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / cases / OfToSalTunnelIpv4SrcCase.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.Ipv4SrcCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.src._case.Ipv4Src;
25
26 public class OfToSalTunnelIpv4SrcCase extends ConvertorCase<Ipv4SrcCase, MatchBuilder, MatchResponseConvertorData> {
27     public OfToSalTunnelIpv4SrcCase() {
28         super(Ipv4SrcCase.class, true, OFConstants.OFP_VERSION_1_0, OFConstants.OFP_VERSION_1_3);
29     }
30
31     @Override
32     public Optional<MatchBuilder> process(@Nonnull Ipv4SrcCase source, MatchResponseConvertorData data) {
33         final MatchBuilder matchBuilder = data.getMatchBuilder();
34         final Ipv4MatchBuilder ipv4MatchBuilder = data.getIpv4MatchBuilder();
35         final TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = data.getTunnelIpv4MatchBuilder();
36
37         Ipv4Src tunnelIpv4Dst = source.getIpv4Src();
38
39         if (tunnelIpv4Dst != null) {
40             String ipv4PrefixStr = tunnelIpv4Dst.getIpv4Address().getValue();
41             byte[] mask = tunnelIpv4Dst.getMask();
42             ipv4PrefixStr += IpConversionUtil.PREFIX_SEPARATOR + ByteBuffer.wrap(tunnelIpv4Dst.getMask()).getInt();
43
44             final Ipv4Prefix ipv4Prefix;
45             if (mask != null) {
46                 ipv4Prefix = IpConversionUtil.createPrefix(new Ipv4Address(ipv4PrefixStr), mask);
47             } else {
48                 //Openflow Spec : 1.3.2
49                 //An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
50                 // So when user specify 32 as a mast, switch omit that mast and we get null as a mask in flow
51                 // statistics response.
52                 ipv4Prefix = IpConversionUtil.createPrefix(new Ipv4Address(ipv4PrefixStr));
53             }
54
55             ipv4MatchBuilder.setIpv4Source(ipv4Prefix);
56             matchBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
57         }
58
59         return Optional.of(matchBuilder);
60     }
61 }