ICMP fix and Packet class should store and provide access to the raw payload in case...
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / Ethernet.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.sal.packet;
11
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15
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
20 /**
21  * Class that represents the Ethernet frame objects
22  */
23 public class Ethernet extends Packet {
24     private static final String DMAC = "DestinationMACAddress";
25     private static final String SMAC = "SourceMACAddress";
26     private static final String ETHT = "EtherType";
27
28     // TODO: This has to be outside and it should be possible for osgi
29     // to add new coming packet classes
30     public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
31     static {
32         etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
33         etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
34         etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
35         etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
36     }
37     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
38         private static final long serialVersionUID = 1L;
39         {
40             put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
41             put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
42             put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
43         }
44     };
45     private final Map<String, byte[]> fieldValues;
46
47     /**
48      * Default constructor that creates and sets the HashMap
49      */
50     public Ethernet() {
51         super();
52         fieldValues = new HashMap<String, byte[]>();
53         hdrFieldCoordMap = fieldCoordinates;
54         hdrFieldsMap = fieldValues;
55     }
56
57     /**
58      * Constructor that sets the access level for the packet and
59      * creates and sets the HashMap
60      */
61     public Ethernet(boolean writeAccess) {
62         super(writeAccess);
63         fieldValues = new HashMap<String, byte[]>();
64         hdrFieldCoordMap = fieldCoordinates;
65         hdrFieldsMap = fieldValues;
66     }
67
68     @Override
69     public void setHeaderField(String headerField, byte[] readValue) {
70         if (headerField.equals(ETHT)) {
71             payloadClass = etherTypeClassMap.get(BitBufferHelper
72                     .getShort(readValue));
73         }
74         hdrFieldsMap.put(headerField, readValue);
75     }
76
77     /**
78      * Gets the destination MAC address stored
79      * @return byte[] - the destinationMACAddress
80      */
81     public byte[] getDestinationMACAddress() {
82         return fieldValues.get(DMAC);
83     }
84
85     /**
86      * Gets the source MAC address stored
87      * @return byte[] - the sourceMACAddress
88      */
89     public byte[] getSourceMACAddress() {
90         return fieldValues.get(SMAC);
91     }
92
93     /**
94      * Gets the etherType stored
95      * @return short - the etherType
96      */
97     public short getEtherType() {
98         return BitBufferHelper.getShort(fieldValues.get(ETHT));
99     }
100
101     /**
102      * Sets the destination MAC address for the current Ethernet object instance
103      * @param byte[] - the destinationMACAddress to set
104      */
105     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
106         fieldValues.put(DMAC, destinationMACAddress);
107         return this;
108     }
109
110     /**
111      * Sets the source MAC address for the current Ethernet object instance
112      * @param byte[] - the sourceMACAddress to set
113      */
114     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
115         fieldValues.put(SMAC, sourceMACAddress);
116         return this;
117     }
118
119     /**
120      * Sets the etherType for the current Ethernet object instance
121      * @param short - the etherType to set
122      */
123     public Ethernet setEtherType(short etherType) {
124         byte[] ethType = BitBufferHelper.toByteArray(etherType);
125         fieldValues.put(ETHT, ethType);
126         return this;
127     }
128
129 }