Initial transfer of assests from github
[packetcable.git] / src / main / java / org / umu / cops / ospdp / COPSPdpOSMsgSender.java
1 package org.umu.cops.ospdp;\r
2 \r
3 import java.io.IOException;\r
4 import java.net.Socket;\r
5 import java.util.Enumeration;\r
6 import java.util.Vector;\r
7 \r
8 import org.umu.cops.stack.COPSContext;\r
9 import org.umu.cops.stack.COPSDecision;\r
10 import org.umu.cops.stack.COPSDecisionMsg;\r
11 import org.umu.cops.stack.COPSException;\r
12 import org.umu.cops.stack.COPSHandle;\r
13 import org.umu.cops.stack.COPSHeader;\r
14 import org.umu.cops.stack.COPSSyncStateMsg;\r
15 \r
16 /**\r
17  * COPS message transceiver class for outsourcing connections at the PDP side.\r
18  */\r
19 public class COPSPdpOSMsgSender {\r
20     /**\r
21      * Socket connected to PEP\r
22      */\r
23     protected Socket _sock;\r
24 \r
25     /**\r
26      * COPS client-type that identifies the policy client\r
27      */\r
28     protected short _clientType;\r
29 \r
30     /**\r
31      * COPS client handle used to uniquely identify a particular\r
32      * PEP's request for a client-type\r
33      */\r
34     protected COPSHandle _handle;\r
35 \r
36     /**\r
37      * Creates a COPSPepMsgSender\r
38      *\r
39      * @param clientType        COPS client-type\r
40      * @param clientHandle      Client handle\r
41      * @param sock              Socket to the PEP\r
42      */\r
43     public COPSPdpOSMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {\r
44         // COPS Handle\r
45         _handle = clientHandle;\r
46         _clientType = clientType;\r
47 \r
48         _sock = sock;\r
49     }\r
50 \r
51     /**\r
52      * Gets the client handle\r
53      * @return   Client's <tt>COPSHandle</tt>\r
54      */\r
55     public COPSHandle getClientHandle() {\r
56         return _handle;\r
57     }\r
58 \r
59     /**\r
60      * Gets the client-type\r
61      * @return   Client-type value\r
62      */\r
63     public short getClientType() {\r
64         return _clientType;\r
65     }\r
66 \r
67     /**\r
68      * Sends a decision message which was requested by the PEP\r
69      * @param removeDecs    Decisions to be removed\r
70      * @param installDecs   Decisions to be installed\r
71      * @throws COPSPdpException\r
72      */\r
73     public void sendSolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {\r
74         sendDecision(removeDecs, installDecs, true);\r
75     }\r
76 \r
77     /**\r
78      * Sends a decision message which was not requested by the PEP\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 sendUnsolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {\r
84         sendDecision(removeDecs, installDecs, false);\r
85     }\r
86 \r
87     /**\r
88      * Sends a decision message to the PEP\r
89      * @param removeDecs    Decisions to be removed\r
90      * @param installDecs   Decisions to be installed\r
91      * @param solicited     <tt>true</tt> if the PEP requested this decision, <tt>false</tt> otherwise\r
92      * @throws COPSPdpException\r
93      */\r
94     public void sendDecision(Vector removeDecs, Vector installDecs, boolean solicited) throws COPSPdpException {\r
95         // Common Header holding the same ClientType as the request\r
96         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());\r
97 \r
98         if (solicited)\r
99             hdr.setFlag(COPSHeader.COPS_FLAG_SOLICITED);\r
100 \r
101         // Client Handle with the same clientHandle as the request\r
102         COPSHandle handle = new COPSHandle();\r
103         handle.setId(getClientHandle().getId());\r
104 \r
105         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();\r
106         try {\r
107             decisionMsg.add(hdr);\r
108             decisionMsg.add(handle);\r
109 \r
110             // Decisions (no flags supplied)\r
111             //  <Context>\r
112             COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);\r
113 \r
114             // Remove Decisions\r
115             //  <Decision: Flags>\r
116             if (removeDecs.size() > 0) {\r
117                 COPSDecision rdec1 = new COPSDecision();\r
118                 rdec1.setCmdCode(COPSDecision.DEC_REMOVE);\r
119 \r
120                 decisionMsg.addDecision(rdec1, cntxt);\r
121 \r
122                 Enumeration removeDecsEnum = removeDecs.elements();\r
123                 while (removeDecsEnum.hasMoreElements())\r
124                     decisionMsg.addDecision((COPSDecision) removeDecsEnum.nextElement(), cntxt);\r
125             }\r
126 \r
127             // Install Decisions\r
128             //  <Decision: Flags>\r
129             if (installDecs.size() > 0) {\r
130                 COPSDecision idec1 = new COPSDecision();\r
131                 idec1.setCmdCode(COPSDecision.DEC_INSTALL);\r
132 \r
133                 decisionMsg.addDecision(idec1, cntxt);\r
134 \r
135                 Enumeration installDecsEnum = installDecs.elements();\r
136                 while (installDecsEnum.hasMoreElements())\r
137                     decisionMsg.addDecision((COPSDecision) installDecsEnum.nextElement(), cntxt);\r
138                 /**\r
139                 COPSIntegrity intr = new COPSIntegrity();\r
140                 intr.setKeyId(19);\r
141                 intr.setSeqNum(9);\r
142                 intr.setKeyDigest(new COPSData("KEY DIGEST"));\r
143                 decisionMsg.add(intr);\r
144                 /**/\r
145             }\r
146         } catch (COPSException e) {\r
147             e.printStackTrace();\r
148             throw new COPSPdpException("Error making Msg");\r
149         }\r
150 \r
151         //** Send decision\r
152         //**\r
153         try {\r
154             decisionMsg.writeData(_sock);\r
155         } catch (IOException e) {\r
156             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());\r
157         }\r
158     }\r
159 \r
160     /**FIXME: unused?\r
161      * Sends a message asking that the request state be deleted\r
162      * @throws   COPSPdpException\r
163      */\r
164     public void sendDeleteRequestState() throws COPSPdpException {\r
165         /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>\r
166          *                          <Client Handle>\r
167          *                          *(<Decision>)\r
168          *                          [<Integrity>]\r
169          * <Decision> ::= <Context>\r
170          *                  <Decision: Flags>\r
171          * <Decision: Flags> ::= Remove Request-State\r
172          *\r
173         */\r
174 \r
175         // Common Header with the same ClientType as the request (default UNSOLICITED)\r
176         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());\r
177 \r
178         // Client Handle with the same clientHandle as the request\r
179         COPSHandle clienthandle = new COPSHandle();\r
180         clienthandle.setId(_handle.getId());\r
181 \r
182         // Decisions\r
183         //  <Context>\r
184         COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);\r
185         //  <Decision: Flags>\r
186         COPSDecision dec = new COPSDecision();\r
187         dec.setCmdCode(COPSDecision.DEC_REMOVE);\r
188         dec.setFlags(COPSDecision.F_REQSTATE);\r
189 \r
190         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();\r
191         try {\r
192             decisionMsg.add(hdr);\r
193             decisionMsg.add(clienthandle);\r
194             decisionMsg.addDecision(dec, cntxt);\r
195         } catch (COPSException e) {\r
196             throw new COPSPdpException("Error making Msg");\r
197         }\r
198 \r
199         try {\r
200             decisionMsg.writeData(_sock);\r
201         } catch (IOException e) {\r
202             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());\r
203         }\r
204     }\r
205 \r
206     /**\r
207      * Method sendOpenNewRequestState\r
208      *\r
209      * @throws   COPSPdpException\r
210      *\r
211      */\r
212     //FIXME: Unused?\r
213     public void sendOpenNewRequestState() throws COPSPdpException {\r
214         /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>\r
215          *                          <Client Handle>\r
216          *                          *(<Decision>)\r
217          *                          [<Integrity>]\r
218          * <Decision> ::= <Context>\r
219          *                  <Decision: Flags>\r
220          * <Decision: Flags> ::= Install Request-State\r
221          *\r
222         */\r
223 \r
224         // Common Header with the same ClientType as the request (default UNSOLICITED)\r
225         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());\r
226 \r
227         // Client Handle with the same clientHandle as the request\r
228         COPSHandle clienthandle = new COPSHandle();\r
229         clienthandle.setId(_handle.getId());\r
230 \r
231         // Decisions\r
232         //  <Context>\r
233         COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);\r
234         //  <Decision: Flags>\r
235         COPSDecision dec = new COPSDecision();\r
236         dec.setCmdCode(COPSDecision.DEC_INSTALL);\r
237         dec.setFlags(COPSDecision.F_REQSTATE);\r
238 \r
239         COPSDecisionMsg decisionMsg = new COPSDecisionMsg();\r
240         try {\r
241             decisionMsg.add(hdr);\r
242             decisionMsg.add(clienthandle);\r
243             decisionMsg.addDecision(dec, cntxt);\r
244         } catch (COPSException e) {\r
245             throw new COPSPdpException("Error making Msg");\r
246         }\r
247 \r
248         try {\r
249             decisionMsg.writeData(_sock);\r
250         } catch (IOException e) {\r
251             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());\r
252         }\r
253     }\r
254 \r
255     /**\r
256      * Sends a message asking for a COPS sync operation\r
257      * @throws COPSPdpException\r
258      */\r
259     public void sendSyncRequestState()\r
260     throws COPSPdpException {\r
261         /* <Synchronize State Request>  ::= <Common Header>\r
262          *                                  [<Client Handle>]\r
263          *                                  [<Integrity>]\r
264          */\r
265 \r
266         // Common Header with the same ClientType as the request\r
267         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType());\r
268 \r
269         // Client Handle with the same clientHandle as the request\r
270         COPSHandle clienthandle = new COPSHandle();\r
271         clienthandle.setId(_handle.getId());\r
272 \r
273         COPSSyncStateMsg msg = new COPSSyncStateMsg();\r
274         try {\r
275             msg.add(hdr);\r
276             msg.add(clienthandle);\r
277         } catch (Exception e) {\r
278             throw new COPSPdpException("Error making Msg");\r
279         }\r
280 \r
281         try {\r
282             msg.writeData(_sock);\r
283         } catch (IOException e) {\r
284             throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());\r
285         }\r
286     }\r
287 \r
288 }\r