Merge "Enabling interfacemanager-shell"
[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("00000000FFFFFFFF", 16);
14     public static final BigInteger METADATA_MASK_LPORT_TAG =     new BigInteger("1FFFFF0000000000", 16);
15     public static final BigInteger METADATA_MASK_SERVICE =       new BigInteger("000000FFFF000000", 16);
16     public static final BigInteger METADATA_MASK_SERVICE_INDEX = new BigInteger("E000000000000000", 16);
17     public static final BigInteger METADATA_MASK_LPORT_WRITE =   new BigInteger("00FFFF0000000000", 16);
18     public static final BigInteger METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID = 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("1FFFFF0000000001", 16);
23     public static final BigInteger METADATA_MASK_SUBNET_ROUTE =         new BigInteger("0000FFFFFFFFFFFF", 16);
24
25     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex) {
26         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag));
27     }
28
29     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex,
30                                                            BigInteger serviceMetaData) {
31         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag)).or(serviceMetaData);
32     }
33
34     public static BigInteger getServiceIndexMetaData(int serviceIndex) {
35         return new BigInteger("7", 16).and(BigInteger.valueOf(serviceIndex)).shiftLeft(61);
36     }
37
38     public static BigInteger getLportTagMetaData(int lportTag) {
39         return new BigInteger("1FFFFF", 16).and(BigInteger.valueOf(lportTag)).shiftLeft(40);
40     }
41
42     public static BigInteger getMetaDataMaskForLPortDispatcher() {
43         return METADATA_MASK_SERVICE_INDEX.or(METADATA_MASK_LPORT_TAG);
44     }
45
46     public static BigInteger getMetadataLPort(int lPortTag) {
47         return (new BigInteger("FFFF", 16).and(BigInteger.valueOf(lPortTag))).shiftLeft(40);
48     }
49
50     public static BigInteger getLportFromMetadata(BigInteger metadata) {
51         return (metadata.and(METADATA_MASK_LPORT_TAG)).shiftRight(40);
52     }
53
54     public static int getElanTagFromMetadata(BigInteger metadata) {
55         return (((metadata.and(MetaDataUtil.METADATA_MASK_SERVICE)).
56                 shiftRight(24))).intValue();
57     }
58
59     public static BigInteger getMetaDataMaskForLPortDispatcher(BigInteger metadataMaskForServiceIndex,
60                                                                BigInteger metadataMaskForLPortTag, BigInteger metadataMaskForService) {
61         return metadataMaskForServiceIndex.or(metadataMaskForLPortTag).or(metadataMaskForService);
62     }
63
64     /**
65      * For the tunnel id with VNI and valid-vni-flag set, the most significant byte
66      * should have 08. So, shifting 08 to 7 bytes (56 bits) and the result is OR-ed with
67      * VNI being shifted to 1 byte.
68      */
69     public static BigInteger getTunnelIdWithValidVniBitAndVniSet(int vni) {
70         return BigInteger.valueOf(0X08).shiftLeft(56).or(BigInteger.valueOf(vni).shiftLeft(8));
71     }
72
73     public static long getNatRouterIdFromMetadata(BigInteger metadata){
74         return (metadata.and(METADATA_MASK_VRFID)).longValue();
75     }
76
77     public static BigInteger getWriteMetaDataMaskForDispatcherTable() {
78         return new BigInteger("FFFFFFFFFFFFFFFF", 16);
79     }
80 }