mdsalutil-api clean up Checkstyle violations (not enforced yet)
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / packet / Ethernet.java
1 /*
2  * Copyright (c) 2013, 2015 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.genius.mdsalutil.packet;
10
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14 import org.apache.commons.lang3.tuple.ImmutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.opendaylight.controller.liblldp.BitBufferHelper;
17 import org.opendaylight.controller.liblldp.EtherTypes;
18 import org.opendaylight.controller.liblldp.LLDP;
19 import org.opendaylight.controller.liblldp.NetUtils;
20 import org.opendaylight.controller.liblldp.Packet;
21
22 /**
23  * Class that represents the Ethernet frame objects
24  * taken from opendaylight(helium) adsal bundle.
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     @SuppressWarnings("checkstyle:ConstantName") // public constant is used in other projects; too late to rename easily
34     public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
35
36     static {
37         etherTypeClassMap = new HashMap<>();
38         etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
39         etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
40         etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
41         // TODO: Add support for more classes here
42         etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
43         // etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
44         // etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
45         // etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
46     }
47
48     private static Map<String, Pair<Integer, Integer>> fieldCoordinates
49         = new LinkedHashMap<String, Pair<Integer, Integer>>() { {
50                 put(DMAC, new ImmutablePair<>(0, 48));
51                 put(SMAC, new ImmutablePair<>(48, 48));
52                 put(ETHT, new ImmutablePair<>(96, 16));
53             }
54         };
55     private final Map<String, byte[]> fieldValues;
56
57     /**
58      * Default constructor that creates and sets the HashMap.
59      */
60     public Ethernet() {
61         super();
62         fieldValues = new HashMap<>();
63         hdrFieldCoordMap = fieldCoordinates;
64         hdrFieldsMap = fieldValues;
65     }
66
67     /**
68      * Constructor that sets the access level for the packet and
69      * creates and sets the HashMap.
70      * @param writeAccess boolean
71      */
72     public Ethernet(boolean writeAccess) {
73         super(writeAccess);
74         fieldValues = new HashMap<>();
75         hdrFieldCoordMap = fieldCoordinates;
76         hdrFieldsMap = fieldValues;
77     }
78
79     @Override
80     public void setHeaderField(String headerField, byte[] readValue) {
81         if (headerField.equals(ETHT)) {
82             payloadClass = etherTypeClassMap.get(BitBufferHelper
83                     .getShort(readValue));
84         }
85         hdrFieldsMap.put(headerField, readValue);
86     }
87
88     public byte[] getDestinationMACAddress() {
89         return fieldValues.get(DMAC);
90     }
91
92     public byte[] getSourceMACAddress() {
93         return fieldValues.get(SMAC);
94     }
95
96     public short getEtherType() {
97         return BitBufferHelper.getShort(fieldValues.get(ETHT));
98     }
99
100     public boolean isBroadcast() {
101         return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
102     }
103
104     public boolean isMulticast() {
105         return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
106     }
107
108     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
109         fieldValues.put(DMAC, destinationMACAddress);
110         return this;
111     }
112
113     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
114         fieldValues.put(SMAC, sourceMACAddress);
115         return this;
116     }
117
118     public Ethernet setEtherType(short etherType) {
119         byte[] ethType = BitBufferHelper.toByteArray(etherType);
120         fieldValues.put(ETHT, ethType);
121         return this;
122     }
123
124 }