Basic fixes to make build work :)
[packetcable.git] / src / main / java / org / umu / cops / stack / COPSReportMsg.java
1 /*\r
2  * Copyright (c) 2003 University of Murcia.  All rights reserved.\r
3  * --------------------------------------------------------------\r
4  * For more information, please see <http://www.umu.euro6ix.org/>.\r
5  */\r
6 \r
7 package org.umu.cops.stack;\r
8 \r
9 import java.io.IOException;\r
10 import java.io.OutputStream;\r
11 import java.net.Socket;\r
12 import java.util.Enumeration;\r
13 import java.util.Vector;\r
14 \r
15 /**\r
16  * COPS Report Message (RFC 2748 pag. 25)\r
17  *\r
18  *    The RPT message is used by the PEP to communicate to the PDP its\r
19  *   success or failure in carrying out the PDP's decision, or to report\r
20  *   an accounting related change in state. The Report-Type specifies the\r
21  *   kind of report and the optional ClientSI can carry additional\r
22  *   information per Client-Type.\r
23  *\r
24  *   For every DEC message containing a configuration context that is\r
25  *   received by a PEP, the PEP MUST generate a corresponding Report State\r
26  *   message with the Solicited Message flag set describing its success or\r
27  *   failure in applying the configuration decision. In addition,\r
28  *   outsourcing decisions from the PDP MAY result in a corresponding\r
29  *   solicited Report State from the PEP depending on the context and the\r
30  *   type of client. RPT messages solicited by decisions for a given\r
31  *   Client Handle MUST set the Solicited Message flag and MUST be sent in\r
32  *   the same order as their corresponding Decision messages were\r
33  *   received. There MUST never be more than one Report State message\r
34  *   generated with the Solicited Message flag set per Decision.\r
35  *\r
36  *   The Report State may also be used to provide periodic updates of\r
37  *   client specific information for accounting and state monitoring\r
38  *   purposes depending on the type of the client. In such cases the\r
39  *   accounting report type should be specified utilizing the appropriate\r
40  *   client specific information object.\r
41  *\r
42  *              <Report State> ::== <Common Header>\r
43  *                                  <Client Handle>\r
44  *                                  <Report-Type>\r
45  *                                  [<ClientSI>]\r
46  *                                  [<Integrity>]\r
47  *\r
48  * @version COPSReportMsg.java, v 1.00 2003\r
49  *\r
50  */\r
51 public class COPSReportMsg extends COPSMsg {\r
52     /* COPSHeader coming from base class */\r
53     private COPSHandle _clientHandle;\r
54     private COPSReportType _report;\r
55     private Vector _clientSI;\r
56     private COPSIntegrity _integrity;\r
57 \r
58     public COPSReportMsg() {\r
59         _clientHandle = null;\r
60         _report = null;\r
61         _integrity = null;\r
62         _clientSI = new Vector(20);\r
63     }\r
64 \r
65     /**\r
66           Parse data and create COPSReportMsg object\r
67      */\r
68     protected COPSReportMsg (byte[] data) throws COPSException {\r
69         _clientHandle = null;\r
70         _report = null;\r
71         _integrity = null;\r
72         parse(data);\r
73     }\r
74 \r
75     /**\r
76      * Checks the sanity of COPS message and throw an\r
77      * COPSException when data is bad.\r
78      */\r
79     public void checkSanity() throws COPSException {\r
80         if ((_hdr == null) || (_clientHandle == null) || (_report == null))\r
81             throw new COPSException("Bad message format");\r
82     }\r
83 \r
84     /**\r
85      * Add message header\r
86      *\r
87      * @param    hdr                 a  COPSHeader\r
88      *\r
89      * @throws   COPSException\r
90      *\r
91      */\r
92     public void add (COPSHeader hdr) throws COPSException {\r
93         if (hdr == null)\r
94             throw new COPSException ("Null Header");\r
95         if (hdr.getOpCode() != COPSHeader.COPS_OP_RPT)\r
96             throw new COPSException ("Error Header (no COPS_OP_REQ)");\r
97         _hdr = hdr;\r
98         setMsgLength();\r
99     }\r
100 \r
101     /**\r
102      * Add Report object to the message\r
103      *\r
104      * @param    report              a  COPSReportType\r
105      *\r
106      * @throws   COPSException\r
107      *\r
108      */\r
109     public void add (COPSReportType report) throws COPSException {\r
110         if (report == null)\r
111             throw new COPSException ("Null Handle");\r
112 \r
113         //Message integrity object should be the very last one\r
114         //If it is already added\r
115         if (_integrity != null)\r
116             throw new COPSException ("No null Handle");\r
117 \r
118         _report = report;\r
119         setMsgLength();\r
120     }\r
121 \r
122     /**\r
123      * Add client handle to the message\r
124      *\r
125      * @param    handle              a  COPSHandle\r
126      *\r
127      * @throws   COPSException\r
128      *\r
129      */\r
130     public void add (COPSHandle handle) throws COPSException {\r
131         if (handle == null)\r
132             throw new COPSException ("Null Handle");\r
133 \r
134         //Message integrity object should be the very last one\r
135         //If it is already added\r
136         if (_integrity != null)\r
137             throw new COPSException ("No null Handle");\r
138 \r
139         _clientHandle = handle;\r
140         setMsgLength();\r
141     }\r
142 \r
143     /**\r
144      * Add one or more clientSI objects\r
145      *\r
146      * @param    clientSI            a  COPSClientSI\r
147      *\r
148      * @throws   COPSException\r
149      *\r
150      */\r
151     public void add (COPSClientSI clientSI) throws COPSException {\r
152         if (clientSI == null)\r
153             throw new COPSException ("Null ClientSI");\r
154         _clientSI.add(clientSI);\r
155         setMsgLength();\r
156     }\r
157 \r
158     /**\r
159      * Add integrity object\r
160      *\r
161      * @param    integrity           a  COPSIntegrity\r
162      *\r
163      * @throws   COPSException\r
164      *\r
165      */\r
166     public void add (COPSIntegrity integrity) throws COPSException {\r
167         if (integrity == null)\r
168             throw new COPSException ("Null Integrity");\r
169         if (!integrity.isMessageIntegrity())\r
170             throw new COPSException ("Error Integrity");\r
171         _integrity = integrity;\r
172         setMsgLength();\r
173     }\r
174 \r
175     /**\r
176      * Get client Handle\r
177      *\r
178      * @return   a COPSHandle\r
179      *\r
180      */\r
181     public COPSHandle getClientHandle() {\r
182         return _clientHandle;\r
183     }\r
184 \r
185     /**\r
186      * Get report type\r
187      *\r
188      * @return   a COPSReportType\r
189      *\r
190      */\r
191     public COPSReportType getReport() {\r
192         return _report;\r
193     }\r
194 \r
195     /**\r
196      * Get clientSI\r
197      *\r
198      * @return   a Vector\r
199      *\r
200      */\r
201     public Vector getClientSI() {\r
202         return _clientSI;\r
203     }\r
204 \r
205     /**\r
206      * Returns true if it has Integrity object\r
207      *\r
208      * @return   a boolean\r
209      *\r
210      */\r
211     public boolean hasIntegrity() {\r
212         return (_integrity != null);\r
213     }\r
214 \r
215 \r
216     /**\r
217      * Get Integrity. Should check hasIntegrity() before calling\r
218      *\r
219      * @return   a COPSIntegrity\r
220      *\r
221      */\r
222     public COPSIntegrity getIntegrity() {\r
223         return (_integrity);\r
224     }\r
225 \r
226     /**\r
227      * Writes data to given network socket\r
228      *\r
229      * @param    id                  a  Socket\r
230      *\r
231      * @throws   IOException\r
232      *\r
233      */\r
234     public void writeData(Socket id) throws IOException {\r
235         //checkSanity();\r
236         if (_hdr != null) _hdr.writeData(id);\r
237         if (_clientHandle != null) _clientHandle.writeData(id);\r
238         if (_report != null) _report.writeData(id);\r
239 \r
240         for (Enumeration e = _clientSI.elements() ; e.hasMoreElements() ;) {\r
241             COPSClientSI clientSI = (COPSClientSI) e.nextElement();\r
242             clientSI.writeData(id);\r
243         }\r
244 \r
245         if (_integrity != null) _integrity.writeData(id);\r
246     }\r
247 \r
248     /**\r
249      * Parse data\r
250      *\r
251      * @param    data                a  byte[]\r
252      *\r
253      * @throws   COPSException\r
254      *\r
255      */\r
256     protected void parse(byte[] data) throws COPSException {\r
257         super.parseHeader(data);\r
258 \r
259         while (_dataStart < _dataLength) {\r
260             byte[] buf = new byte[data.length - _dataStart];\r
261             System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);\r
262 \r
263             COPSObjHeader objHdr = new COPSObjHeader (buf);\r
264             switch (objHdr.getCNum()) {\r
265             case COPSObjHeader.COPS_HANDLE: {\r
266                 _clientHandle = new COPSHandle(buf);\r
267                 _dataStart += _clientHandle.getDataLength();\r
268             }\r
269             break;\r
270             case COPSObjHeader.COPS_RPT: {\r
271                 _report = new COPSReportType(buf);\r
272                 _dataStart += _report.getDataLength();\r
273             }\r
274             break;\r
275             case COPSObjHeader.COPS_CSI: {\r
276                 COPSClientSI csi = new COPSClientSI(buf);\r
277                 _dataStart += csi.getDataLength();\r
278                 _clientSI.add(csi);\r
279             }\r
280             break;\r
281 \r
282             case COPSObjHeader.COPS_MSG_INTEGRITY: {\r
283                 _integrity = new COPSIntegrity(buf);\r
284                 _dataStart += _integrity.getDataLength();\r
285             }\r
286             break;\r
287 \r
288             default: {\r
289                 throw new COPSException("Bad Message format, unknown object type");\r
290             }\r
291             }\r
292         }\r
293         checkSanity();\r
294     }\r
295 \r
296     /**\r
297      * Parse data\r
298      *\r
299      * @param    hdr                 a  COPSHeader\r
300      * @param    data                a  byte[]\r
301      *\r
302      * @throws   COPSException\r
303      *\r
304      */\r
305     protected void parse(COPSHeader hdr, byte[] data) throws COPSException {\r
306         if (hdr.getOpCode() != COPSHeader.COPS_OP_RPT)\r
307             throw new COPSException ("Null Header");\r
308         _hdr = hdr;\r
309         parse(data);\r
310         setMsgLength();\r
311     }\r
312 \r
313     /**\r
314      * Set the message length, base on the set of objects it contains\r
315      *\r
316      * @throws   COPSException\r
317      *\r
318      */\r
319     protected void setMsgLength() throws COPSException {\r
320         short len = 0;\r
321         if (_clientHandle != null) len += _clientHandle.getDataLength();\r
322         if (_report != null) len += _report.getDataLength();\r
323 \r
324         for (Enumeration e = _clientSI.elements() ; e.hasMoreElements() ;) {\r
325             COPSClientSI clientSI = (COPSClientSI) e.nextElement();\r
326             len += clientSI.getDataLength();\r
327         }\r
328 \r
329         if (_integrity != null) len += _integrity.getDataLength();\r
330         _hdr.setMsgLength(len);\r
331     }\r
332 \r
333     /**\r
334      * Write an object textual description in the output stream\r
335      *\r
336      * @param    os                  an OutputStream\r
337      *\r
338      * @throws   IOException\r
339      *\r
340      */\r
341     public void dump(OutputStream os) throws IOException {\r
342         _hdr.dump(os);\r
343 \r
344         if (_clientHandle != null)\r
345             _clientHandle.dump(os);\r
346 \r
347         if (_report != null)\r
348             _report.dump(os);\r
349 \r
350         for (Enumeration e = _clientSI.elements() ; e.hasMoreElements() ;) {\r
351             COPSClientSI clientSI = (COPSClientSI) e.nextElement();\r
352             clientSI.dump(os);\r
353         }\r
354 \r
355         if (_integrity != null) {\r
356             _integrity.dump(os);\r
357         }\r
358     }\r
359 }\r
360 \r
361 \r
362 \r