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.sal.packet;
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;
18 import org.opendaylight.controller.sal.utils.EtherTypes;
19 import org.opendaylight.controller.sal.utils.NetUtils;
22 * Class that represents the Ethernet frame objects
24 public class Ethernet extends Packet {
25 private static final String DMAC = "DestinationMACAddress";
26 private static final String SMAC = "SourceMACAddress";
27 private static final String ETHT = "EtherType";
29 // TODO: This has to be outside and it should be possible for osgi
30 // to add new coming packet classes
31 public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
33 etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
34 etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
35 etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
36 etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
37 etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
38 etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
39 etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
40 etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
42 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
43 private static final long serialVersionUID = 1L;
45 put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
46 put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
47 put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
50 private final Map<String, byte[]> fieldValues;
53 * Default constructor that creates and sets the HashMap
57 fieldValues = new HashMap<String, byte[]>();
58 hdrFieldCoordMap = fieldCoordinates;
59 hdrFieldsMap = fieldValues;
63 * Constructor that sets the access level for the packet and
64 * creates and sets the HashMap
66 public Ethernet(boolean writeAccess) {
68 fieldValues = new HashMap<String, byte[]>();
69 hdrFieldCoordMap = fieldCoordinates;
70 hdrFieldsMap = fieldValues;
74 public void setHeaderField(String headerField, byte[] readValue) {
75 if (headerField.equals(ETHT)) {
76 payloadClass = etherTypeClassMap.get(BitBufferHelper
77 .getShort(readValue));
79 hdrFieldsMap.put(headerField, readValue);
83 * Gets the destination MAC address stored
84 * @return byte[] - the destinationMACAddress
86 public byte[] getDestinationMACAddress() {
87 return fieldValues.get(DMAC);
91 * Gets the source MAC address stored
92 * @return byte[] - the sourceMACAddress
94 public byte[] getSourceMACAddress() {
95 return fieldValues.get(SMAC);
99 * Gets the etherType stored
100 * @return short - the etherType
102 public short getEtherType() {
103 return BitBufferHelper.getShort(fieldValues.get(ETHT));
106 public boolean isBroadcast(){
107 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
110 public boolean isMulticast(){
111 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
115 * Sets the destination MAC address for the current Ethernet object instance
116 * @param byte[] - the destinationMACAddress to set
118 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
119 fieldValues.put(DMAC, destinationMACAddress);
124 * Sets the source MAC address for the current Ethernet object instance
125 * @param byte[] - the sourceMACAddress to set
127 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
128 fieldValues.put(SMAC, sourceMACAddress);
133 * Sets the etherType for the current Ethernet object instance
134 * @param short - the etherType to set
136 public Ethernet setEtherType(short etherType) {
137 byte[] ethType = BitBufferHelper.toByteArray(etherType);
138 fieldValues.put(ETHT, ethType);