2 * Copyright (c) 2013 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.controller.liblldp;
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;
19 * Class that represents the Ethernet frame objects
21 public class Ethernet extends Packet {
22 private static final String DMAC = "DestinationMACAddress";
23 private static final String SMAC = "SourceMACAddress";
24 private static final String ETHT = "EtherType";
26 // TODO: This has to be outside and it should be possible for osgi
27 // to add new coming packet classes
28 public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
30 etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
31 etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
33 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
34 private static final long serialVersionUID = 1L;
36 put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
37 put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
38 put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
41 private final Map<String, byte[]> fieldValues;
44 * Default constructor that creates and sets the HashMap
48 fieldValues = new HashMap<String, byte[]>();
49 hdrFieldCoordMap = fieldCoordinates;
50 hdrFieldsMap = fieldValues;
54 * Constructor that sets the access level for the packet and
55 * creates and sets the HashMap
57 public Ethernet(boolean writeAccess) {
59 fieldValues = new HashMap<String, byte[]>();
60 hdrFieldCoordMap = fieldCoordinates;
61 hdrFieldsMap = fieldValues;
65 public void setHeaderField(String headerField, byte[] readValue) {
66 if (headerField.equals(ETHT)) {
67 payloadClass = etherTypeClassMap.get(BitBufferHelper
68 .getShort(readValue));
70 hdrFieldsMap.put(headerField, readValue);
74 * Gets the destination MAC address stored
75 * @return byte[] - the destinationMACAddress
77 public byte[] getDestinationMACAddress() {
78 return fieldValues.get(DMAC);
82 * Gets the source MAC address stored
83 * @return byte[] - the sourceMACAddress
85 public byte[] getSourceMACAddress() {
86 return fieldValues.get(SMAC);
90 * Gets the etherType stored
91 * @return short - the etherType
93 public short getEtherType() {
94 return BitBufferHelper.getShort(fieldValues.get(ETHT));
97 public boolean isBroadcast(){
98 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
101 public boolean isMulticast(){
102 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
106 * Sets the destination MAC address for the current Ethernet object instance
107 * @param byte[] - the destinationMACAddress to set
109 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
110 fieldValues.put(DMAC, destinationMACAddress);
115 * Sets the source MAC address for the current Ethernet object instance
116 * @param byte[] - the sourceMACAddress to set
118 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
119 fieldValues.put(SMAC, sourceMACAddress);
124 * Sets the etherType for the current Ethernet object instance
125 * @param short - the etherType to set
127 public Ethernet setEtherType(short etherType) {
128 byte[] ethType = BitBufferHelper.toByteArray(etherType);
129 fieldValues.put(ETHT, ethType);