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 // TODO: Add support for more classes here
39 // etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
40 // etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
41 // etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
42 // etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
44 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
45 private static final long serialVersionUID = 1L;
47 put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
48 put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
49 put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
52 private final Map<String, byte[]> fieldValues;
55 * Default constructor that creates and sets the HashMap
59 fieldValues = new HashMap<String, byte[]>();
60 hdrFieldCoordMap = fieldCoordinates;
61 hdrFieldsMap = fieldValues;
65 * Constructor that sets the access level for the packet and
66 * creates and sets the HashMap
68 public Ethernet(boolean writeAccess) {
70 fieldValues = new HashMap<String, byte[]>();
71 hdrFieldCoordMap = fieldCoordinates;
72 hdrFieldsMap = fieldValues;
76 public void setHeaderField(String headerField, byte[] readValue) {
77 if (headerField.equals(ETHT)) {
78 payloadClass = etherTypeClassMap.get(BitBufferHelper
79 .getShort(readValue));
81 hdrFieldsMap.put(headerField, readValue);
84 public byte[] getDestinationMACAddress() {
85 return fieldValues.get(DMAC);
88 public byte[] getSourceMACAddress() {
89 return fieldValues.get(SMAC);
92 public short getEtherType() {
93 return BitBufferHelper.getShort(fieldValues.get(ETHT));
96 public boolean isBroadcast(){
97 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
100 public boolean isMulticast(){
101 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
104 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
105 fieldValues.put(DMAC, destinationMACAddress);
109 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
110 fieldValues.put(SMAC, sourceMACAddress);
114 public Ethernet setEtherType(short etherType) {
115 byte[] ethType = BitBufferHelper.toByteArray(etherType);
116 fieldValues.put(ETHT, ethType);