2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 * @file RawPacket.java
12 * @brief Describe a raw Data Packet, this is how a packet is
13 * received from the network and how it will be transmitted. It
14 * essentially wraps the raw bytestream
17 package org.opendaylight.controller.sal.packet;
20 import java.util.HashMap;
22 import org.opendaylight.controller.sal.core.ConstructionException;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.core.TimeStamp;
28 * Describe a raw Data Packet, this is how a packet is received from the network
29 * and how it will be transmitted. It essentially wraps the raw bytestream
32 public class RawPacket {
33 private byte[] packetData;
34 private final LinkEncap encap;
35 private final TimeStamp incomingTime;
36 private final TimeStamp copyTime;
37 private Map<Object, Object> props;
38 private NodeConnector incomingNodeConnector;
39 private NodeConnector outgoingNodeConnector;
42 * If the packet is being sent this parameter tells where the packet is sent
46 * @return the NodeConnector toward where the packet is being sent
48 public NodeConnector getOutgoingNodeConnector() {
49 return outgoingNodeConnector;
53 * Setter method for OutGoing NodeConnector
55 * @param outgoingNodeConnector
56 * NodeConnector toward where the packet is travelling
58 public void setOutgoingNodeConnector(NodeConnector outgoingNodeConnector) {
59 this.outgoingNodeConnector = outgoingNodeConnector;
63 * Return the incoming NodeConnector if the packet was received
65 * @return NodeConnector where the packet was received from
67 public NodeConnector getIncomingNodeConnector() {
68 return incomingNodeConnector;
72 * Setter for Incoming NodeConnector
74 * @param incomingNodeConnector
75 * NodeConnector to be used and incoming one
77 public void setIncomingNodeConnector(NodeConnector incomingNodeConnector) {
78 this.incomingNodeConnector = incomingNodeConnector;
82 * Retrieve a given property attached to the packet, if exits of course
85 * Key to retrieve the wanted property attached to the packet
87 * @return The property attached to the packet
89 public Object getProps(Object key) {
90 if (this.props != null) {
91 return this.props.get(key);
97 * Generic data associated to the data packet
100 * key for the association
102 * value associated to the key
104 public void setProps(Object key, Object value) {
105 if (this.props == null) {
106 this.props = new HashMap<Object, Object>();
109 this.props.put(key, value);
113 * Constructor for RawPacket
116 * content of the packet as bytestream
118 * datalink encapsulation for the packet
121 public RawPacket(byte[] data, LinkEncap e) throws ConstructionException {
123 throw new ConstructionException("Empty data");
126 throw new ConstructionException("Encap not known");
128 this.packetData = new byte[data.length];
129 System.arraycopy(data, 0, this.packetData, 0, data.length);
131 this.incomingTime = new TimeStamp(System.currentTimeMillis(),
133 this.copyTime = null;
137 * Copy Constructor for RawPacket, it performs a copy of the packet so each
138 * packet can be modified independently without worrying that source packet
142 * packet to copy data from
145 public RawPacket(RawPacket src) throws ConstructionException {
147 throw new ConstructionException("null source packet");
149 if (src.getPacketData() != null) {
150 this.packetData = new byte[src.getPacketData().length];
151 System.arraycopy(src.getPacketData(), 0, this.packetData, 0,
152 src.getPacketData().length);
154 throw new ConstructionException("Empty packetData");
156 this.encap = src.getEncap();
157 this.incomingTime = src.getIncomingTime();
158 this.incomingNodeConnector = src.getIncomingNodeConnector();
159 this.outgoingNodeConnector = src.getOutgoingNodeConnector();
160 this.props = (src.props == null ? null : new HashMap<Object, Object>(
162 this.copyTime = new TimeStamp(System.currentTimeMillis(), "CopyTime");
166 * Constructor for RawPacket with Ethernet encapsulation
169 * content of the packet as bytestream
172 public RawPacket(byte[] data) throws ConstructionException {
173 this(data, LinkEncap.ETHERNET);
177 * Read the time stamp when the packet has entered the system
179 * @return The time stamp when the packet has entered the system
181 public TimeStamp getIncomingTime() {
182 return this.incomingTime;
186 * Read the packet encapsulation
188 * @return The encapsulation for the raw packet, necessary to start parsing
191 public LinkEncap getEncap() {
196 * Get bytestream of the packet body
198 * @return The raw bytestream composing the packet
200 public byte[] getPacketData() {
201 return this.packetData;
205 * Returns the time at which the current instance of RawPacket was created
206 * as a copy of the original one.
208 * @return The time stamp at which this RawPacket instance was created. null
209 * if this is the original instance.
211 public TimeStamp getCopyTime() {
212 return this.copyTime;