deb8c7573fd7b457b6f8177987179b839aff4774
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / MetaDataUtil.java
1 /*
2  * Copyright (c) 2015 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.vpnservice.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
21     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex) {
22         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag));
23     }
24
25     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex,
26                                                            BigInteger serviceMetaData) {
27         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag)).or(serviceMetaData);
28     }
29
30     public static BigInteger getServiceIndexMetaData(int serviceIndex) {
31         return new BigInteger("7", 16).and(BigInteger.valueOf(serviceIndex)).shiftLeft(61);
32     }
33
34     public static BigInteger getLportTagMetaData(int lportTag) {
35         return new BigInteger("1FFFFF", 16).and(BigInteger.valueOf(lportTag)).shiftLeft(40);
36     }
37
38     public static BigInteger getMetaDataMaskForLPortDispatcher() {
39         return METADATA_MASK_SERVICE_INDEX.or(METADATA_MASK_LPORT_TAG);
40     }
41
42     public static BigInteger getMetadataLPort(int lPortTag) {
43         return (new BigInteger("FFFF", 16).and(BigInteger.valueOf(lPortTag))).shiftLeft(40);
44     }
45
46     public static BigInteger getLportFromMetadata(BigInteger metadata) {
47         return (metadata.and(METADATA_MASK_LPORT_TAG)).shiftRight(40);
48     }
49
50     public static int getElanTagFromMetadata(BigInteger metadata) {
51         return (((metadata.and(MetaDataUtil.METADATA_MASK_SERVICE)).
52                 shiftRight(24))).intValue();
53     }
54
55     public static BigInteger getMetaDataMaskForLPortDispatcher(BigInteger metadataMaskForServiceIndex,
56                                                                BigInteger metadataMaskForLPortTag, BigInteger metadataMaskForService) {
57         return metadataMaskForServiceIndex.or(metadataMaskForLPortTag).or(metadataMaskForService);
58     }
59
60     /**
61      * For the tunnel id with VNI and valid-vni-flag set, the most significant byte
62      * should have 08. So, shifting 08 to 7 bytes (56 bits) and the result is OR-ed with
63      * VNI being shifted to 1 byte.
64      */
65     public static BigInteger getTunnelIdWithValidVniBitAndVniSet(int vni) {
66         return BigInteger.valueOf(0X08).shiftLeft(56).or(BigInteger.valueOf(vni).shiftLeft(8));
67     }
68 }