Service Binding on Tunnels - Basic Framework
[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("0000000000FFFFFE", 16);
17     public static final BigInteger METADATA_MASK_REMOTE_ACL_ID = new BigInteger("0000000000FFFFFE", 16);
18     public static final BigInteger METADATA_MASK_POLICY_CLASSIFER_ID = new BigInteger("0000000000FFFFFE", 16);
19     public static final BigInteger METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID
20         = new BigInteger("08000000FFFFFF00", 16);
21     public static final BigInteger METADATA_MASK_LABEL_ITM =     new BigInteger("40FFFFFF000000FF", 16);
22     public static final BigInteger METADA_MASK_TUNNEL_ID =       new BigInteger("00000000FFFFFF00", 16);
23     public static final BigInteger METADATA_MASK_SERVICE_SH_FLAG = new BigInteger("000000FFFF000001", 16);
24     public static final BigInteger METADATA_MASK_LPORT_TAG_SH_FLAG =     new BigInteger("0FFFFF0000000001", 16);
25     public static final BigInteger METADATA_MASK_SH_FLAG = new BigInteger("0000000000000001", 16);
26     public static final BigInteger METADATA_MASK_ELAN_SUBNET_ROUTE =    new BigInteger("000000FFFF000000", 16);
27     public static final BigInteger METADATA_MASK_SUBNET_ROUTE =         new BigInteger("000000FFFFFFFFFE", 16);
28
29     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex) {
30         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag));
31     }
32
33     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex,
34             BigInteger serviceMetaData) {
35         return getMetaDataForLPortDispatcher(lportTag, serviceIndex, serviceMetaData, false);
36     }
37
38     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex,
39                                                            BigInteger serviceMetaData, boolean isSHFlagSet) {
40         int shBit = isSHFlagSet ? 1 : 0;
41         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag)).or(serviceMetaData)
42                 .or(BigInteger.valueOf(shBit));
43     }
44
45     public static BigInteger getPolicyClassifierMetaData(long classifier) {
46         return METADATA_MASK_POLICY_CLASSIFER_ID.and(BigInteger.valueOf(classifier).shiftLeft(1));
47     }
48
49     public static BigInteger getServiceIndexMetaData(int serviceIndex) {
50         return new BigInteger("F", 16).and(BigInteger.valueOf(serviceIndex)).shiftLeft(60);
51     }
52
53     public static BigInteger getLportTagMetaData(int lportTag) {
54         return new BigInteger("FFFFF", 16).and(BigInteger.valueOf(lportTag)).shiftLeft(40);
55     }
56
57     public static BigInteger getMetaDataMaskForLPortDispatcher() {
58         return getMetaDataMaskForLPortDispatcher(METADATA_MASK_LPORT_TAG);
59     }
60
61     public static BigInteger getMetaDataMaskForLPortDispatcher(BigInteger metadataMaskForLPortTag) {
62         return METADATA_MASK_SERVICE_INDEX.or(metadataMaskForLPortTag);
63     }
64
65     public static BigInteger getMetaDataMaskForLPortDispatcher(BigInteger metadataMaskForServiceIndex,
66             BigInteger metadataMaskForLPortTag, BigInteger metadataMaskForService) {
67         return metadataMaskForServiceIndex.or(metadataMaskForLPortTag).or(metadataMaskForService);
68     }
69
70     public static BigInteger getMetadataLPort(int portTag) {
71         return new BigInteger("FFFF", 16).and(BigInteger.valueOf(portTag)).shiftLeft(40);
72     }
73
74     public static BigInteger getLportFromMetadata(BigInteger metadata) {
75         return metadata.and(METADATA_MASK_LPORT_TAG).shiftRight(40);
76     }
77
78     public static int getElanTagFromMetadata(BigInteger metadata) {
79         return metadata.and(MetaDataUtil.METADATA_MASK_SERVICE).shiftRight(24).intValue();
80     }
81
82     public static long getPolicyClassifierFromMetadata(BigInteger metadata) {
83         return metadata.and(METADATA_MASK_POLICY_CLASSIFER_ID).shiftRight(1).longValue();
84     }
85
86     public static BigInteger getElanTagMetadata(long elanTag) {
87         return BigInteger.valueOf(elanTag).shiftLeft(24);
88     }
89
90     public static int getServiceTagFromMetadata(BigInteger metadata) {
91         return metadata.and(MetaDataUtil.METADATA_MASK_SERVICE_INDEX)
92                 .shiftRight(60).intValue();
93     }
94
95     /**
96      * For the tunnel id with VNI and valid-vni-flag set, the most significant byte
97      * should have 08. So, shifting 08 to 7 bytes (56 bits) and the result is OR-ed with
98      * VNI being shifted to 1 byte.
99      */
100     public static BigInteger getTunnelIdWithValidVniBitAndVniSet(int vni) {
101         return BigInteger.valueOf(0X08).shiftLeft(56).or(BigInteger.valueOf(vni).shiftLeft(8));
102     }
103
104     public static long getNatRouterIdFromMetadata(BigInteger metadata) {
105         return getVpnIdFromMetadata(metadata);
106     }
107
108     public static BigInteger getVpnIdMetadata(long vrfId) {
109         return METADATA_MASK_VRFID.and(BigInteger.valueOf(vrfId).shiftLeft(1));
110     }
111
112     public static long getVpnIdFromMetadata(BigInteger metadata) {
113         return metadata.and(METADATA_MASK_VRFID).shiftRight(1).longValue();
114     }
115
116     public static BigInteger getWriteMetaDataMaskForDispatcherTable() {
117         return new BigInteger("FFFFFFFFFFFFFFFE", 16);
118     }
119
120     public static BigInteger getWriteMetaDataMaskForEgressDispatcherTable() {
121         return new BigInteger("000000FFFFFFFFFE", 16);
122     }
123
124     public static BigInteger getLportTagForReg6(int lportTag) {
125         return new BigInteger("FFFFF", 16).and(BigInteger.valueOf(lportTag)).shiftLeft(8);
126     }
127
128     public static BigInteger getServiceIndexForReg6(int serviceIndex) {
129         return new BigInteger("F", 16).and(BigInteger.valueOf(serviceIndex)).shiftLeft(28);
130     }
131
132     public static BigInteger getInterfaceTypeForReg6(int tunnelType) {
133         return new BigInteger("F", 16).and(BigInteger.valueOf(tunnelType)).shiftLeft(4);
134     }
135
136     public static long getReg6ValueForLPortDispatcher(int lportTag, short serviceIndex) {
137         return getServiceIndexForReg6(serviceIndex).or(getLportTagForReg6(lportTag)).longValue();
138     }
139
140     /** Utility to fetch the register value for lport dispatcher table.
141      * Register6 used for service binding will have first 4 bits of service-index, next 20 bits for lportTag,
142      * and next 4 bits for interface-type
143      */
144     public static long getReg6ValueForLPortDispatcher(int lportTag, short serviceIndex, short interfaceType) {
145         return getServiceIndexForReg6(serviceIndex).or(getLportTagForReg6(lportTag)
146                 .or(getInterfaceTypeForReg6(interfaceType))).longValue();
147     }
148
149     public static long getLportTagMaskForReg6() {
150         return METADATA_MASK_LPORT_TAG.shiftRight(32).longValue();
151     }
152
153     public static long getElanMaskForReg() {
154         return METADATA_MASK_SERVICE.shiftRight(24).longValue();
155     }
156
157     public static long getVpnIdMaskForReg() {
158         return METADATA_MASK_VRFID.shiftRight(1).longValue();
159     }
160
161     public static BigInteger mergeMetadataValues(BigInteger metadata, BigInteger metadata2) {
162         return metadata.or(metadata2);
163     }
164
165     public static BigInteger mergeMetadataMask(BigInteger mask, BigInteger mask2) {
166         return mask.or(mask2);
167     }
168 }