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.net.Socket;
\r
10 import java.util.Enumeration;
\r
11 import java.util.Hashtable;
\r
12 import java.util.Vector;
\r
14 import org.umu.cops.stack.COPSClientSI;
\r
15 import org.umu.cops.stack.COPSContext;
\r
16 import org.umu.cops.stack.COPSData;
\r
17 import org.umu.cops.stack.COPSDeleteMsg;
\r
18 import org.umu.cops.stack.COPSError;
\r
19 import org.umu.cops.stack.COPSHandle;
\r
20 import org.umu.cops.stack.COPSHeader;
\r
21 import org.umu.cops.stack.COPSPrObjBase;
\r
22 import org.umu.cops.stack.COPSReportMsg;
\r
23 import org.umu.cops.stack.COPSReportType;
\r
24 import org.umu.cops.stack.COPSReqMsg;
\r
25 import org.umu.cops.stack.COPSSyncStateMsg;
\r
28 * State manager class for provisioning requests, at the PDP side.
\r
30 public class COPSPdpReqStateMan {
\r
33 * Request State created
\r
35 public final static short ST_CREATE = 1;
\r
39 public final static short ST_INIT = 2;
\r
43 public final static short ST_DECS = 3;
\r
47 public final static short ST_REPORT = 4;
\r
49 * Request State finalized
\r
51 public final static short ST_FINAL = 5;
\r
53 * New Request State solicited
\r
55 public final static short ST_NEW = 6;
\r
57 * Delete Request State solicited
\r
59 public final static short ST_DEL = 7;
\r
63 public final static short ST_SYNC = 8;
\r
67 public final static short ST_SYNCALL = 9;
\r
69 * Close connection received
\r
71 public final static short ST_CCONN = 10;
\r
73 * Keep-alive timeout
\r
75 public final static short ST_NOKA = 11;
\r
77 * Accounting timeout
\r
79 public final static short ST_ACCT = 12;
\r
82 * COPS client-type that identifies the policy client
\r
84 protected short _clientType;
\r
87 * COPS client handle used to uniquely identify a particular
\r
88 * PEP's request for a client-type
\r
90 protected COPSHandle _handle;
\r
93 * Object for performing policy data processing
\r
95 protected COPSPdpDataProcess _process;
\r
98 * Current state of the request being managed
\r
100 protected short _status;
\r
102 /** COPS message transceiver used to send COPS messages */
\r
103 protected COPSPdpMsgSender _sender;
\r
106 * Creates a request state manager
\r
107 * @param clientType Client-type
\r
108 * @param clientHandle Client handle
\r
110 public COPSPdpReqStateMan(short clientType, String clientHandle) {
\r
112 _handle = new COPSHandle();
\r
113 COPSData id = new COPSData(clientHandle);
\r
116 _clientType = clientType;
\r
118 _status = ST_CREATE;
\r
122 * Gets the client handle
\r
123 * @return Client's <tt>COPSHandle</tt>
\r
125 public COPSHandle getClientHandle() {
\r
130 * Gets the client-type
\r
131 * @return Client-type value
\r
133 public short getClientType() {
\r
134 return _clientType;
\r
138 * Gets the status of the request
\r
139 * @return Request state value
\r
141 public short getStatus() {
\r
146 * Gets the policy data processing object
\r
147 * @return Policy data processing object
\r
149 public COPSPdpDataProcess getDataProcess() {
\r
154 * Sets the policy data processing object
\r
155 * @param process Policy data processing object
\r
157 public void setDataProcess(COPSPdpDataProcess process) {
\r
158 _process = process;
\r
162 * Called when COPS sync is completed
\r
163 * @param repMsg COPS sync message
\r
164 * @throws COPSPdpException
\r
166 protected void processSyncComplete(COPSSyncStateMsg repMsg)
\r
167 throws COPSPdpException {
\r
169 _status = ST_SYNCALL;
\r
171 // maybe we should notifySyncComplete ...
\r
175 * Initializes a new request state over a socket
\r
176 * @param sock Socket to the PEP
\r
177 * @throws COPSPdpException
\r
179 protected void initRequestState(Socket sock)
\r
180 throws COPSPdpException {
\r
181 // Inits an object for sending COPS messages to the PEP
\r
182 _sender = new COPSPdpMsgSender(_clientType, _handle, sock);
\r
189 * Processes a COPS request
\r
190 * @param msg COPS request received from the PEP
\r
191 * @throws COPSPdpException
\r
193 protected void processRequest(COPSReqMsg msg)
\r
194 throws COPSPdpException {
\r
196 COPSHeader hdrmsg = msg.getHeader();
\r
197 COPSHandle handlemsg = msg.getClientHandle();
\r
198 COPSContext contextmsg = msg.getContext();
\r
200 //** Analyze the request
\r
203 /* <Request> ::= <Common Header>
\r
206 * *(<Named ClientSI>)
\r
208 * <Named ClientSI> ::= <*(<PRID> <EPD>)>
\r
210 * Very important, this is actually being treated like this:
\r
211 * <Named ClientSI> ::= <PRID> | <EPD>
\r
215 Vector clientSIs = msg.getClientSI();
\r
216 Hashtable reqSIs = new Hashtable(40);
\r
217 String strobjprid = new String();
\r
218 for (Enumeration e = clientSIs.elements() ; e.hasMoreElements() ;) {
\r
219 COPSClientSI clientSI = (COPSClientSI) e.nextElement();
\r
221 COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
\r
222 switch (obj.getSNum())
\r
224 case COPSPrObjBase.PR_PRID:
\r
225 strobjprid = obj.getData().str();
\r
227 case COPSPrObjBase.PR_EPD:
\r
228 reqSIs.put(strobjprid, obj.getData().str());
\r
229 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
\r
230 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
\r
237 //** Here we must retrieve a decision depending on
\r
238 //** the supplied ClientSIs
\r
239 // reqSIs is a hashtable with the prid and epds
\r
241 // ................
\r
243 Hashtable removeDecs = new Hashtable();
\r
244 Hashtable installDecs = new Hashtable();
\r
245 _process.setClientData(this, reqSIs);
\r
247 removeDecs = _process.getRemovePolicy(this);
\r
248 installDecs = _process.getInstallPolicy(this);
\r
250 //** We create the SOLICITED decision
\r
252 _sender.sendDecision(removeDecs, installDecs);
\r
258 * Processes a report
\r
259 * @param msg Report message from the PEP
\r
260 * @throws COPSPdpException
\r
262 protected void processReport(COPSReportMsg msg)
\r
263 throws COPSPdpException {
\r
265 //** Analyze the report
\r
269 * <Report State> ::= <Common Header>
\r
272 * *(<Named ClientSI>)
\r
274 * <Named ClientSI: Report> ::= <[<GPERR>] *(<report>)>
\r
275 * <report> ::= <ErrorPRID> <CPERR> *(<PRID><EPD>)
\r
277 * Important, <Named ClientSI> is not parsed
\r
280 // COPSHeader hdrmsg = msg.getHeader();
\r
281 // COPSHandle handlemsg = msg.getClientHandle();
\r
284 COPSReportType rtypemsg = msg.getReport();
\r
287 Vector clientSIs = msg.getClientSI();
\r
288 Hashtable repSIs = new Hashtable(40);
\r
289 String strobjprid = new String();
\r
290 for (Enumeration e = clientSIs.elements() ; e.hasMoreElements() ;) {
\r
291 COPSClientSI clientSI = (COPSClientSI) e.nextElement();
\r
293 COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
\r
294 switch (obj.getSNum()) {
\r
295 case COPSPrObjBase.PR_PRID:
\r
296 strobjprid = obj.getData().str();
\r
298 case COPSPrObjBase.PR_EPD:
\r
299 repSIs.put(strobjprid, obj.getData().str());
\r
300 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
\r
301 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
\r
308 //** Here we must act in accordance with
\r
309 //** the report received
\r
310 if (rtypemsg.isSuccess()) {
\r
311 _status = ST_REPORT;
\r
312 _process.successReport(this, repSIs);
\r
313 } else if (rtypemsg.isFailure()) {
\r
314 _status = ST_REPORT;
\r
315 _process.failReport(this, repSIs);
\r
316 } else if (rtypemsg.isAccounting()) {
\r
318 _process.acctReport(this, repSIs);
\r
323 * Called when connection is closed
\r
324 * @param error Reason
\r
325 * @throws COPSPdpException
\r
327 protected void processClosedConnection(COPSError error)
\r
328 throws COPSPdpException {
\r
329 if (_process != null)
\r
330 _process.notifyClosedConnection(this, error);
\r
332 _status = ST_CCONN;
\r
336 * Called when no keep-alive is received
\r
337 * @throws COPSPdpException
\r
339 protected void processNoKAConnection()
\r
340 throws COPSPdpException {
\r
341 if (_process != null)
\r
342 _process.notifyNoKAliveReceived(this);
\r
348 * Deletes the request state
\r
349 * @throws COPSPdpException
\r
351 protected void finalizeRequestState()
\r
352 throws COPSPdpException {
\r
353 _sender.sendDeleteRequestState();
\r
354 _status = ST_FINAL;
\r
358 * Asks for a COPS sync
\r
359 * @throws COPSPdpException
\r
361 protected void syncRequestState()
\r
362 throws COPSPdpException {
\r
363 _sender.sendSyncRequestState();
\r
368 * Opens a new request state
\r
369 * @throws COPSPdpException
\r
371 protected void openNewRequestState()
\r
372 throws COPSPdpException {
\r
373 _sender.sendOpenNewRequestState();
\r
378 * Processes a COPS delete message
\r
379 * @param dMsg <tt>COPSDeleteMsg</tt> received from the PEP
\r
380 * @throws COPSPdpException
\r
382 protected void processDeleteRequestState(COPSDeleteMsg dMsg)
\r
383 throws COPSPdpException {
\r
384 if (_process != null)
\r
385 _process.closeRequestState(this);
\r