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
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 public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
35 etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
36 etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
37 etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
38 etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
39 etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
40 etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
41 etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
42 etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
44 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
45 private static final long serialVersionUID = 1L;
47 put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
48 put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
49 put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
52 private final Map<String, byte[]> fieldValues;
55 * Default constructor that creates and sets the HashMap
59 fieldValues = new HashMap<String, byte[]>();
60 hdrFieldCoordMap = fieldCoordinates;
61 hdrFieldsMap = fieldValues;
65 * Constructor that sets the access level for the packet and
66 * creates and sets the HashMap
68 public Ethernet(boolean writeAccess) {
70 fieldValues = new HashMap<String, byte[]>();
71 hdrFieldCoordMap = fieldCoordinates;
72 hdrFieldsMap = fieldValues;
76 public void setHeaderField(String headerField, byte[] readValue) {
77 if (headerField.equals(ETHT)) {
78 payloadClass = etherTypeClassMap.get(BitBufferHelper
79 .getShort(readValue));
81 hdrFieldsMap.put(headerField, readValue);
85 * Gets the destination MAC address stored
86 * @return byte[] - the destinationMACAddress
88 public byte[] getDestinationMACAddress() {
89 return fieldValues.get(DMAC);
93 * Gets the source MAC address stored
94 * @return byte[] - the sourceMACAddress
96 public byte[] getSourceMACAddress() {
97 return fieldValues.get(SMAC);
101 * Gets the etherType stored
102 * @return short - the etherType
104 public short getEtherType() {
105 return BitBufferHelper.getShort(fieldValues.get(ETHT));
108 public boolean isBroadcast(){
109 return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
112 public boolean isMulticast(){
113 return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
117 * Sets the destination MAC address for the current Ethernet object instance
118 * @param byte[] - the destinationMACAddress to set
120 public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
121 fieldValues.put(DMAC, destinationMACAddress);
126 * Sets the source MAC address for the current Ethernet object instance
127 * @param byte[] - the sourceMACAddress to set
129 public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
130 fieldValues.put(SMAC, sourceMACAddress);
135 * Sets the etherType for the current Ethernet object instance
136 * @param short - the etherType to set
138 public Ethernet setEtherType(short etherType) {
139 byte[] ethType = BitBufferHelper.toByteArray(etherType);
140 fieldValues.put(ETHT, ethType);
145 public void populateMatch(Match match) {
146 match.setField(MatchType.DL_SRC, this.getSourceMACAddress());
147 match.setField(MatchType.DL_DST, this.getDestinationMACAddress());
148 match.setField(MatchType.DL_TYPE, this.getEtherType());