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