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.genius.mdsalutil.packet;
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
14 import org.apache.commons.lang3.tuple.ImmutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.opendaylight.controller.liblldp.BitBufferHelper;
17 import org.opendaylight.controller.liblldp.EtherTypes;
18 import org.opendaylight.controller.liblldp.LLDP;
19 import org.opendaylight.controller.liblldp.NetUtils;
20 import org.opendaylight.controller.liblldp.Packet;
23 * Class that represents the Ethernet frame objects
24 * taken from opendaylight(helium) adsal bundle.
26 public class Ethernet extends Packet {
27 private static final String DMAC = "DestinationMACAddress";
28 private static final String SMAC = "SourceMACAddress";
29 private static final String ETHT = "EtherType";
31 // TODO: This has to be outside and it should be possible for osgi
32 // to add new coming packet classes
33 @SuppressWarnings("checkstyle:ConstantName") // public constant is used in other projects; too late to rename easily
34 public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
37 etherTypeClassMap = new HashMap<>();
38 etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
39 etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
40 etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
41 // TODO: Add support for more classes here
42 etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
43 // etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
44 // etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
45 // etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
48 private static Map<String, Pair<Integer, Integer>> fieldCoordinates
49 = new LinkedHashMap<String, Pair<Integer, Integer>>() { {
50 put(DMAC, new ImmutablePair<>(0, 48));
51 put(SMAC, new ImmutablePair<>(48, 48));
52 put(ETHT, new ImmutablePair<>(96, 16));
55 private final Map<String, byte[]> fieldValues;
58 * Default constructor that creates and sets the HashMap.
62 fieldValues = new HashMap<>();
63 hdrFieldCoordMap = fieldCoordinates;
64 hdrFieldsMap = fieldValues;
68 * Constructor that sets the access level for the packet and
69 * creates and sets the HashMap.
70 * @param writeAccess boolean
72 public Ethernet(boolean writeAccess) {
74 fieldValues = new HashMap<>();
75 hdrFieldCoordMap = fieldCoordinates;
76 hdrFieldsMap = fieldValues;
80 public void setHeaderField(String headerField, byte[] readValue) {
81 if (headerField.equals(ETHT)) {
82 payloadClass = etherTypeClassMap.get(BitBufferHelper
83 .getShort(readValue));
85 hdrFieldsMap.put(headerField, readValue);
88 public byte[] getDestinationMACAddress() {
89 return fieldValues.get(DMAC);
92 public byte[] getSourceMACAddress() {
93 return fieldValues.get(SMAC);
96 public short getEtherType() {
97 return BitBufferHelper.getShort(fieldValues.get(ETHT));
100 public boolean isBroadcast() {
101 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
104 public boolean isMulticast() {
105 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
108 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
109 fieldValues.put(DMAC, destinationMACAddress);
113 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
114 fieldValues.put(SMAC, sourceMACAddress);
118 public Ethernet setEtherType(short etherType) {
119 byte[] ethType = BitBufferHelper.toByteArray(etherType);
120 fieldValues.put(ETHT, ethType);