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