Merge "Fixed remove rpc api in rpc registry"
[controller.git] / opendaylight / commons / liblldp / src / main / java / org / opendaylight / controller / liblldp / 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.liblldp;
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
19 /**
20  * Class that represents the Ethernet frame objects
21  */
22 public class Ethernet extends Packet {
23     private static final String DMAC = "DestinationMACAddress";
24     private static final String SMAC = "SourceMACAddress";
25     private static final String ETHT = "EtherType";
26
27     // TODO: This has to be outside and it should be possible for osgi
28     // to add new coming packet classes
29     public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
30     static {
31         etherTypeClassMap = new HashMap<Short, Class<? extends Packet>>();
32         etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
33     }
34     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
35         private static final long serialVersionUID = 1L;
36         {
37             put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
38             put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
39             put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
40         }
41     };
42     private final Map<String, byte[]> fieldValues;
43
44     /**
45      * Default constructor that creates and sets the HashMap
46      */
47     public Ethernet() {
48         super();
49         fieldValues = new HashMap<String, byte[]>();
50         hdrFieldCoordMap = fieldCoordinates;
51         hdrFieldsMap = fieldValues;
52     }
53
54     /**
55      * Constructor that sets the access level for the packet and
56      * creates and sets the HashMap
57      */
58     public Ethernet(boolean writeAccess) {
59         super(writeAccess);
60         fieldValues = new HashMap<String, byte[]>();
61         hdrFieldCoordMap = fieldCoordinates;
62         hdrFieldsMap = fieldValues;
63     }
64
65     @Override
66     public void setHeaderField(String headerField, byte[] readValue) {
67         if (headerField.equals(ETHT)) {
68             payloadClass = etherTypeClassMap.get(BitBufferHelper
69                     .getShort(readValue));
70         }
71         hdrFieldsMap.put(headerField, readValue);
72     }
73
74     /**
75      * Gets the destination MAC address stored
76      * @return byte[] - the destinationMACAddress
77      */
78     public byte[] getDestinationMACAddress() {
79         return fieldValues.get(DMAC);
80     }
81
82     /**
83      * Gets the source MAC address stored
84      * @return byte[] - the sourceMACAddress
85      */
86     public byte[] getSourceMACAddress() {
87         return fieldValues.get(SMAC);
88     }
89
90     /**
91      * Gets the etherType stored
92      * @return short - the etherType
93      */
94     public short getEtherType() {
95         return BitBufferHelper.getShort(fieldValues.get(ETHT));
96     }
97
98     public boolean isBroadcast(){
99         return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
100     }
101
102     public boolean isMulticast(){
103         return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
104     }
105
106     /**
107      * Sets the destination MAC address for the current Ethernet object instance
108      * @param byte[] - the destinationMACAddress to set
109      */
110     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
111         fieldValues.put(DMAC, destinationMACAddress);
112         return this;
113     }
114
115     /**
116      * Sets the source MAC address for the current Ethernet object instance
117      * @param byte[] - the sourceMACAddress to set
118      */
119     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
120         fieldValues.put(SMAC, sourceMACAddress);
121         return this;
122     }
123
124     /**
125      * Sets the etherType for the current Ethernet object instance
126      * @param short - the etherType to set
127      */
128     public Ethernet setEtherType(short etherType) {
129         byte[] ethType = BitBufferHelper.toByteArray(etherType);
130         fieldValues.put(ETHT, ethType);
131         return this;
132     }
133
134 }