BUG 2302 : odl-clustering-test-app should not be part of the odl-restconf-all feature set
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / RawPacket.java
1 /*
2  * Copyright (c) 2013 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 /**
10  * @file   RawPacket.java
11  *
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
15  *
16  */
17 package org.opendaylight.controller.sal.packet;
18
19 import java.util.Map;
20 import java.util.HashMap;
21
22 import org.opendaylight.controller.sal.core.ConstructionException;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.core.TimeStamp;
25
26 /**
27  *
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
30  *
31  */
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;
40
41     /**
42      * If the packet is being sent this parameter tells where the packet is sent
43      * toward
44      *
45      *
46      * @return the NodeConnector toward where the packet is being sent
47      */
48     public NodeConnector getOutgoingNodeConnector() {
49         return outgoingNodeConnector;
50     }
51
52     /**
53      * Setter method for OutGoing NodeConnector
54      *
55      * @param outgoingNodeConnector
56      *            NodeConnector toward where the packet is travelling
57      */
58     public void setOutgoingNodeConnector(NodeConnector outgoingNodeConnector) {
59         this.outgoingNodeConnector = outgoingNodeConnector;
60     }
61
62     /**
63      * Return the incoming NodeConnector if the packet was received
64      *
65      * @return NodeConnector where the packet was received from
66      */
67     public NodeConnector getIncomingNodeConnector() {
68         return incomingNodeConnector;
69     }
70
71     /**
72      * Setter for Incoming NodeConnector
73      *
74      * @param incomingNodeConnector
75      *            NodeConnector to be used and incoming one
76      */
77     public void setIncomingNodeConnector(NodeConnector incomingNodeConnector) {
78         this.incomingNodeConnector = incomingNodeConnector;
79     }
80
81     /**
82      * Retrieve a given property attached to the packet, if exits of course
83      *
84      * @param key
85      *            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
100      *            key for the association
101      * @param value
102      *            value associated to the key
103      */
104     public void setProps(Object key, Object value) {
105         if (this.props == null) {
106             this.props = new HashMap<Object, Object>();
107         }
108
109         this.props.put(key, value);
110     }
111
112     /**
113      * Constructor for RawPacket
114      *
115      * @param data
116      *            content of the packet as bytestream
117      * @param e
118      *            datalink encapsulation for the packet
119      *
120      */
121     public RawPacket(byte[] data, LinkEncap e) throws ConstructionException {
122         if (data == null) {
123             throw new ConstructionException("Empty data");
124         }
125         if (e == null) {
126             throw new ConstructionException("Encap not known");
127         }
128         this.packetData = new byte[data.length];
129         System.arraycopy(data, 0, this.packetData, 0, data.length);
130         this.encap = e;
131         this.incomingTime = new TimeStamp(System.currentTimeMillis(),
132                 "IncomingTime");
133         this.copyTime = null;
134     }
135
136     /**
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
139      * content is touched
140      *
141      * @param src
142      *            packet to copy data from
143      *
144      */
145     public RawPacket(RawPacket src) throws ConstructionException {
146         if (src == null) {
147             throw new ConstructionException("null source packet");
148         }
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);
153         } else {
154             throw new ConstructionException("Empty packetData");
155         }
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>(
161                 src.props));
162         this.copyTime = new TimeStamp(System.currentTimeMillis(), "CopyTime");
163     }
164
165     /**
166      * Constructor for RawPacket with Ethernet encapsulation
167      *
168      * @param data
169      *            content of the packet as bytestream
170      *
171      */
172     public RawPacket(byte[] data) throws ConstructionException {
173         this(data, LinkEncap.ETHERNET);
174     }
175
176     /**
177      * Read the time stamp when the packet has entered the system
178      *
179      * @return The time stamp when the packet has entered the system
180      */
181     public TimeStamp getIncomingTime() {
182         return this.incomingTime;
183     }
184
185     /**
186      * Read the packet encapsulation
187      *
188      * @return The encapsulation for the raw packet, necessary to start parsing
189      *         the packet
190      */
191     public LinkEncap getEncap() {
192         return this.encap;
193     }
194
195     /**
196      * Get bytestream of the packet body
197      *
198      * @return The raw bytestream composing the packet
199      */
200     public byte[] getPacketData() {
201         return this.packetData;
202     }
203
204     /**
205      * Returns the time at which the current instance of RawPacket was created
206      * as a copy of the original one.
207      *
208      * @return The time stamp at which this RawPacket instance was created. null
209      *         if this is the original instance.
210      */
211     public TimeStamp getCopyTime() {
212         return this.copyTime;
213     }
214 }