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.match.Match;
19 import org.opendaylight.controller.sal.match.MatchType;
20 import org.opendaylight.controller.sal.utils.EtherTypes;
21 import org.opendaylight.controller.sal.utils.NetUtils;
24 * Class that represents the Ethernet frame objects
27 public class Ethernet extends Packet {
28 private static final String DMAC = "DestinationMACAddress";
29 private static final String SMAC = "SourceMACAddress";
30 private static final String ETHT = "EtherType";
32 // TODO: This has to be outside and it should be possible for osgi
33 // to add new coming packet classes
34 public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
36 etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
37 etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
38 etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
39 etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
40 etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
41 etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
42 etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
43 etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
45 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
46 private static final long serialVersionUID = 1L;
48 put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
49 put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
50 put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
53 private final Map<String, byte[]> fieldValues;
56 * Default constructor that creates and sets the HashMap
60 fieldValues = new HashMap<String, byte[]>();
61 hdrFieldCoordMap = fieldCoordinates;
62 hdrFieldsMap = fieldValues;
66 * Constructor that sets the access level for the packet and
67 * creates and sets the HashMap
69 public Ethernet(boolean writeAccess) {
71 fieldValues = new HashMap<String, byte[]>();
72 hdrFieldCoordMap = fieldCoordinates;
73 hdrFieldsMap = fieldValues;
77 public void setHeaderField(String headerField, byte[] readValue) {
78 if (headerField.equals(ETHT)) {
79 payloadClass = etherTypeClassMap.get(BitBufferHelper
80 .getShort(readValue));
82 hdrFieldsMap.put(headerField, readValue);
86 * Gets the destination MAC address stored
87 * @return byte[] - the destinationMACAddress
89 public byte[] getDestinationMACAddress() {
90 return fieldValues.get(DMAC);
94 * Gets the source MAC address stored
95 * @return byte[] - the sourceMACAddress
97 public byte[] getSourceMACAddress() {
98 return fieldValues.get(SMAC);
102 * Gets the etherType stored
103 * @return short - the etherType
105 public short getEtherType() {
106 return BitBufferHelper.getShort(fieldValues.get(ETHT));
109 public boolean isBroadcast(){
110 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
113 public boolean isMulticast(){
114 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
118 * Sets the destination MAC address for the current Ethernet object instance
119 * @param byte[] - the destinationMACAddress to set
121 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
122 fieldValues.put(DMAC, destinationMACAddress);
127 * Sets the source MAC address for the current Ethernet object instance
128 * @param byte[] - the sourceMACAddress to set
130 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
131 fieldValues.put(SMAC, sourceMACAddress);
136 * Sets the etherType for the current Ethernet object instance
137 * @param short - the etherType to set
139 public Ethernet setEtherType(short etherType) {
140 byte[] ethType = BitBufferHelper.toByteArray(etherType);
141 fieldValues.put(ETHT, ethType);
146 public void populateMatch(Match match) {
147 match.setField(MatchType.DL_SRC, this.getSourceMACAddress());
148 match.setField(MatchType.DL_DST, this.getDestinationMACAddress());
149 match.setField(MatchType.DL_TYPE, this.getEtherType());