Merge "ITM Tep Auto Config Unit test using Guice."
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / MetaDataUtil.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.genius.mdsalutil;
9
10 import java.math.BigInteger;
11
12 public class MetaDataUtil {
13     public static final BigInteger METADATA_MASK_LPORT_TAG =     new BigInteger("0FFFFF0000000000", 16);
14     public static final BigInteger METADATA_MASK_SERVICE =       new BigInteger("000000FFFF000000", 16);
15     public static final BigInteger METADATA_MASK_SERVICE_INDEX = new BigInteger("F000000000000000", 16);
16     public static final BigInteger METADATA_MASK_VRFID =         new BigInteger("00000000FFFFFFFE", 16);
17     public static final BigInteger METADATA_MASK_REMOTE_ACL_ID = new BigInteger("0000000000FFFFFE", 16);
18     public static final BigInteger METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID
19         = new BigInteger("08000000FFFFFF00", 16);
20     public static final BigInteger METADATA_MASK_LABEL_ITM =     new BigInteger("40FFFFFF000000FF", 16);
21     public static final BigInteger METADA_MASK_TUNNEL_ID =       new BigInteger("00000000FFFFFF00", 16);
22     public static final BigInteger METADATA_MASK_SERVICE_SH_FLAG = new BigInteger("000000FFFF000001", 16);
23     public static final BigInteger METADATA_MASK_LPORT_TAG_SH_FLAG =     new BigInteger("0FFFFF0000000001", 16);
24     public static final BigInteger METADATA_MASK_SH_FLAG = new BigInteger("0000000000000001", 16);
25     public static final BigInteger METADATA_MASK_ELAN_SUBNET_ROUTE =    new BigInteger("0000FFFF00000000", 16);
26     public static final BigInteger METADATA_MASK_SUBNET_ROUTE =         new BigInteger("0000FFFFFFFFFFFE", 16);
27
28     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex) {
29         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag));
30     }
31
32     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex,
33             BigInteger serviceMetaData) {
34         return getMetaDataForLPortDispatcher(lportTag, serviceIndex, serviceMetaData, false);
35     }
36
37     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex,
38                                                            BigInteger serviceMetaData, boolean isSHFlagSet) {
39         int shBit = isSHFlagSet ? 1 : 0;
40         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag)).or(serviceMetaData)
41                 .or(BigInteger.valueOf(shBit));
42     }
43
44     public static BigInteger getServiceIndexMetaData(int serviceIndex) {
45         return new BigInteger("F", 16).and(BigInteger.valueOf(serviceIndex)).shiftLeft(60);
46     }
47
48     public static BigInteger getLportTagMetaData(int lportTag) {
49         return new BigInteger("FFFFF", 16).and(BigInteger.valueOf(lportTag)).shiftLeft(40);
50     }
51
52     public static BigInteger getMetaDataMaskForLPortDispatcher() {
53         return getMetaDataMaskForLPortDispatcher(METADATA_MASK_LPORT_TAG);
54     }
55
56     public static BigInteger getMetaDataMaskForLPortDispatcher(BigInteger metadataMaskForLPortTag) {
57         return METADATA_MASK_SERVICE_INDEX.or(metadataMaskForLPortTag);
58     }
59
60     public static BigInteger getMetaDataMaskForLPortDispatcher(BigInteger metadataMaskForServiceIndex,
61             BigInteger metadataMaskForLPortTag, BigInteger metadataMaskForService) {
62         return metadataMaskForServiceIndex.or(metadataMaskForLPortTag).or(metadataMaskForService);
63     }
64
65     public static BigInteger getMetadataLPort(int portTag) {
66         return new BigInteger("FFFF", 16).and(BigInteger.valueOf(portTag)).shiftLeft(40);
67     }
68
69     public static BigInteger getLportFromMetadata(BigInteger metadata) {
70         return metadata.and(METADATA_MASK_LPORT_TAG).shiftRight(40);
71     }
72
73     public static int getElanTagFromMetadata(BigInteger metadata) {
74         return metadata.and(MetaDataUtil.METADATA_MASK_SERVICE)
75                 .shiftRight(24).intValue();
76     }
77
78     public static BigInteger getElanTagMetadata(long elanTag) {
79         return BigInteger.valueOf(elanTag).shiftLeft(24);
80     }
81
82     public static int getServiceTagFromMetadata(BigInteger metadata) {
83         return metadata.and(MetaDataUtil.METADATA_MASK_SERVICE_INDEX)
84                 .shiftRight(60).intValue();
85     }
86
87     /**
88      * For the tunnel id with VNI and valid-vni-flag set, the most significant byte
89      * should have 08. So, shifting 08 to 7 bytes (56 bits) and the result is OR-ed with
90      * VNI being shifted to 1 byte.
91      */
92     public static BigInteger getTunnelIdWithValidVniBitAndVniSet(int vni) {
93         return BigInteger.valueOf(0X08).shiftLeft(56).or(BigInteger.valueOf(vni).shiftLeft(8));
94     }
95
96     public static long getNatRouterIdFromMetadata(BigInteger metadata) {
97         return getVpnIdFromMetadata(metadata);
98     }
99
100     public static BigInteger getVpnIdMetadata(long vrfId) {
101         return METADATA_MASK_VRFID.and(BigInteger.valueOf(vrfId).shiftLeft(1));
102     }
103
104     public static long getVpnIdFromMetadata(BigInteger metadata) {
105         return metadata.and(METADATA_MASK_VRFID).shiftRight(1).longValue();
106     }
107
108     public static BigInteger getWriteMetaDataMaskForDispatcherTable() {
109         return new BigInteger("FFFFFFFFFFFFFFFE", 16);
110     }
111
112     public static BigInteger getLportTagForReg6(int lportTag) {
113         return new BigInteger("FFFFF", 16).and(BigInteger.valueOf(lportTag)).shiftLeft(8);
114     }
115
116     public static BigInteger getServiceIndexForReg6(int serviceIndex) {
117         return new BigInteger("F", 16).and(BigInteger.valueOf(serviceIndex)).shiftLeft(28);
118     }
119
120     public static long getReg6ValueForLPortDispatcher(int lportTag, short serviceIndex) {
121         return getServiceIndexForReg6(serviceIndex).or(getLportTagForReg6(lportTag)).longValue();
122     }
123
124 }