BUG-1277 Add missing openflowplugin.util dependency to distribution pom
[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.common.types.rev130731.Ipv6ExthdrFlags;
14
15 /**
16  * match related tools
17  */
18 public abstract class MatchConvertorUtil {
19
20     /**
21      * @param pField
22      * @return integer containing lower 9 bits filled with corresponding flags
23      */
24     public static Integer ipv6ExthdrFlagsToInt(Ipv6ExthdrFlags pField) {
25         Integer bitmap = 0;
26         bitmap |= pField.isNonext() ? (1 << 0) : 0;
27         bitmap |= pField.isEsp() ?    (1 << 1) : 0;
28         bitmap |= pField.isAuth() ?   (1 << 2) : 0;
29         bitmap |= pField.isDest() ?   (1 << 3) : 0;
30         bitmap |= pField.isFrag() ?   (1 << 4) : 0;
31         bitmap |= pField.isRouter() ? (1 << 5) : 0;
32         bitmap |= pField.isHop() ?    (1 << 6) : 0;
33         bitmap |= pField.isUnrep() ?  (1 << 7) : 0;
34         bitmap |= pField.isUnseq() ?  (1 << 8) : 0;
35         return bitmap;
36     }
37     
38     public static int ipv6NetmaskArrayToCIDRValue(byte[] rawMask){
39
40         /*
41          * Openflow Spec : 1.3.2+
42          * An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
43          * So when user specify 128 as a mask, switch omit that mask and we get null as a mask in flow
44          * statistics response.
45          */
46
47         int maskValue = 128;
48
49         if (rawMask != null) {
50             maskValue = 0;
51             for(int subArrayCounter=0;subArrayCounter<4;subArrayCounter++){
52                 int copyFrom = subArrayCounter * 4;
53
54                 byte[] subArray = Arrays.copyOfRange(rawMask, copyFrom, copyFrom+4);  
55
56                 int receivedMask = ByteBuffer.wrap(subArray).getInt();
57
58                 int shiftCount=0;
59
60                 if (receivedMask == 0) {
61                     break;
62                 }
63
64                 while(receivedMask != 0xffffffff){
65                     receivedMask = receivedMask >> 1;
66                     shiftCount++;
67                 }
68
69                 maskValue = maskValue+(32-shiftCount);
70                 if(shiftCount != 0) {
71                     break;
72                 }
73             }
74         }
75         return maskValue;
76     }
77
78 }