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
33 public class RawPacket {
34 private byte[] packetData;
35 private final LinkEncap encap;
36 private final TimeStamp incomingTime;
37 private final TimeStamp copyTime;
38 private Map<Object, Object> props;
39 private NodeConnector incomingNodeConnector;
40 private NodeConnector outgoingNodeConnector;
43 * If the packet is being sent this parameter tells where the packet is sent
47 * @return the NodeConnector toward where the packet is being sent
49 public NodeConnector getOutgoingNodeConnector() {
50 return outgoingNodeConnector;
54 * Setter method for OutGoing NodeConnector
56 * @param outgoingNodeConnector
57 * NodeConnector toward where the packet is travelling
59 public void setOutgoingNodeConnector(NodeConnector outgoingNodeConnector) {
60 this.outgoingNodeConnector = outgoingNodeConnector;
64 * Return the incoming NodeConnector if the packet was received
66 * @return NodeConnector where the packet was received from
68 public NodeConnector getIncomingNodeConnector() {
69 return incomingNodeConnector;
73 * Setter for Incoming NodeConnector
75 * @param incomingNodeConnector
76 * 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
86 * Key to retrieve the wanted property attached to the packet
88 * @return The property attached to the packet
90 public Object getProps(Object key) {
91 if (this.props != null) {
92 return this.props.get(key);
98 * Generic data associated to the data packet
101 * key for the association
103 * value associated to the key
105 public void setProps(Object key, Object value) {
106 if (this.props == null) {
107 this.props = new HashMap<Object, Object>();
110 this.props.put(key, value);
114 * Constructor for RawPacket
117 * content of the packet as bytestream
119 * datalink encapsulation for the packet
122 public RawPacket(byte[] data, LinkEncap e) throws ConstructionException {
124 throw new ConstructionException("Empty data");
127 throw new ConstructionException("Encap not known");
129 this.packetData = new byte[data.length];
130 System.arraycopy(data, 0, this.packetData, 0, data.length);
132 this.incomingTime = new TimeStamp(System.currentTimeMillis(),
134 this.copyTime = null;
138 * Copy Constructor for RawPacket, it performs a copy of the packet so each
139 * packet can be modified independently without worrying that source packet
143 * packet to copy data from
146 public RawPacket(RawPacket src) throws ConstructionException {
148 throw new ConstructionException("null source packet");
150 if (src.getPacketData() != null) {
151 this.packetData = new byte[src.getPacketData().length];
152 System.arraycopy(src.getPacketData(), 0, this.packetData, 0,
153 src.getPacketData().length);
155 throw new ConstructionException("Empty packetData");
157 this.encap = src.getEncap();
158 this.incomingTime = src.getIncomingTime();
159 this.incomingNodeConnector = src.getIncomingNodeConnector();
160 this.outgoingNodeConnector = src.getOutgoingNodeConnector();
161 this.props = (src.props == null ? null : new HashMap<Object, Object>(
163 this.copyTime = new TimeStamp(System.currentTimeMillis(), "CopyTime");
167 * Constructor for RawPacket with Ethernet encapsulation
170 * content of the packet as bytestream
173 public RawPacket(byte[] data) throws ConstructionException {
174 this(data, LinkEncap.ETHERNET);
178 * Read the time stamp when the packet has entered the system
180 * @return The time stamp when the packet has entered the system
182 public TimeStamp getIncomingTime() {
183 return this.incomingTime;
187 * Read the packet encapsulation
189 * @return The encapsulation for the raw packet, necessary to start parsing
192 public LinkEncap getEncap() {
197 * Get bytestream of the packet body
199 * @return The raw bytestream composing the packet
201 public byte[] getPacketData() {
202 return this.packetData;
206 * Returns the time at which the current instance of RawPacket was created
207 * as a copy of the original one.
209 * @return The time stamp at which this RawPacket instance was created. null
210 * if this is the original instance.
212 public TimeStamp getCopyTime() {
213 return this.copyTime;