Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / RawPacket.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 /**
11  * @file   RawPacket.java
12  *
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
16  *
17  */
18 package org.opendaylight.controller.sal.packet;
19
20 import java.util.Map;
21 import java.util.HashMap;
22
23 import org.opendaylight.controller.sal.core.ConstructionException;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.core.TimeStamp;
26
27 /**
28  *
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
32  *
33  */
34 public class RawPacket {
35     private byte[] packetData;
36     private LinkEncap encap;
37     private TimeStamp incomingTime;
38     private TimeStamp copyTime;
39     private Map props;
40     private NodeConnector incomingNodeConnector;
41     private NodeConnector outgoingNodeConnector;
42
43     /**
44      * If the packet is being sent this parameter tells where the
45      * packet is sent toward
46      *
47      *
48      * @return the NodeConnector toward where the packet is being sent
49      */
50     public NodeConnector getOutgoingNodeConnector() {
51         return outgoingNodeConnector;
52     }
53
54     /**
55      * Setter method for OutGoing NodeConnector
56      *
57      * @param outgoingNodeConnector NodeConnector toward where the
58      * packet is travelling
59      */
60     public void setOutgoingNodeConnector(NodeConnector outgoingNodeConnector) {
61         this.outgoingNodeConnector = outgoingNodeConnector;
62     }
63
64     /**
65      * Return the incoming NodeConnector if the packet was received
66      *
67      * @return NodeConnector where the packet was received from
68      */
69     public NodeConnector getIncomingNodeConnector() {
70         return incomingNodeConnector;
71     }
72
73     /**
74      * Setter for Incoming NodeConnector
75      *
76      * @param incomingNodeConnector NodeConnector to be used and incoming one
77      */
78     public void setIncomingNodeConnector(NodeConnector incomingNodeConnector) {
79         this.incomingNodeConnector = incomingNodeConnector;
80     }
81
82     /**
83      * Retrieve a given property attached to the packet, if exits of course
84      *
85      * @param key Key to retrieve the wanted property attached to the packet
86      *
87      * @return The property attached to the packet
88      */
89     public Object getProps(Object key) {
90         if (this.props != null) {
91             return this.props.get(key);
92         }
93         return null;
94     }
95
96     /**
97      * Generic data associated to the data packet
98      *
99      * @param key key for the association
100      * @param value value associated to the key
101      */
102     public void setProps(Object key, Object value) {
103         if (this.props == null) {
104             this.props = new HashMap();
105         }
106
107         this.props.put(key, value);
108     }
109
110     /**
111      * Constructor for RawPacket
112      *
113      * @param data content of the packet as bytestream
114      * @param e datalink encapsulation for the packet
115      *
116      */
117     public RawPacket(byte[] data, LinkEncap e) throws ConstructionException {
118         if (data == null) {
119             throw new ConstructionException("Empty data");
120         }
121         if (e == null) {
122             throw new ConstructionException("Encap not known");
123         }
124         this.packetData = new byte[data.length];
125         System.arraycopy(data, 0, this.packetData, 0, data.length);
126         this.encap = e;
127         this.incomingTime = new TimeStamp(System.currentTimeMillis(),
128                 "IncomingTime");
129         this.copyTime = null;
130     }
131
132     /**
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
136      *
137      * @param src packet to copy data from
138      *
139      */
140     public RawPacket(RawPacket src) throws ConstructionException {
141         if (src == null) {
142             throw new ConstructionException("null source packet");
143         }
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);
148         } else {
149             throw new ConstructionException("Empty packetData");
150         }
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");
157     }
158
159     /**
160      * Constructor for RawPacket with Ethernet encapsulation
161      *
162      * @param data content of the packet as bytestream
163      *
164      */
165     public RawPacket(byte[] data) throws ConstructionException {
166         this(data, LinkEncap.ETHERNET);
167     }
168
169     /**
170      * Read the timestamp when the packet has entered the system
171      *
172      * @return The timestamp when the packet has entered the system
173      */
174     public TimeStamp getIncomingTime() {
175         return this.incomingTime;
176     }
177
178     /**
179      * Read the packet encapsulation
180      *
181      * @return The encapsulation for the raw packet, necessary to
182      * start parsing the packet
183      */
184     public LinkEncap getEncap() {
185         return this.encap;
186     }
187
188     /**
189      * Get bytestream of the packet body
190      *
191      * @return The raw bytestream composing the packet
192      */
193     public byte[] getPacketData() {
194         return this.packetData;
195     }
196 }