3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
11 * @file RawPacket.java
13 * @brief Describe a raw Data Packet, this is how a packet is
14 * received from the network and how it will be transmitted. It
15 * essentially wraps the raw bytestream
18 package org.opendaylight.controller.sal.packet;
21 import java.util.HashMap;
23 import org.opendaylight.controller.sal.core.ConstructionException;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.core.TimeStamp;
29 * Describe a raw Data Packet, this is how a packet is
30 * received from the network and how it will be transmitted. It
31 * essentially wraps the raw bytestream
34 public class RawPacket {
35 private byte[] packetData;
36 private LinkEncap encap;
37 private TimeStamp incomingTime;
38 private TimeStamp copyTime;
40 private NodeConnector incomingNodeConnector;
41 private NodeConnector outgoingNodeConnector;
44 * If the packet is being sent this parameter tells where the
45 * packet is sent toward
48 * @return the NodeConnector toward where the packet is being sent
50 public NodeConnector getOutgoingNodeConnector() {
51 return outgoingNodeConnector;
55 * Setter method for OutGoing NodeConnector
57 * @param outgoingNodeConnector NodeConnector toward where the
58 * packet is travelling
60 public void setOutgoingNodeConnector(NodeConnector outgoingNodeConnector) {
61 this.outgoingNodeConnector = outgoingNodeConnector;
65 * Return the incoming NodeConnector if the packet was received
67 * @return NodeConnector where the packet was received from
69 public NodeConnector getIncomingNodeConnector() {
70 return incomingNodeConnector;
74 * Setter for Incoming NodeConnector
76 * @param incomingNodeConnector NodeConnector to be used and incoming one
78 public void setIncomingNodeConnector(NodeConnector incomingNodeConnector) {
79 this.incomingNodeConnector = incomingNodeConnector;
83 * Retrieve a given property attached to the packet, if exits of course
85 * @param key 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
99 * @param key key for the association
100 * @param value value associated to the key
102 public void setProps(Object key, Object value) {
103 if (this.props == null) {
104 this.props = new HashMap();
107 this.props.put(key, value);
111 * Constructor for RawPacket
113 * @param data content of the packet as bytestream
114 * @param e datalink encapsulation for the packet
117 public RawPacket(byte[] data, LinkEncap e) throws ConstructionException {
119 throw new ConstructionException("Empty data");
122 throw new ConstructionException("Encap not known");
124 this.packetData = new byte[data.length];
125 System.arraycopy(data, 0, this.packetData, 0, data.length);
127 this.incomingTime = new TimeStamp(System.currentTimeMillis(),
129 this.copyTime = null;
133 * Copy Constructor for RawPacket, it perform a copy of the packet
134 * so each packet can be modified indipendently without worrying
135 * that source packet content is touched
137 * @param src packet to copy data from
140 public RawPacket(RawPacket src) throws ConstructionException {
142 throw new ConstructionException("null source packet");
144 if (src.getPacketData() != null) {
145 this.packetData = new byte[src.getPacketData().length];
146 System.arraycopy(src.getPacketData(), 0, this.packetData, 0, src
147 .getPacketData().length);
149 throw new ConstructionException("Empty packetData");
151 this.encap = src.getEncap();
152 this.incomingTime = src.getIncomingTime();
153 this.incomingNodeConnector = src.getIncomingNodeConnector();
154 this.outgoingNodeConnector = src.getOutgoingNodeConnector();
155 this.props = (src.props == null ? null : new HashMap(src.props));
156 this.copyTime = new TimeStamp(System.currentTimeMillis(), "CopyTime");
160 * Constructor for RawPacket with Ethernet encapsulation
162 * @param data content of the packet as bytestream
165 public RawPacket(byte[] data) throws ConstructionException {
166 this(data, LinkEncap.ETHERNET);
170 * Read the timestamp when the packet has entered the system
172 * @return The timestamp when the packet has entered the system
174 public TimeStamp getIncomingTime() {
175 return this.incomingTime;
179 * Read the packet encapsulation
181 * @return The encapsulation for the raw packet, necessary to
182 * start parsing the packet
184 public LinkEncap getEncap() {
189 * Get bytestream of the packet body
191 * @return The raw bytestream composing the packet
193 public byte[] getPacketData() {
194 return this.packetData;