Remove invalid annotations
[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     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
74     public static byte @Nullable[] extractIpv4Mask(final Iterator<String> addressParts) {
75         final int prefix;
76         if (addressParts.hasNext()) {
77             int potentionalPrefix = Integer.parseInt(addressParts.next());
78             prefix = potentionalPrefix < 32 ? potentionalPrefix : 0;
79         } else {
80             prefix = 0;
81         }
82
83         if (prefix != 0) {
84             // clone() is necessary to protect our constants
85             return IPV4_MASKS[prefix].clone();
86         }
87
88         return null;
89     }
90
91     /**
92      * To of ip dscp match entry.
93      *
94      * @param ipDscp the ip dscp
95      * @return the match entry
96      */
97     public static MatchEntry toOfIpDscp(final Dscp ipDscp) {
98         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
99         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
100         matchEntryBuilder.setHasMask(false);
101         matchEntryBuilder.setOxmMatchField(IpDscp.class);
102
103         IpDscpCaseBuilder ipDscpCaseBuilder = new IpDscpCaseBuilder();
104         IpDscpBuilder ipDscpBuilder = new IpDscpBuilder();
105         ipDscpBuilder.setDscp(ipDscp);
106         ipDscpCaseBuilder.setIpDscp(ipDscpBuilder.build());
107         matchEntryBuilder.setMatchEntryValue(ipDscpCaseBuilder.build());
108         return matchEntryBuilder.build();
109     }
110
111     /**
112      * To of vlan pcp match entry.
113      *
114      * @param vlanPcp the vlan pcp
115      * @return the match entry
116      */
117     public static MatchEntry toOfVlanPcp(
118             final org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp vlanPcp) {
119         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
120         matchEntryBuilder.setOxmClass(OpenflowBasicClass.class);
121         matchEntryBuilder.setHasMask(false);
122         matchEntryBuilder.setOxmMatchField(VlanPcp.class);
123         VlanPcpCaseBuilder vlanPcpCaseBuilder = new VlanPcpCaseBuilder();
124         VlanPcpBuilder vlanPcpBuilder = new VlanPcpBuilder();
125         vlanPcpBuilder.setVlanPcp(vlanPcp.getValue());
126         vlanPcpCaseBuilder.setVlanPcp(vlanPcpBuilder.build());
127         matchEntryBuilder.setMatchEntryValue(vlanPcpCaseBuilder.build());
128         return matchEntryBuilder.build();
129     }
130 }