Merge "Remove ganymed dependency"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorUtil.java
1 /**
2  * Copyright (c) 2013 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 java.nio.ByteBuffer;
11 import java.util.Arrays;
12
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
15
16 /**
17  * match related tools
18  */
19 public abstract class MatchConvertorUtil {
20
21     private static final String PREFIX_SEPARATOR = "/";
22
23     /**
24      * @param maskMatchEntry
25      * @return subnetwork suffix in form of "/"+<mask value {0..32}>
26      */
27     public static String getIpv4Mask(MaskMatchEntry maskMatchEntry) {
28         int receivedMask = ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
29         int shiftCount = 0;
30         
31         if (receivedMask == 0) {
32             shiftCount = 32;
33         } else {
34             while (receivedMask != 0xffffffff) {
35                 receivedMask = receivedMask >> 1;
36                 shiftCount++;
37                 if (shiftCount >= 32) {
38                     throw new IllegalArgumentException("given mask is invalid: "+Arrays.toString(maskMatchEntry.getMask()));
39                 }
40             }
41         }
42         return PREFIX_SEPARATOR + (32 - shiftCount);
43     }
44
45     /**
46      * @param pField
47      * @return integer containing lower 9 bits filled with corresponding flags
48      */
49     public static Integer ipv6ExthdrFlagsToInt(Ipv6ExthdrFlags pField) {
50         Integer bitmap = 0;
51         bitmap |= pField.isNonext() ? (1 << 0) : 0;
52         bitmap |= pField.isEsp() ? (1 << 1) : 0;
53         bitmap |= pField.isAuth() ? (1 << 2) : 0;
54         bitmap |= pField.isDest() ? (1 << 3) : 0;
55         bitmap |= pField.isFrag() ? (1 << 4) : 0;
56         bitmap |= pField.isRouter() ? (1 << 5) : 0;
57         bitmap |= pField.isHop() ? (1 << 6) : 0;
58         bitmap |= pField.isUnrep() ? (1 << 7) : 0;
59         bitmap |= pField.isUnseq() ? (1 << 8) : 0;
60         return bitmap;
61     }
62
63     public static int ipv6NetmaskArrayToCIDRValue(byte[] rawMask) {
64
65         /*
66          * Openflow Spec : 1.3.2+
67          * An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
68          * So when user specify 128 as a mask, switch omit that mask and we get null as a mask in flow
69          * statistics response.
70          */
71
72         int maskValue = 128;
73
74         if (rawMask != null) {
75             maskValue = 0;
76             for (int subArrayCounter = 0; subArrayCounter < 4; subArrayCounter++) {
77                 int copyFrom = subArrayCounter * 4;
78
79                 byte[] subArray = Arrays.copyOfRange(rawMask, copyFrom, copyFrom + 4);
80
81                 int receivedMask = ByteBuffer.wrap(subArray).getInt();
82
83                 int shiftCount = 0;
84
85                 if (receivedMask == 0) {
86                     break;
87                 }
88
89                 while (receivedMask != 0xffffffff) {
90                     receivedMask = receivedMask >> 1;
91                     shiftCount++;
92                 }
93
94                 maskValue = maskValue + (32 - shiftCount);
95                 if (shiftCount != 0) {
96                     break;
97                 }
98             }
99         }
100         return maskValue;
101     }
102
103 }