d0068564a93b29a326395b1e6cae9eeacf968a53
[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 import org.opendaylight.controller.sal.utils.NetUtils;
20
21 /**
22  * Class that represents the Ethernet frame objects
23  */
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";
28
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;
32     static {
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);
41     }
42     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
43         private static final long serialVersionUID = 1L;
44         {
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));
48         }
49     };
50     private final Map<String, byte[]> fieldValues;
51
52     /**
53      * Default constructor that creates and sets the HashMap
54      */
55     public Ethernet() {
56         super();
57         fieldValues = new HashMap<String, byte[]>();
58         hdrFieldCoordMap = fieldCoordinates;
59         hdrFieldsMap = fieldValues;
60     }
61
62     /**
63      * Constructor that sets the access level for the packet and
64      * creates and sets the HashMap
65      */
66     public Ethernet(boolean writeAccess) {
67         super(writeAccess);
68         fieldValues = new HashMap<String, byte[]>();
69         hdrFieldCoordMap = fieldCoordinates;
70         hdrFieldsMap = fieldValues;
71     }
72
73     @Override
74     public void setHeaderField(String headerField, byte[] readValue) {
75         if (headerField.equals(ETHT)) {
76             payloadClass = etherTypeClassMap.get(BitBufferHelper
77                     .getShort(readValue));
78         }
79         hdrFieldsMap.put(headerField, readValue);
80     }
81
82     /**
83      * Gets the destination MAC address stored
84      * @return byte[] - the destinationMACAddress
85      */
86     public byte[] getDestinationMACAddress() {
87         return fieldValues.get(DMAC);
88     }
89
90     /**
91      * Gets the source MAC address stored
92      * @return byte[] - the sourceMACAddress
93      */
94     public byte[] getSourceMACAddress() {
95         return fieldValues.get(SMAC);
96     }
97
98     /**
99      * Gets the etherType stored
100      * @return short - the etherType
101      */
102     public short getEtherType() {
103         return BitBufferHelper.getShort(fieldValues.get(ETHT));
104     }
105
106     public boolean isBroadcast(){
107         return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
108     }
109
110     public boolean isMulticast(){
111         return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
112     }
113
114     /**
115      * Sets the destination MAC address for the current Ethernet object instance
116      * @param byte[] - the destinationMACAddress to set
117      */
118     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
119         fieldValues.put(DMAC, destinationMACAddress);
120         return this;
121     }
122
123     /**
124      * Sets the source MAC address for the current Ethernet object instance
125      * @param byte[] - the sourceMACAddress to set
126      */
127     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
128         fieldValues.put(SMAC, sourceMACAddress);
129         return this;
130     }
131
132     /**
133      * Sets the etherType for the current Ethernet object instance
134      * @param short - the etherType to set
135      */
136     public Ethernet setEtherType(short etherType) {
137         byte[] ethType = BitBufferHelper.toByteArray(etherType);
138         fieldValues.put(ETHT, ethType);
139         return this;
140     }
141
142 }