Merge "Eliminate {infra,service}utils.version"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorUtil.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;
10
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Iterator;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpDscp;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanPcp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpDscpCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanPcpCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.dscp._case.IpDscpBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.pcp._case.VlanPcpBuilder;
25
26 /**
27  * Match related tools.
28  */
29 public final class MatchConvertorUtil {
30     // Pre-calculated masks for the 33 possible values. Do not give them out, but clone() them as they may
31     // end up being leaked and vulnerable.
32     private static final byte[][] IPV4_MASKS;
33
34     static {
35         final byte[][] tmp = new byte[33][];
36         for (int i = 0; i <= 32; ++i) {
37             final int mask = 0xffffffff << 32 - i;
38             tmp[i] = new byte[]{(byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8), (byte) mask};
39         }
40
41         IPV4_MASKS = tmp;
42     }
43
44     private MatchConvertorUtil() {
45     }
46
47     /**
48      * Ipv 6 exthdr flags to int integer.
49      *
50      * @param flags ipv6 external header flag
51      * @return integer containing lower 9 bits filled with corresponding flags
52      */
53     public static Integer ipv6ExthdrFlagsToInt(final Ipv6ExthdrFlags flags) {
54         Integer bitmap = 0;
55         bitmap |= flags.isNonext() ? 1 : 0;
56         bitmap |= flags.isEsp() ? 1 << 1 : 0;
57         bitmap |= flags.isAuth() ? 1 << 2 : 0;
58         bitmap |= flags.isDest() ? 1 << 3 : 0;
59         bitmap |= flags.isFrag() ? 1 << 4 : 0;
60         bitmap |= flags.isRouter() ? 1 << 5 : 0;
61         bitmap |= flags.isHop() ? 1 << 6 : 0;
62         bitmap |= flags.isUnrep() ? 1 << 7 : 0;
63         bitmap |= flags.isUnseq() ? 1 << 8 : 0;
64         return bitmap;
65     }
66
67     /**
68      * Extract ipv 4 mask byte [ ].
69      *
70      * @param addressParts the address parts
71      * @return the byte [ ]
72      */
73     @Nullable
74     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
75     public static byte[] extractIpv4Mask(final Iterator<String> addressParts) {
76         final int prefix;
77         if (addressParts.hasNext()) {
78             int potentionalPrefix = Integer.parseInt(addressParts.next());
79             prefix = potentionalPrefix < 32 ? potentionalPrefix : 0;
80         } else {
81             prefix = 0;
82         }
83
84         if (prefix != 0) {
85             // clone() is necessary to protect our constants
86             return IPV4_MASKS[prefix].clone();
87         }
88
89         return null;
90     }
91
92     /**
93      * To of ip dscp match entry.
94      *
95      * @param ipDscp the ip dscp
96      * @return the match entry
97      */
98     public static MatchEntry toOfIpDscp(final Dscp ipDscp) {
99         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
100         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
101         matchEntryBuilder.setHasMask(false);
102         matchEntryBuilder.setOxmMatchField(IpDscp.class);
103
104         IpDscpCaseBuilder ipDscpCaseBuilder = new IpDscpCaseBuilder();
105         IpDscpBuilder ipDscpBuilder = new IpDscpBuilder();
106         ipDscpBuilder.setDscp(ipDscp);
107         ipDscpCaseBuilder.setIpDscp(ipDscpBuilder.build());
108         matchEntryBuilder.setMatchEntryValue(ipDscpCaseBuilder.build());
109         return matchEntryBuilder.build();
110     }
111
112     /**
113      * To of vlan pcp match entry.
114      *
115      * @param vlanPcp the vlan pcp
116      * @return the match entry
117      */
118     public static MatchEntry toOfVlanPcp(
119             final org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp vlanPcp) {
120         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
121         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
122         matchEntryBuilder.setHasMask(false);
123         matchEntryBuilder.setOxmMatchField(VlanPcp.class);
124         VlanPcpCaseBuilder vlanPcpCaseBuilder = new VlanPcpCaseBuilder();
125         VlanPcpBuilder vlanPcpBuilder = new VlanPcpBuilder();
126         vlanPcpBuilder.setVlanPcp(vlanPcp.getValue());
127         vlanPcpCaseBuilder.setVlanPcp(vlanPcpBuilder.build());
128         matchEntryBuilder.setMatchEntryValue(vlanPcpCaseBuilder.build());
129         return matchEntryBuilder.build();
130     }
131 }