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