added arputil module
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / 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.vpnservice.mdsalutil.packet;
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 import org.opendaylight.controller.liblldp.BitBufferHelper;
18 import org.opendaylight.controller.liblldp.EtherTypes;
19 import org.opendaylight.controller.liblldp.LLDP;
20 import org.opendaylight.controller.liblldp.NetUtils;
21 import org.opendaylight.controller.liblldp.Packet;
22
23 /**
24  * Class that represents the Ethernet frame objects
25  * taken from opendaylight(helium) adsal bundle
26  */
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         // TODO: Add support for more classes here
39         // etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
40         // etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
41         // etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
42         // etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
43     }
44     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
45         private static final long serialVersionUID = 1L;
46         {
47             put(DMAC, new ImmutablePair<Integer, Integer>(0, 48));
48             put(SMAC, new ImmutablePair<Integer, Integer>(48, 48));
49             put(ETHT, new ImmutablePair<Integer, Integer>(96, 16));
50         }
51     };
52     private final Map<String, byte[]> fieldValues;
53
54     /**
55      * Default constructor that creates and sets the HashMap
56      */
57     public Ethernet() {
58         super();
59         fieldValues = new HashMap<String, byte[]>();
60         hdrFieldCoordMap = fieldCoordinates;
61         hdrFieldsMap = fieldValues;
62     }
63
64     /**
65      * Constructor that sets the access level for the packet and
66      * creates and sets the HashMap
67      */
68     public Ethernet(boolean writeAccess) {
69         super(writeAccess);
70         fieldValues = new HashMap<String, byte[]>();
71         hdrFieldCoordMap = fieldCoordinates;
72         hdrFieldsMap = fieldValues;
73     }
74
75     @Override
76     public void setHeaderField(String headerField, byte[] readValue) {
77         if (headerField.equals(ETHT)) {
78             payloadClass = etherTypeClassMap.get(BitBufferHelper
79                     .getShort(readValue));
80         }
81         hdrFieldsMap.put(headerField, readValue);
82     }
83
84     public byte[] getDestinationMACAddress() {
85         return fieldValues.get(DMAC);
86     }
87
88     public byte[] getSourceMACAddress() {
89         return fieldValues.get(SMAC);
90     }
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     public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
105         fieldValues.put(DMAC, destinationMACAddress);
106         return this;
107     }
108
109     public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
110         fieldValues.put(SMAC, sourceMACAddress);
111         return this;
112     }
113
114     public Ethernet setEtherType(short etherType) {
115         byte[] ethType = BitBufferHelper.toByteArray(etherType);
116         fieldValues.put(ETHT, ethType);
117         return this;
118     }
119
120 }