Merge "Remove "response" from yang."
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / umu / cops / prpdp / COPSPdpMsgSender.java
1 /*\r
2  * Copyright (c) 2004 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.prpdp;\r
8 \r
9 import java.io.IOException;\r
10 import java.net.Socket;\r
11 import java.util.Enumeration;\r
12 import java.util.Hashtable;\r
13 \r
14 import org.umu.cops.stack.COPSContext;\r
15 import org.umu.cops.stack.COPSData;\r
16 import org.umu.cops.stack.COPSDecision;\r
17 import org.umu.cops.stack.COPSDecisionMsg;\r
18 import org.umu.cops.stack.COPSException;\r
19 import org.umu.cops.stack.COPSHandle;\r
20 import org.umu.cops.stack.COPSHeader;\r
21 import org.umu.cops.stack.COPSPrEPD;\r
22 import org.umu.cops.stack.COPSPrID;\r
23 import org.umu.cops.stack.COPSSyncStateMsg;\r
24 \r
25 /**\r
26  * COPS message transceiver class for provisioning connections at the PDP side.\r
27  */\r
28 public class COPSPdpMsgSender {\r
29 \r
30     /**\r
31      * Socket connected to PEP\r
32      */\r
33     protected Socket _sock;\r
34 \r
35     /**\r
36      * COPS client-type that identifies the policy client\r
37      */\r
38     protected short _clientType;\r
39 \r
40     /**\r
41      * COPS client handle used to uniquely identify a particular\r
42      * PEP's request for a client-type\r
43      */\r
44     protected COPSHandle _handle;\r
45 \r
46     /**\r
47      * Creates a COPSPepMsgSender\r
48      *\r
49      * @param clientType        COPS client-type\r
50      * @param clientHandle      Client handle\r
51      * @param sock              Socket to the PEP\r
52      */\r
53     public COPSPdpMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {\r
54         // COPS Handle\r
55         _handle = clientHandle;\r
56         _clientType = clientType;\r
57 \r
58         _sock = sock;\r
59     }\r
60 \r
61     /**\r
62      * Gets the client handle\r
63      * @return   Client's <tt>COPSHandle</tt>\r
64      */\r
65     public COPSHandle getClientHandle() {\r
66         return _handle;\r
67     }\r
68 \r
69     /**\r
70      * Gets the client-type\r
71      * @return   Client-type value\r
72      */\r
73     public short getClientType() {\r
74         return _clientType;\r
75     }\r
76 \r
77     /**\r
78      * Sends a decision message\r
79      * @param removeDecs    Decisions to be removed\r
80      * @param installDecs   Decisions to be installed\r
81      * @throws COPSPdpException\r
82      */\r
83     public void sendDecision(Hashtable removeDecs, Hashtable installDecs)\r
84     throws COPSPdpException {\r
85         /* <Decision Message> ::= <Common Header: Flag SOLICITED>\r
86          *                          <Client Handle>\r
87          *                          *(<Decision>) | <Error>\r
88          *                          [<Integrity>]\r
89          * <Decision> ::= <Context>\r
90          *                  <Decision: Flags>\r
91          *                  [<Named Decision Data: Provisioning>]\r
92          * <Decision: Flags> ::= <Command-Code> NULLFlag\r
93          * <Command-Code> ::= NULLDecision | Install | Remove\r
94          * <Named Decision Data> ::= <<Install Decision> | <Remove Decision>>\r
95          * <Install Decision> ::= *(<PRID> <EPD>)\r
96          * <Remove Decision> ::= *(<PRID> | <PPRID>)\r
97          *\r
98          * Very important, this is actually being treated like this:\r
99          * <Install Decision> ::= <PRID> | <EPD>\r
100          * <Remove Decision> ::= <PRID> | <PPRID>\r
101          *\r
102         */\r
103 \r
104         // Common Header with the same ClientType as the request\r
105         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());\r
106         hdr.setFlag(COPSHeader.COPS_FLAG_SOLICITED);\r
107 \r
108         // Client Handle with the same clientHandle as the request\r
109         COPSHandle handle = new COPSHandle();\r
110         handle.setId(getClientHandle().getId());\r
111 \r
112         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();\r
113         try {\r
114             decisionMsg.add(hdr);\r
115             decisionMsg.add(handle);\r
116 \r
117             // Decisions (no flags supplied)\r
118             //  <Context>\r
119             COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);\r
120 \r
121             // Remove Decisions\r
122             //  <Decision: Flags>\r
123             if (removeDecs.size() > 0) {\r
124                 COPSDecision rdec1 = new COPSDecision();\r
125                 rdec1.setCmdCode(COPSDecision.DEC_REMOVE);\r
126 \r
127                 decisionMsg.addDecision(rdec1, cntxt);\r
128 \r
129                 for (Enumeration e = removeDecs.keys() ; e.hasMoreElements() ;) {\r
130                     String strprid = (String) e.nextElement();\r
131                     String strepd = (String) removeDecs.get(strprid);\r
132 \r
133                     //  <Named Decision Data: Provisioning> (PRID)\r
134                     COPSDecision dec2 = new COPSDecision(COPSDecision.DEC_NAMED);\r
135                     COPSPrID prid = new COPSPrID();\r
136                     prid.setData(new COPSData(strprid));\r
137                     dec2.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));\r
138                     //  <Named Decision Data: Provisioning> (EPD)\r
139                     COPSDecision dec3 = new COPSDecision(COPSDecision.DEC_NAMED);\r
140                     COPSPrEPD epd = new COPSPrEPD();\r
141                     epd.setData(new COPSData(strepd));\r
142                     dec3.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));\r
143 \r
144                     decisionMsg.addDecision(dec2, cntxt);\r
145                     decisionMsg.addDecision(dec3, cntxt);\r
146                 }\r
147             }\r
148 \r
149             // Install Decisions\r
150             //  <Decision: Flags>\r
151             if (installDecs.size() > 0) {\r
152                 COPSDecision idec1 = new COPSDecision();\r
153                 idec1.setCmdCode(COPSDecision.DEC_INSTALL);\r
154 \r
155                 decisionMsg.addDecision(idec1, cntxt);\r
156 \r
157                 for (Enumeration e = installDecs.keys() ; e.hasMoreElements() ;) {\r
158                     String strprid = (String) e.nextElement();\r
159                     String strepd = (String) installDecs.get(strprid);\r
160 \r
161                     //  <Named Decision Data: Provisioning> (PRID)\r
162                     COPSDecision dec2 = new COPSDecision(COPSDecision.DEC_NAMED);\r
163                     COPSPrID prid = new COPSPrID();\r
164                     prid.setData(new COPSData(strprid));\r
165                     dec2.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));\r
166                     //  <Named Decision Data: Provisioning> (EPD)\r
167                     COPSDecision dec3 = new COPSDecision(COPSDecision.DEC_NAMED);\r
168                     COPSPrEPD epd = new COPSPrEPD();\r
169                     epd.setData(new COPSData(strepd));\r
170                     dec3.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));\r
171 \r
172                     decisionMsg.addDecision(dec2, cntxt);\r
173                     decisionMsg.addDecision(dec3, cntxt);\r
174                 }\r
175 \r
176                 /**\r
177                 COPSIntegrity intr = new COPSIntegrity();\r
178                 intr.setKeyId(19);\r
179                 intr.setSeqNum(9);\r
180                 intr.setKeyDigest(new COPSData("KEY DIGEST"));\r
181                 decisionMsg.add(intr);\r
182                 /**/\r
183             }\r
184         } catch (COPSException e) {\r
185             throw new COPSPdpException("Error making Msg");\r
186         }\r
187 \r
188         //** Send the decision\r
189         //**\r
190         try {\r
191             decisionMsg.writeData(_sock);\r
192         } catch (IOException e) {\r
193             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());\r
194         }\r
195     }\r
196 \r
197     /**\r
198      * Sends a decision message which was not requested by the PEP\r
199      * @param removeDecs    Decisions to be removed\r
200      * @param installDecs   Decisions to be installed\r
201      * @throws COPSPdpException\r
202      */\r
203     public void sendUnsolicitedDecision(Hashtable removeDecs, Hashtable installDecs)\r
204     throws COPSPdpException {\r
205         //** Example of an UNSOLICITED decision\r
206         //**\r
207 \r
208         /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>\r
209          *                          <Client Handle>\r
210          *                          *(<Decision>) | <Error>\r
211          *                          [<Integrity>]\r
212          * <Decision> ::= <Context>\r
213          *                  <Decision: Flags>\r
214          *                  [<Named Decision Data: Provisioning>]\r
215          * <Decision: Flags> ::= <Command-Code> NULLFlag\r
216          * <Command-Code> ::= NULLDecision | Install | Remove\r
217          * <Named Decision Data> ::= <<Install Decision> | <Remove Decision>>\r
218          * <Install Decision> ::= *(<PRID> <EPD>)\r
219          * <Remove Decision> ::= *(<PRID> | <PPRID>)\r
220          *\r
221          * Very important, this is actually being treated like this:\r
222          * <Install Decision> ::= <PRID> | <EPD>\r
223          * <Remove Decision> ::= <PRID> | <PPRID>\r
224          *\r
225         */\r
226 \r
227         // Common Header with the same ClientType as the request\r
228         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());\r
229 \r
230         // Client Handle with the same clientHandle as the request\r
231         COPSHandle handle = new COPSHandle();\r
232         handle.setId(getClientHandle().getId());\r
233 \r
234         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();\r
235         try {\r
236             decisionMsg.add(hdr);\r
237             decisionMsg.add(handle);\r
238 \r
239             // Decisions (no flags supplied)\r
240             //  <Context>\r
241             COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);\r
242 \r
243             // Remove Decisions\r
244             //  <Decision: Flags>\r
245             COPSDecision rdec1 = new COPSDecision();\r
246             rdec1.setCmdCode(COPSDecision.DEC_REMOVE);\r
247 \r
248             decisionMsg.addDecision(rdec1, cntxt);\r
249 \r
250             for (Enumeration e = removeDecs.keys() ; e.hasMoreElements() ;) {\r
251                 String strprid = (String) e.nextElement();\r
252                 String strepd = (String) removeDecs.get(strprid);\r
253 \r
254                 //  <Named Decision Data: Provisioning> (PRID)\r
255                 COPSDecision dec2 = new COPSDecision(COPSDecision.DEC_NAMED);\r
256                 COPSPrID prid = new COPSPrID();\r
257                 prid.setData(new COPSData(strprid));\r
258                 dec2.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));\r
259                 //  <Named Decision Data: Provisioning> (EPD)\r
260                 COPSDecision dec3 = new COPSDecision(COPSDecision.DEC_NAMED);\r
261                 COPSPrEPD epd = new COPSPrEPD();\r
262                 epd.setData(new COPSData(strepd));\r
263                 dec3.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));\r
264 \r
265                 decisionMsg.addDecision(dec2, cntxt);\r
266                 decisionMsg.addDecision(dec3, cntxt);\r
267             }\r
268 \r
269             // Install Decisions\r
270             //  <Decision: Flags>\r
271             COPSDecision idec1 = new COPSDecision();\r
272             idec1.setCmdCode(COPSDecision.DEC_INSTALL);\r
273 \r
274             decisionMsg.addDecision(idec1, cntxt);\r
275 \r
276             for (Enumeration e = installDecs.keys() ; e.hasMoreElements() ;) {\r
277                 String strprid = (String) e.nextElement();\r
278                 String strepd = (String) installDecs.get(strprid);\r
279 \r
280                 //  <Named Decision Data: Provisioning> (PRID)\r
281                 COPSDecision dec2 = new COPSDecision(COPSDecision.DEC_NAMED);\r
282                 COPSPrID prid = new COPSPrID();\r
283                 prid.setData(new COPSData(strprid));\r
284                 dec2.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));\r
285                 //  <Named Decision Data: Provisioning> (EPD)\r
286                 COPSDecision dec3 = new COPSDecision(COPSDecision.DEC_NAMED);\r
287                 COPSPrEPD epd = new COPSPrEPD();\r
288                 epd.setData(new COPSData(strepd));\r
289                 dec3.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));\r
290 \r
291                 decisionMsg.addDecision(dec2, cntxt);\r
292                 decisionMsg.addDecision(dec3, cntxt);\r
293             }\r
294 \r
295             /**\r
296             COPSIntegrity intr = new COPSIntegrity();\r
297             intr.setKeyId(19);\r
298             intr.setSeqNum(9);\r
299             intr.setKeyDigest(new COPSData("KEY DIGEST"));\r
300             decisionMsg.add(intr);\r
301             /**/\r
302         } catch (COPSException e) {\r
303             throw new COPSPdpException("Error making Msg");\r
304         }\r
305 \r
306         //** Send the decision\r
307         //**\r
308         try {\r
309             decisionMsg.writeData(_sock);\r
310         } catch (IOException e) {\r
311             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());\r
312         }\r
313     }\r
314 \r
315     /**\r
316      * Sends a message asking that the request state be deleted\r
317      * @throws   COPSPdpException\r
318      */\r
319     public void sendDeleteRequestState()\r
320     throws COPSPdpException {\r
321         /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>\r
322          *                          <Client Handle>\r
323          *                          *(<Decision>)\r
324          *                          [<Integrity>]\r
325          * <Decision> ::= <Context>\r
326          *                  <Decision: Flags>\r
327          * <Decision: Flags> ::= Remove Request-State\r
328          *\r
329         */\r
330 \r
331         // Common Header with the same ClientType as the request (default UNSOLICITED)\r
332         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());\r
333 \r
334         // Client Handle with the same clientHandle as the request\r
335         COPSHandle clienthandle = new COPSHandle();\r
336         clienthandle.setId(_handle.getId());\r
337 \r
338         // Decisions\r
339         //  <Context>\r
340         COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);\r
341         //  <Decision: Flags>\r
342         COPSDecision dec = new COPSDecision();\r
343         dec.setCmdCode(COPSDecision.DEC_REMOVE);\r
344         dec.setFlags(COPSDecision.F_REQSTATE);\r
345 \r
346         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();\r
347         try {\r
348             decisionMsg.add(hdr);\r
349             decisionMsg.add(clienthandle);\r
350             decisionMsg.addDecision(dec, cntxt);\r
351         } catch (COPSException e) {\r
352             throw new COPSPdpException("Error making Msg");\r
353         }\r
354 \r
355         try {\r
356             decisionMsg.writeData(_sock);\r
357         } catch (IOException e) {\r
358             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());\r
359         }\r
360     }\r
361 \r
362     /**\r
363      * Sends a request asking that a new request state be created\r
364      * @throws   COPSPdpException\r
365      */\r
366     public void sendOpenNewRequestState()\r
367     throws COPSPdpException {\r
368         /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>\r
369          *                          <Client Handle>\r
370          *                          *(<Decision>)\r
371          *                          [<Integrity>]\r
372          * <Decision> ::= <Context>\r
373          *                  <Decision: Flags>\r
374          * <Decision: Flags> ::= Install Request-State\r
375          *\r
376         */\r
377 \r
378         // Common Header with the same ClientType as the request (default UNSOLICITED)\r
379         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());\r
380 \r
381         // Client Handle with the same clientHandle as the request\r
382         COPSHandle clienthandle = new COPSHandle();\r
383         clienthandle.setId(_handle.getId());\r
384 \r
385         // Decisions\r
386         //  <Context>\r
387         COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);\r
388         //  <Decision: Flags>\r
389         COPSDecision dec = new COPSDecision();\r
390         dec.setCmdCode(COPSDecision.DEC_INSTALL);\r
391         dec.setFlags(COPSDecision.F_REQSTATE);\r
392 \r
393         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();\r
394         try {\r
395             decisionMsg.add(hdr);\r
396             decisionMsg.add(clienthandle);\r
397             decisionMsg.addDecision(dec, cntxt);\r
398         } catch (COPSException e) {\r
399             throw new COPSPdpException("Error making Msg");\r
400         }\r
401 \r
402         try {\r
403             decisionMsg.writeData(_sock);\r
404         } catch (IOException e) {\r
405             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());\r
406         }\r
407     }\r
408 \r
409     /**\r
410      * Sends a message asking for a COPS sync operation\r
411      * @throws COPSPdpException\r
412      */\r
413     public void sendSyncRequestState()\r
414     throws COPSPdpException {\r
415         /* <Synchronize State Request>  ::= <Common Header>\r
416          *                                  [<Client Handle>]\r
417          *                                  [<Integrity>]\r
418          */\r
419 \r
420         // Common Header with the same ClientType as the request\r
421         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType());\r
422 \r
423         // Client Handle with the same clientHandle as the request\r
424         COPSHandle clienthandle = new COPSHandle();\r
425         clienthandle.setId(_handle.getId());\r
426 \r
427         COPSSyncStateMsg msg = new COPSSyncStateMsg();\r
428         try {\r
429             msg.add(hdr);\r
430             msg.add(clienthandle);\r
431         } catch (Exception e) {\r
432             throw new COPSPdpException("Error making Msg");\r
433         }\r
434 \r
435         try {\r
436             msg.writeData(_sock);\r
437         } catch (IOException e) {\r
438             throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());\r
439         }\r
440     }\r
441 }\r