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
7 package org.umu.cops.prpdp;
\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
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
26 * COPS message transceiver class for provisioning connections at the PDP side.
\r
28 public class COPSPdpMsgSender {
\r
31 * Socket connected to PEP
\r
33 protected Socket _sock;
\r
36 * COPS client-type that identifies the policy client
\r
38 protected short _clientType;
\r
41 * COPS client handle used to uniquely identify a particular
\r
42 * PEP's request for a client-type
\r
44 protected COPSHandle _handle;
\r
47 * Creates a COPSPepMsgSender
\r
49 * @param clientType COPS client-type
\r
50 * @param clientHandle Client handle
\r
51 * @param sock Socket to the PEP
\r
53 public COPSPdpMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {
\r
55 _handle = clientHandle;
\r
56 _clientType = clientType;
\r
62 * Gets the client handle
\r
63 * @return Client's <tt>COPSHandle</tt>
\r
65 public COPSHandle getClientHandle() {
\r
70 * Gets the client-type
\r
71 * @return Client-type value
\r
73 public short getClientType() {
\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
83 public void sendDecision(Hashtable removeDecs, Hashtable installDecs)
\r
84 throws COPSPdpException {
\r
85 /* <Decision Message> ::= <Common Header: Flag SOLICITED>
\r
87 * *(<Decision>) | <Error>
\r
89 * <Decision> ::= <Context>
\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
98 * Very important, this is actually being treated like this:
\r
99 * <Install Decision> ::= <PRID> | <EPD>
\r
100 * <Remove Decision> ::= <PRID> | <PPRID>
\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
108 // Client Handle with the same clientHandle as the request
\r
109 COPSHandle handle = new COPSHandle();
\r
110 handle.setId(getClientHandle().getId());
\r
112 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
\r
114 decisionMsg.add(hdr);
\r
115 decisionMsg.add(handle);
\r
117 // Decisions (no flags supplied)
\r
119 COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);
\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
127 decisionMsg.addDecision(rdec1, cntxt);
\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
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
144 decisionMsg.addDecision(dec2, cntxt);
\r
145 decisionMsg.addDecision(dec3, cntxt);
\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
155 decisionMsg.addDecision(idec1, cntxt);
\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
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
172 decisionMsg.addDecision(dec2, cntxt);
\r
173 decisionMsg.addDecision(dec3, cntxt);
\r
177 COPSIntegrity intr = new COPSIntegrity();
\r
180 intr.setKeyDigest(new COPSData("KEY DIGEST"));
\r
181 decisionMsg.add(intr);
\r
184 } catch (COPSException e) {
\r
185 throw new COPSPdpException("Error making Msg");
\r
188 //** Send the decision
\r
191 decisionMsg.writeData(_sock);
\r
192 } catch (IOException e) {
\r
193 throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
\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
203 public void sendUnsolicitedDecision(Hashtable removeDecs, Hashtable installDecs)
\r
204 throws COPSPdpException {
\r
205 //** Example of an UNSOLICITED decision
\r
208 /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
\r
210 * *(<Decision>) | <Error>
\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
221 * Very important, this is actually being treated like this:
\r
222 * <Install Decision> ::= <PRID> | <EPD>
\r
223 * <Remove Decision> ::= <PRID> | <PPRID>
\r
227 // Common Header with the same ClientType as the request
\r
228 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
\r
230 // Client Handle with the same clientHandle as the request
\r
231 COPSHandle handle = new COPSHandle();
\r
232 handle.setId(getClientHandle().getId());
\r
234 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
\r
236 decisionMsg.add(hdr);
\r
237 decisionMsg.add(handle);
\r
239 // Decisions (no flags supplied)
\r
241 COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);
\r
243 // Remove Decisions
\r
244 // <Decision: Flags>
\r
245 COPSDecision rdec1 = new COPSDecision();
\r
246 rdec1.setCmdCode(COPSDecision.DEC_REMOVE);
\r
248 decisionMsg.addDecision(rdec1, cntxt);
\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
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
265 decisionMsg.addDecision(dec2, cntxt);
\r
266 decisionMsg.addDecision(dec3, cntxt);
\r
269 // Install Decisions
\r
270 // <Decision: Flags>
\r
271 COPSDecision idec1 = new COPSDecision();
\r
272 idec1.setCmdCode(COPSDecision.DEC_INSTALL);
\r
274 decisionMsg.addDecision(idec1, cntxt);
\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
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
291 decisionMsg.addDecision(dec2, cntxt);
\r
292 decisionMsg.addDecision(dec3, cntxt);
\r
296 COPSIntegrity intr = new COPSIntegrity();
\r
299 intr.setKeyDigest(new COPSData("KEY DIGEST"));
\r
300 decisionMsg.add(intr);
\r
302 } catch (COPSException e) {
\r
303 throw new COPSPdpException("Error making Msg");
\r
306 //** Send the decision
\r
309 decisionMsg.writeData(_sock);
\r
310 } catch (IOException e) {
\r
311 throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
\r
316 * Sends a message asking that the request state be deleted
\r
317 * @throws COPSPdpException
\r
319 public void sendDeleteRequestState()
\r
320 throws COPSPdpException {
\r
321 /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
\r
325 * <Decision> ::= <Context>
\r
326 * <Decision: Flags>
\r
327 * <Decision: Flags> ::= Remove Request-State
\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
334 // Client Handle with the same clientHandle as the request
\r
335 COPSHandle clienthandle = new COPSHandle();
\r
336 clienthandle.setId(_handle.getId());
\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
346 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
\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
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
363 * Sends a request asking that a new request state be created
\r
364 * @throws COPSPdpException
\r
366 public void sendOpenNewRequestState()
\r
367 throws COPSPdpException {
\r
368 /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
\r
372 * <Decision> ::= <Context>
\r
373 * <Decision: Flags>
\r
374 * <Decision: Flags> ::= Install Request-State
\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
381 // Client Handle with the same clientHandle as the request
\r
382 COPSHandle clienthandle = new COPSHandle();
\r
383 clienthandle.setId(_handle.getId());
\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
393 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
\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
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
410 * Sends a message asking for a COPS sync operation
\r
411 * @throws COPSPdpException
\r
413 public void sendSyncRequestState()
\r
414 throws COPSPdpException {
\r
415 /* <Synchronize State Request> ::= <Common Header>
\r
416 * [<Client Handle>]
\r
420 // Common Header with the same ClientType as the request
\r
421 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType());
\r
423 // Client Handle with the same clientHandle as the request
\r
424 COPSHandle clienthandle = new COPSHandle();
\r
425 clienthandle.setId(_handle.getId());
\r
427 COPSSyncStateMsg msg = new COPSSyncStateMsg();
\r
430 msg.add(clienthandle);
\r
431 } catch (Exception e) {
\r
432 throw new COPSPdpException("Error making Msg");
\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