2 * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.mdsalutil.packet;
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
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;
24 * Class that represents the Ethernet frame objects
25 * taken from opendaylight(helium) adsal bundle
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";
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;
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);
46 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
47 private static final long serialVersionUID = 1L;
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));
54 private final Map<String, byte[]> fieldValues;
57 * Default constructor that creates and sets the HashMap
61 fieldValues = new HashMap<String, byte[]>();
62 hdrFieldCoordMap = fieldCoordinates;
63 hdrFieldsMap = fieldValues;
67 * Constructor that sets the access level for the packet and
68 * creates and sets the HashMap
69 * @param writeAccess boolean
71 public Ethernet(boolean writeAccess) {
73 fieldValues = new HashMap<String, byte[]>();
74 hdrFieldCoordMap = fieldCoordinates;
75 hdrFieldsMap = fieldValues;
79 public void setHeaderField(String headerField, byte[] readValue) {
80 if (headerField.equals(ETHT)) {
81 payloadClass = etherTypeClassMap.get(BitBufferHelper
82 .getShort(readValue));
84 hdrFieldsMap.put(headerField, readValue);
87 public byte[] getDestinationMACAddress() {
88 return fieldValues.get(DMAC);
91 public byte[] getSourceMACAddress() {
92 return fieldValues.get(SMAC);
95 public short getEtherType() {
96 return BitBufferHelper.getShort(fieldValues.get(ETHT));
99 public boolean isBroadcast(){
100 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
103 public boolean isMulticast(){
104 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
107 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
108 fieldValues.put(DMAC, destinationMACAddress);
112 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
113 fieldValues.put(SMAC, sourceMACAddress);
117 public Ethernet setEtherType(short etherType) {
118 byte[] ethType = BitBufferHelper.toByteArray(etherType);
119 fieldValues.put(ETHT, ethType);