Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / 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 @Deprecated
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;
41
42     /**
43      * If the packet is being sent this parameter tells where the packet is sent
44      * toward
45      *
46      *
47      * @return the NodeConnector toward where the packet is being sent
48      */
49     public NodeConnector getOutgoingNodeConnector() {
50         return outgoingNodeConnector;
51     }
52
53     /**
54      * Setter method for OutGoing NodeConnector
55      *
56      * @param outgoingNodeConnector
57      *            NodeConnector toward where the packet is travelling
58      */
59     public void setOutgoingNodeConnector(NodeConnector outgoingNodeConnector) {
60         this.outgoingNodeConnector = outgoingNodeConnector;
61     }
62
63     /**
64      * Return the incoming NodeConnector if the packet was received
65      *
66      * @return NodeConnector where the packet was received from
67      */
68     public NodeConnector getIncomingNodeConnector() {
69         return incomingNodeConnector;
70     }
71
72     /**
73      * Setter for Incoming NodeConnector
74      *
75      * @param incomingNodeConnector
76      *            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
86      *            Key to retrieve the wanted property attached to the packet
87      *
88      * @return The property attached to the packet
89      */
90     public Object getProps(Object key) {
91         if (this.props != null) {
92             return this.props.get(key);
93         }
94         return null;
95     }
96
97     /**
98      * Generic data associated to the data packet
99      *
100      * @param key
101      *            key for the association
102      * @param value
103      *            value associated to the key
104      */
105     public void setProps(Object key, Object value) {
106         if (this.props == null) {
107             this.props = new HashMap<Object, Object>();
108         }
109
110         this.props.put(key, value);
111     }
112
113     /**
114      * Constructor for RawPacket
115      *
116      * @param data
117      *            content of the packet as bytestream
118      * @param e
119      *            datalink encapsulation for the packet
120      *
121      */
122     public RawPacket(byte[] data, LinkEncap e) throws ConstructionException {
123         if (data == null) {
124             throw new ConstructionException("Empty data");
125         }
126         if (e == null) {
127             throw new ConstructionException("Encap not known");
128         }
129         this.packetData = new byte[data.length];
130         System.arraycopy(data, 0, this.packetData, 0, data.length);
131         this.encap = e;
132         this.incomingTime = new TimeStamp(System.currentTimeMillis(),
133                 "IncomingTime");
134         this.copyTime = null;
135     }
136
137     /**
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
140      * content is touched
141      *
142      * @param src
143      *            packet to copy data from
144      *
145      */
146     public RawPacket(RawPacket src) throws ConstructionException {
147         if (src == null) {
148             throw new ConstructionException("null source packet");
149         }
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);
154         } else {
155             throw new ConstructionException("Empty packetData");
156         }
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>(
162                 src.props));
163         this.copyTime = new TimeStamp(System.currentTimeMillis(), "CopyTime");
164     }
165
166     /**
167      * Constructor for RawPacket with Ethernet encapsulation
168      *
169      * @param data
170      *            content of the packet as bytestream
171      *
172      */
173     public RawPacket(byte[] data) throws ConstructionException {
174         this(data, LinkEncap.ETHERNET);
175     }
176
177     /**
178      * Read the time stamp when the packet has entered the system
179      *
180      * @return The time stamp when the packet has entered the system
181      */
182     public TimeStamp getIncomingTime() {
183         return this.incomingTime;
184     }
185
186     /**
187      * Read the packet encapsulation
188      *
189      * @return The encapsulation for the raw packet, necessary to start parsing
190      *         the packet
191      */
192     public LinkEncap getEncap() {
193         return this.encap;
194     }
195
196     /**
197      * Get bytestream of the packet body
198      *
199      * @return The raw bytestream composing the packet
200      */
201     public byte[] getPacketData() {
202         return this.packetData;
203     }
204
205     /**
206      * Returns the time at which the current instance of RawPacket was created
207      * as a copy of the original one.
208      *
209      * @return The time stamp at which this RawPacket instance was created. null
210      *         if this is the original instance.
211      */
212     public TimeStamp getCopyTime() {
213         return this.copyTime;
214     }
215 }