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<>();
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<>(0, 48));
37 put(SMAC, new ImmutablePair<>(48, 48));
38 put(ETHT, new ImmutablePair<>(96, 16));
41 private final Map<String, byte[]> fieldValues;
44 * Default constructor that creates and sets the HashMap
47 fieldValues = new HashMap<>();
48 hdrFieldCoordMap = fieldCoordinates;
49 hdrFieldsMap = fieldValues;
53 * Constructor that sets the access level for the packet and
54 * creates and sets the HashMap
56 public Ethernet(final boolean writeAccess) {
58 fieldValues = new HashMap<>();
59 hdrFieldCoordMap = fieldCoordinates;
60 hdrFieldsMap = fieldValues;
64 public void setHeaderField(final String headerField, final byte[] readValue) {
65 if (headerField.equals(ETHT)) {
66 payloadClass = etherTypeClassMap.get(BitBufferHelper
67 .getShort(readValue));
69 hdrFieldsMap.put(headerField, readValue);
73 * Gets the destination MAC address stored
74 * @return byte[] - the destinationMACAddress
76 public byte[] getDestinationMACAddress() {
77 return fieldValues.get(DMAC);
81 * Gets the source MAC address stored
82 * @return byte[] - the sourceMACAddress
84 public byte[] getSourceMACAddress() {
85 return fieldValues.get(SMAC);
89 * Gets the etherType stored
90 * @return short - the etherType
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());
105 * Sets the destination MAC address for the current Ethernet object instance
106 * @param destinationMACAddress the destinationMACAddress to set
108 public Ethernet setDestinationMACAddress(final byte[] destinationMACAddress) {
109 fieldValues.put(DMAC, destinationMACAddress);
114 * Sets the source MAC address for the current Ethernet object instance
115 * @param sourceMACAddress the sourceMACAddress to set
117 public Ethernet setSourceMACAddress(final byte[] sourceMACAddress) {
118 fieldValues.put(SMAC, sourceMACAddress);
123 * Sets the etherType for the current Ethernet object instance
124 * @param etherType the etherType to set
126 public Ethernet setEtherType(final short etherType) {
127 byte[] ethType = BitBufferHelper.toByteArray(etherType);
128 fieldValues.put(ETHT, ethType);