3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.liblldp;
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
20 * Class that represents the Ethernet frame objects
22 public class Ethernet extends Packet {
23 private static final String DMAC = "DestinationMACAddress";
24 private static final String SMAC = "SourceMACAddress";
25 private static final String ETHT = "EtherType";
27 // TODO: This has to be outside and it should be possible for osgi
28 // to add new coming packet classes
29 public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
31 etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
32 etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
34 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
35 private static final long serialVersionUID = 1L;
37 put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
38 put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
39 put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
42 private final Map<String, byte[]> fieldValues;
45 * Default constructor that creates and sets the HashMap
49 fieldValues = new HashMap<String, byte[]>();
50 hdrFieldCoordMap = fieldCoordinates;
51 hdrFieldsMap = fieldValues;
55 * Constructor that sets the access level for the packet and
56 * creates and sets the HashMap
58 public Ethernet(boolean writeAccess) {
60 fieldValues = new HashMap<String, byte[]>();
61 hdrFieldCoordMap = fieldCoordinates;
62 hdrFieldsMap = fieldValues;
66 public void setHeaderField(String headerField, byte[] readValue) {
67 if (headerField.equals(ETHT)) {
68 payloadClass = etherTypeClassMap.get(BitBufferHelper
69 .getShort(readValue));
71 hdrFieldsMap.put(headerField, readValue);
75 * Gets the destination MAC address stored
76 * @return byte[] - the destinationMACAddress
78 public byte[] getDestinationMACAddress() {
79 return fieldValues.get(DMAC);
83 * Gets the source MAC address stored
84 * @return byte[] - the sourceMACAddress
86 public byte[] getSourceMACAddress() {
87 return fieldValues.get(SMAC);
91 * Gets the etherType stored
92 * @return short - the etherType
94 public short getEtherType() {
95 return BitBufferHelper.getShort(fieldValues.get(ETHT));
98 public boolean isBroadcast(){
99 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
102 public boolean isMulticast(){
103 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
107 * Sets the destination MAC address for the current Ethernet object instance
108 * @param byte[] - the destinationMACAddress to set
110 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
111 fieldValues.put(DMAC, destinationMACAddress);
116 * Sets the source MAC address for the current Ethernet object instance
117 * @param byte[] - the sourceMACAddress to set
119 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
120 fieldValues.put(SMAC, sourceMACAddress);
125 * Sets the etherType for the current Ethernet object instance
126 * @param short - the etherType to set
128 public Ethernet setEtherType(short etherType) {
129 byte[] ethType = BitBufferHelper.toByteArray(etherType);
130 fieldValues.put(ETHT, ethType);