Make sure invokeOperation is set once
[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 @Deprecated
27 public class Ethernet extends Packet {
28     private static final String DMAC = "DestinationMACAddress";
29     private static final String SMAC = "SourceMACAddress";
30     private static final String ETHT = "EtherType";
31
32     // TODO: This has to be outside and it should be possible for osgi
33     // to add new coming packet classes
34     public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
35     static {
36         etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
37         etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
38         etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
39         etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
40         etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
41         etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
42         etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
43         etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
44     }
45     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
46         private static final long serialVersionUID = 1L;
47         {
48             put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
49             put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
50             put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
51         }
52     };
53     private final Map<String, byte[]> fieldValues;
54
55     /**
56      * Default constructor that creates and sets the HashMap
57      */
58     public Ethernet() {
59         super();
60         fieldValues = new HashMap<String, byte[]>();
61         hdrFieldCoordMap = fieldCoordinates;
62         hdrFieldsMap = fieldValues;
63     }
64
65     /**
66      * Constructor that sets the access level for the packet and
67      * creates and sets the HashMap
68      */
69     public Ethernet(boolean writeAccess) {
70         super(writeAccess);
71         fieldValues = new HashMap<String, byte[]>();
72         hdrFieldCoordMap = fieldCoordinates;
73         hdrFieldsMap = fieldValues;
74     }
75
76     @Override
77     public void setHeaderField(String headerField, byte[] readValue) {
78         if (headerField.equals(ETHT)) {
79             payloadClass = etherTypeClassMap.get(BitBufferHelper
80                     .getShort(readValue));
81         }
82         hdrFieldsMap.put(headerField, readValue);
83     }
84
85     /**
86      * Gets the destination MAC address stored
87      * @return byte[] - the destinationMACAddress
88      */
89     public byte[] getDestinationMACAddress() {
90         return fieldValues.get(DMAC);
91     }
92
93     /**
94      * Gets the source MAC address stored
95      * @return byte[] - the sourceMACAddress
96      */
97     public byte[] getSourceMACAddress() {
98         return fieldValues.get(SMAC);
99     }
100
101     /**
102      * Gets the etherType stored
103      * @return short - the etherType
104      */
105     public short getEtherType() {
106         return BitBufferHelper.getShort(fieldValues.get(ETHT));
107     }
108
109     public boolean isBroadcast(){
110         return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
111     }
112
113     public boolean isMulticast(){
114         return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
115     }
116
117     /**
118      * Sets the destination MAC address for the current Ethernet object instance
119      * @param byte[] - the destinationMACAddress to set
120      */
121     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
122         fieldValues.put(DMAC, destinationMACAddress);
123         return this;
124     }
125
126     /**
127      * Sets the source MAC address for the current Ethernet object instance
128      * @param byte[] - the sourceMACAddress to set
129      */
130     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
131         fieldValues.put(SMAC, sourceMACAddress);
132         return this;
133     }
134
135     /**
136      * Sets the etherType for the current Ethernet object instance
137      * @param short - the etherType to set
138      */
139     public Ethernet setEtherType(short etherType) {
140         byte[] ethType = BitBufferHelper.toByteArray(etherType);
141         fieldValues.put(ETHT, ethType);
142         return this;
143     }
144
145     @Override
146     public void populateMatch(Match match) {
147         match.setField(MatchType.DL_SRC, this.getSourceMACAddress());
148         match.setField(MatchType.DL_DST, this.getDestinationMACAddress());
149         match.setField(MatchType.DL_TYPE, this.getEtherType());
150     }
151 }