Bump upstreams for 2022.09 Chlorine
[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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.util.Iterator;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpDscp;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanPcp;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpDscpCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanPcpCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.dscp._case.IpDscpBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.pcp._case.VlanPcpBuilder;
24
25 /**
26  * Match related tools.
27  */
28 public final class MatchConvertorUtil {
29     // Pre-calculated masks for the 33 possible values. Do not give them out, but clone() them as they may
30     // end up being leaked and vulnerable.
31     private static final byte[][] IPV4_MASKS;
32
33     static {
34         final byte[][] tmp = new byte[33][];
35         for (int i = 0; i <= 32; ++i) {
36             final int mask = 0xffffffff << 32 - i;
37             tmp[i] = new byte[]{(byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8), (byte) mask};
38         }
39
40         IPV4_MASKS = tmp;
41     }
42
43     private MatchConvertorUtil() {
44     }
45
46     /**
47      * Ipv 6 exthdr flags to int integer.
48      *
49      * @param flags ipv6 external header flag
50      * @return integer containing lower 9 bits filled with corresponding flags
51      */
52     public static int ipv6ExthdrFlagsToInt(final Ipv6ExthdrFlags flags) {
53         // FIXME: this should be 'int'
54         int bitmap = 0;
55         // FIXME: use explicit if (flags) { bitmap |= 1 << 2; }
56         bitmap |= flags.getNonext() ? 1 : 0;
57         bitmap |= flags.getEsp() ? 1 << 1 : 0;
58         bitmap |= flags.getAuth() ? 1 << 2 : 0;
59         bitmap |= flags.getDest() ? 1 << 3 : 0;
60         bitmap |= flags.getFrag() ? 1 << 4 : 0;
61         bitmap |= flags.getRouter() ? 1 << 5 : 0;
62         bitmap |= flags.getHop() ? 1 << 6 : 0;
63         bitmap |= flags.getUnrep() ? 1 << 7 : 0;
64         bitmap |= flags.getUnseq() ? 1 << 8 : 0;
65         return bitmap;
66     }
67
68     /**
69      * Extract ipv 4 mask byte [ ].
70      *
71      * @param addressParts the address parts
72      * @return the byte [ ]
73      */
74     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
75     public static byte @Nullable[] 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.VALUE);
101         matchEntryBuilder.setHasMask(false);
102         matchEntryBuilder.setOxmMatchField(IpDscp.VALUE);
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.VALUE);
122         matchEntryBuilder.setHasMask(false);
123         matchEntryBuilder.setOxmMatchField(VlanPcp.VALUE);
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 }