restructure and base modules for different features
[netvirt.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / MetaDataUtil.java
1 package org.opendaylight.vpnservice.mdsalutil;
2
3 import java.math.BigInteger;
4
5 public class MetaDataUtil {
6
7     public static final BigInteger METADATA_NO_MASK = new BigInteger("0000000000000000", 16);
8     public static final BigInteger METADATA_MASK_SCF_MATCH = new BigInteger("FF00000000000000", 16);
9     public static final BigInteger METADATA_MASK_SUBP_MATCH = new BigInteger("00000000FFFF0000", 16);
10     public static final BigInteger METADATA_MASK_APPP_MATCH = new BigInteger("000000000000FFFF", 16);
11     public static final BigInteger METADATA_MASK_LPORT_MATCH = new BigInteger("00FFFF0000000000", 16);
12     public static final BigInteger METADATA_MASK_SCID_MATCH = new BigInteger("000000000000FFFF", 16);
13     public static final BigInteger METADATA_MASK_SCID_WRITE = new BigInteger("000000000000FFFF", 16);
14     public static final BigInteger METADATA_MASK_SUBP_WRITE = new BigInteger("00000000FFFF0000", 16);
15     public static final BigInteger METADATA_MASK_APPP_WRITE = new BigInteger("000000000000FFFF", 16);
16     public static final BigInteger MASK_DMAC_WRITE = new BigInteger("0000FFFFFFFFFFFF", 16);
17     public static final BigInteger METADATA_MASK_SCF_WRITE = new BigInteger("FF00000000000000", 16);
18     public static final BigInteger METADATA_MASK_LPORT_WRITE = new BigInteger("00FFFF0000000000", 16);
19     public static final BigInteger METADATA_MASK_LPORT_TAG = new BigInteger("1FFFFF0000000000", 16);
20     public static final BigInteger METADATA_MASK_SERVICE_INDEX = new BigInteger("E000000000000000", 16);
21     public static final BigInteger METADATA_MASK_SERVICE = new BigInteger("000000FFFF000000", 16);
22     public static final BigInteger METADA_MASK_TUNNEL_ID_VNI = new BigInteger("00000000FFFFFF00", 16);
23     public static final BigInteger METADATA_MASK_LABEL_ITM = new BigInteger("40FFFFFF000000FF", 16);
24     public static final BigInteger METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID = new BigInteger("08000000FFFFFF00", 16);
25     public static final BigInteger METADATA_MASK_LABEL_L3 = new BigInteger("000000FFFF000000", 16);
26     public static final BigInteger METADATA_MASK_VRFID = new BigInteger("00000000FFFFFFFF", 16);
27
28     public static BigInteger getMetadataSCF(int scfTag) {
29         return (new BigInteger("FF", 16).and(BigInteger.valueOf(scfTag))).shiftLeft(56);
30     }
31
32     public static BigInteger getMetadataSCID(int scId) {
33         return BigInteger.valueOf(scId).and(new BigInteger("FFFF", 16));
34     }
35
36     public static BigInteger getMetadataSubProfID(int subProfId) {
37         return (BigInteger.valueOf(subProfId).and(new BigInteger("FFFF", 16))).shiftLeft(16);
38     }
39
40     public static BigInteger getMetadataAppProfID(int appProfId) {
41         return BigInteger.valueOf(appProfId).and(new BigInteger("FFFF", 16));
42     }
43
44     public static BigInteger getMetadataAPPP(int appProfId) {
45         return BigInteger.valueOf(appProfId).and(new BigInteger("FFFF", 16));
46     }
47
48     public static BigInteger getCookieSCFEthTypeFilter(int scfTag) {
49         return MetaDataConstants.COOKIE_SCF_BASE.add(new BigInteger("0120000", 16)).add(BigInteger.valueOf(scfTag));
50     }
51
52     public static BigInteger getCookieSubFilter(int scfTag) {
53         return MetaDataConstants.COOKIE_SCF_BASE.add(new BigInteger("0310000", 16)).add(BigInteger.valueOf(scfTag));
54     }
55
56     public static BigInteger getCookieProfMap(int scfTag) {
57         return MetaDataConstants.COOKIE_SCF_BASE.add(new BigInteger("0510000", 16)).add(BigInteger.valueOf(scfTag));
58     }
59
60     public static BigInteger getCookieSCFAppFilter(int scfTag) {
61         return MetaDataConstants.COOKIE_SCF_BASE.add(new BigInteger("0410000", 16)).add(BigInteger.valueOf(scfTag));
62     }
63
64     public static BigInteger getEthDestForIpNextHop(int groupId) {
65         return BigInteger.valueOf(groupId).and(MASK_DMAC_WRITE);
66     }
67     public static long getIpAddress(byte[] rawIpAddress) {
68         return (((rawIpAddress[0] & 0xFF) << (3 * 8)) + ((rawIpAddress[1] & 0xFF) << (2 * 8))
69                 + ((rawIpAddress[2] & 0xFF) << (1 * 8)) + (rawIpAddress[3] & 0xFF)) & 0xffffffffL;
70     }
71
72     public static BigInteger getMetadataLPort(int lPortTag) {
73         return (new BigInteger("FFFF", 16).and(BigInteger.valueOf(lPortTag))).shiftLeft(40);
74     }
75
76     public static BigInteger getMetadataScHop(int scfInstanceTag, int scfPortTag, int serviceChainId) {
77         return getMetadataSCF(scfInstanceTag).or(getMetadataLPort(scfPortTag)).or(getMetadataSCID(serviceChainId));
78     }
79
80     public static BigInteger getMetadataMaskScHop() {
81         return METADATA_MASK_SCF_WRITE.or(METADATA_MASK_LPORT_WRITE).or(METADATA_MASK_SCID_WRITE);
82     }
83
84     public static BigInteger getCookieSCHop(int scfInstanceTag) {
85         return MetaDataConstants.COOKIE_SCF_BASE.add(new BigInteger("0610000", 16)).add(BigInteger.valueOf(scfInstanceTag));
86     }
87
88     public static BigInteger getMetadataScfPort(int scfInstanceTag, int scfPortTag) {
89         return getMetadataSCF(scfInstanceTag).or(getMetadataLPort(scfPortTag));
90     }
91
92     public static BigInteger getMetadataMaskScfPort() {
93         return METADATA_MASK_LPORT_WRITE.or(METADATA_MASK_SCF_WRITE);
94     }
95
96     public static BigInteger getCookieSCFPort() {
97         return new BigInteger("5000000", 16);
98     }
99
100     public static BigInteger getCookieSCFIpv4EthTypeFilter(int scfInstanceTag) {
101         return MetaDataConstants.COOKIE_SCF_BASE.add(new BigInteger("0100000", 16)).add(BigInteger.valueOf(scfInstanceTag));
102     }
103
104     public static BigInteger getCookieSCFArpEthTypeFilter(int scfInstanceTag) {
105         return MetaDataConstants.COOKIE_SCF_BASE.add(new BigInteger("0110000", 16)).add(BigInteger.valueOf(scfInstanceTag));
106     }
107
108     public static BigInteger getLportTagMetaData(int lportTag) {
109         return new BigInteger("1FFFFF", 16).and(BigInteger.valueOf(lportTag)).shiftLeft(40);
110     }
111
112     public static BigInteger getLportFromMetadata(BigInteger metadata) {
113         return (metadata.and(METADATA_MASK_LPORT_TAG)).shiftRight(40);
114     }
115
116     public static BigInteger getServiceIndexMetaData(int serviceIndex) {
117         return new BigInteger("7", 16).and(BigInteger.valueOf(serviceIndex)).shiftLeft(61);
118     }
119
120     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex) {
121         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag));
122     }
123
124     public static BigInteger getMetaDataMaskForLPortDispatcher() {
125         return METADATA_MASK_SERVICE_INDEX.or(METADATA_MASK_LPORT_TAG);
126     }
127
128     public static BigInteger getWriteMetaDataMaskForServicePorts() {
129         return METADATA_MASK_SERVICE_INDEX.or(METADATA_MASK_LPORT_TAG).or(METADATA_MASK_SERVICE);
130     }
131
132     public static BigInteger getMetaDataMaskForLPortDispatcher(BigInteger metadataMaskForServiceIndex, 
133             BigInteger metadataMaskForLPortTag, BigInteger metadataMaskForService) {
134         return metadataMaskForServiceIndex.or(metadataMaskForLPortTag).or(metadataMaskForService);
135     }
136
137     public static BigInteger getMetaDataForLPortDispatcher(int lportTag, short serviceIndex,
138             BigInteger serviceMetaData) {
139         return getServiceIndexMetaData(serviceIndex).or(getLportTagMetaData(lportTag)).or(serviceMetaData);
140     }
141
142
143     public static BigInteger getVmLportTagMetaData(int vrfId) {
144         return BigInteger.valueOf(vrfId);
145     }
146
147     public static BigInteger getTunnelIdWithVni(int vni) {
148         return BigInteger.valueOf(vni).shiftLeft(8);
149     }
150
151     /**
152      * For the tunnel id with VNI and valid-vni-flag set, the most significant byte
153      * should have 08. So, shifting 08 to 7 bytes (56 bits) and the result is OR-ed with
154      * VNI being shifted to 1 byte.
155      */
156     public static BigInteger getTunnelIdWithValidVniBitAndVniSet(int vni) {
157         return BigInteger.valueOf(0X08).shiftLeft(56).or(BigInteger.valueOf(vni).shiftLeft(8));
158     }
159 }