7f151c9502d22f936f2dbb25f24cda9fcdb14585
[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         etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
37         etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
38         etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
39         etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
40     }
41     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
42         private static final long serialVersionUID = 1L;
43         {
44             put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
45             put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
46             put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
47         }
48     };
49     private final Map<String, byte[]> fieldValues;
50
51     /**
52      * Default constructor that creates and sets the HashMap
53      */
54     public Ethernet() {
55         super();
56         fieldValues = new HashMap<String, byte[]>();
57         hdrFieldCoordMap = fieldCoordinates;
58         hdrFieldsMap = fieldValues;
59     }
60
61     /**
62      * Constructor that sets the access level for the packet and
63      * creates and sets the HashMap
64      */
65     public Ethernet(boolean writeAccess) {
66         super(writeAccess);
67         fieldValues = new HashMap<String, byte[]>();
68         hdrFieldCoordMap = fieldCoordinates;
69         hdrFieldsMap = fieldValues;
70     }
71
72     @Override
73     public void setHeaderField(String headerField, byte[] readValue) {
74         if (headerField.equals(ETHT)) {
75             payloadClass = etherTypeClassMap.get(BitBufferHelper
76                     .getShort(readValue));
77         }
78         hdrFieldsMap.put(headerField, readValue);
79     }
80
81     /**
82      * Gets the destination MAC address stored
83      * @return byte[] - the destinationMACAddress
84      */
85     public byte[] getDestinationMACAddress() {
86         return fieldValues.get(DMAC);
87     }
88
89     /**
90      * Gets the source MAC address stored
91      * @return byte[] - the sourceMACAddress
92      */
93     public byte[] getSourceMACAddress() {
94         return fieldValues.get(SMAC);
95     }
96
97     /**
98      * Gets the etherType stored
99      * @return short - the etherType
100      */
101     public short getEtherType() {
102         return BitBufferHelper.getShort(fieldValues.get(ETHT));
103     }
104
105     /**
106      * Sets the destination MAC address for the current Ethernet object instance
107      * @param byte[] - the destinationMACAddress to set
108      */
109     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
110         fieldValues.put(DMAC, destinationMACAddress);
111         return this;
112     }
113
114     /**
115      * Sets the source MAC address for the current Ethernet object instance
116      * @param byte[] - the sourceMACAddress to set
117      */
118     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
119         fieldValues.put(SMAC, sourceMACAddress);
120         return this;
121     }
122
123     /**
124      * Sets the etherType for the current Ethernet object instance
125      * @param short - the etherType to set
126      */
127     public Ethernet setEtherType(short etherType) {
128         byte[] ethType = BitBufferHelper.toByteArray(etherType);
129         fieldValues.put(ETHT, ethType);
130         return this;
131     }
132
133 }