Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / packet / Ethernet.java
1 /*
2  * Copyright (c) 2013, 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.vpnservice.mdsalutil.packet;
10
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.opendaylight.controller.liblldp.BitBufferHelper;
18 import org.opendaylight.controller.liblldp.EtherTypes;
19 import org.opendaylight.controller.liblldp.LLDP;
20 import org.opendaylight.controller.liblldp.NetUtils;
21 import org.opendaylight.controller.liblldp.Packet;
22
23 /**
24  * Class that represents the Ethernet frame objects
25  * taken from opendaylight(helium) adsal bundle
26  */
27 public class Ethernet extends Packet {
28     private static final String DMAC = "DestinationMACAddress";
29     private static final String SMAC = "SourceMACAddress";
30     private static final String ETHT = "EtherType";
31
32     // TODO: This has to be outside and it should be possible for osgi
33     // to add new coming packet classes
34     public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
35     static {
36         etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
37         etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
38         etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
39         etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
40         // TODO: Add support for more classes here
41         etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
42         // etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
43         // etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
44         // etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
45     }
46     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
47         private static final long serialVersionUID = 1L;
48         {
49             put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
50             put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
51             put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
52         }
53     };
54     private final Map<String, byte[]> fieldValues;
55
56     /**
57      * Default constructor that creates and sets the HashMap
58      */
59     public Ethernet() {
60         super();
61         fieldValues = new HashMap<String, byte[]>();
62         hdrFieldCoordMap = fieldCoordinates;
63         hdrFieldsMap = fieldValues;
64     }
65
66     /**
67      * Constructor that sets the access level for the packet and
68      * creates and sets the HashMap
69      * @param writeAccess boolean
70      */
71     public Ethernet(boolean writeAccess) {
72         super(writeAccess);
73         fieldValues = new HashMap<String, byte[]>();
74         hdrFieldCoordMap = fieldCoordinates;
75         hdrFieldsMap = fieldValues;
76     }
77
78     @Override
79     public void setHeaderField(String headerField, byte[] readValue) {
80         if (headerField.equals(ETHT)) {
81             payloadClass = etherTypeClassMap.get(BitBufferHelper
82                     .getShort(readValue));
83         }
84         hdrFieldsMap.put(headerField, readValue);
85     }
86
87     public byte[] getDestinationMACAddress() {
88         return fieldValues.get(DMAC);
89     }
90
91     public byte[] getSourceMACAddress() {
92         return fieldValues.get(SMAC);
93     }
94
95     public short getEtherType() {
96         return BitBufferHelper.getShort(fieldValues.get(ETHT));
97     }
98
99     public boolean isBroadcast(){
100         return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
101     }
102
103     public boolean isMulticast(){
104         return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
105     }
106
107     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
108         fieldValues.put(DMAC, destinationMACAddress);
109         return this;
110     }
111
112     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
113         fieldValues.put(SMAC, sourceMACAddress);
114         return this;
115     }
116
117     public Ethernet setEtherType(short etherType) {
118         byte[] ethType = BitBufferHelper.toByteArray(etherType);
119         fieldValues.put(ETHT, ethType);
120         return this;
121     }
122
123 }