Mechanical code cleanup (commons)
[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         super();
48         fieldValues = new HashMap<>();
49         hdrFieldCoordMap = fieldCoordinates;
50         hdrFieldsMap = fieldValues;
51     }
52
53     /**
54      * Constructor that sets the access level for the packet and
55      * creates and sets the HashMap
56      */
57     public Ethernet(boolean writeAccess) {
58         super(writeAccess);
59         fieldValues = new HashMap<>();
60         hdrFieldCoordMap = fieldCoordinates;
61         hdrFieldsMap = fieldValues;
62     }
63
64     @Override
65     public void setHeaderField(String headerField, byte[] readValue) {
66         if (headerField.equals(ETHT)) {
67             payloadClass = etherTypeClassMap.get(BitBufferHelper
68                     .getShort(readValue));
69         }
70         hdrFieldsMap.put(headerField, readValue);
71     }
72
73     /**
74      * Gets the destination MAC address stored
75      * @return byte[] - the destinationMACAddress
76      */
77     public byte[] getDestinationMACAddress() {
78         return fieldValues.get(DMAC);
79     }
80
81     /**
82      * Gets the source MAC address stored
83      * @return byte[] - the sourceMACAddress
84      */
85     public byte[] getSourceMACAddress() {
86         return fieldValues.get(SMAC);
87     }
88
89     /**
90      * Gets the etherType stored
91      * @return short - the etherType
92      */
93     public short getEtherType() {
94         return BitBufferHelper.getShort(fieldValues.get(ETHT));
95     }
96
97     public boolean isBroadcast(){
98         return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
99     }
100
101     public boolean isMulticast(){
102         return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
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 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.