2 * Copyright (c) 2004 University of Murcia. All rights reserved.
3 * --------------------------------------------------------------
4 * For more information, please see <http://www.umu.euro6ix.org/>.
7 package org.umu.cops.prpep;
9 import org.umu.cops.stack.*;
10 import org.umu.cops.stack.COPSDecision.Command;
12 import java.net.Socket;
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15 import java.util.Vector;
18 * COPSPepReqStateMan manages Request State using Client Handle (RFC 2748 pag. 21)
21 * The client handle is used to identify a unique request state for a
22 * single PEP per client-type. Client handles are chosen by the PEP and
23 * are opaque to the PDP. The PDP simply uses the request handle to
24 * uniquely identify the request state for a particular Client-Type over
25 * a particular TCP connection and generically tie its decisions to a
26 * corresponding request. Client handles are initiated in request
27 * messages and are then used by subsequent request, decision, and
28 * report messages to reference the same request state. When the PEP is
29 * ready to remove a local request state, it will issue a delete message
30 * to the PDP for the corresponding client handle. A handle MUST be
31 * explicitly deleted by the PEP before it can be used by the PEP to
32 * identify a new request state. Handles referring to different request
33 * states MUST be unique within the context of a particular TCP
34 * connection and client-type.
36 * @version COPSPepReqStateMan.java, v 2.00 2004
39 public class COPSPepReqStateMan {
42 * Request State created
44 public final static short ST_CREATE = 1;
48 public final static short ST_INIT = 2;
52 public final static short ST_DECS = 3;
56 public final static short ST_REPORT = 4;
58 * Request State finalized
60 public final static short ST_FINAL = 5;
62 * New Request State solicited
64 public final static short ST_NEW = 6;
66 * Delete Request State solicited
68 public final static short ST_DEL = 7;
70 * SYNC Request received
72 public final static short ST_SYNC = 8;
76 public final static short ST_SYNCALL = 9;
78 * Close Connection received
80 public final static short ST_CCONN = 10;
84 public final static short ST_NOKA = 11;
88 public final static short ST_ACCT = 12;
91 * The client-type identifies the policy client
93 protected short _clientType;
96 * The client handle is used to uniquely identify a particular
97 * PEP's request for a client-type
99 protected COPSHandle _handle;
102 The PolicyDataProcess is used to process policy data in the PEP
104 protected COPSPepDataProcess _process;
107 * State Request State
109 protected short _status;
112 The Msg Sender is used to send COPS messages
114 protected COPSPepMsgSender _sender;
119 protected boolean _syncState;
122 * Create a State Request Manager
124 * @param clientHandle a Client Handle
127 public COPSPepReqStateMan(short clientType, String clientHandle) {
128 _handle = new COPSHandle(new COPSData(clientHandle));
129 _clientType = clientType;
135 * Return client handle
137 * @return a COPSHandle
140 public COPSHandle getClientHandle() {
150 public short getClientType() {
155 * Return Request State status
159 public short getStatus() {
164 * Return the Policy Data Process
166 * @return a PolicyConfigure
169 public COPSPepDataProcess getDataProcess() {
174 * Establish the Policy Data Process
176 * @param process a PolicyConfigure
179 public void setDataProcess(COPSPepDataProcess process) {
186 * @throws COPSPepException
189 protected void initRequestState(Socket sock)
190 throws COPSPepException {
191 // Inits an object for sending COPS messages to the PDP
192 _sender = new COPSPepMsgSender(_clientType, _handle, sock);
194 // If an object for retrieving PEP features exists,
195 // use it for retrieving them
197 if (_process != null)
198 clientSIs = _process.getClientData(this);
203 _sender.sendRequest(clientSIs);
210 * Finalize Request State
212 * @throws COPSPepException
215 protected void finalizeRequestState()
216 throws COPSPepException {
217 _sender.sendDeleteRequest();
222 * Process the message Decision
224 * @param dMsg a COPSDecisionMsg
226 * @throws COPSPepException
229 protected void processDecision(COPSDecisionMsg dMsg)
230 throws COPSPepException {
231 // COPSDebug.out(getClass().getName(), "ClientId:" + getClientHandle().getId().str());
233 // COPSHandle handle = dMsg.getClientHandle();
234 Hashtable decisions = dMsg.getDecisions();
236 Hashtable removeDecs = new Hashtable(40);
237 Hashtable installDecs = new Hashtable(40);
238 Hashtable errorDecs = new Hashtable(40);
239 for (Enumeration e = decisions.keys() ; e.hasMoreElements() ;) {
241 COPSContext context = (COPSContext) e.nextElement();
242 Vector v = (Vector) decisions.get(context);
243 Enumeration ee = v.elements();
244 COPSDecision cmddecision = (COPSDecision) ee.nextElement();
246 // cmddecision --> we must check whether it is an error!
248 if (cmddecision.getCommand().equals(Command.INSTALL)) {
250 for (; ee.hasMoreElements() ;) {
251 COPSDecision decision = (COPSDecision) ee.nextElement();
253 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
254 switch (obj.getSNum()) {
255 case COPSPrObjBase.PR_PRID:
256 prid = obj.getData().str();
258 case COPSPrObjBase.PR_EPD:
259 installDecs.put(prid, obj.getData().str());
267 if (cmddecision.getCommand().equals(Command.REMOVE)) {
270 for (; ee.hasMoreElements() ;) {
271 COPSDecision decision = (COPSDecision) ee.nextElement();
273 COPSPrObjBase obj = new COPSPrObjBase(decision.getData().getData());
274 switch (obj.getSNum()) {
275 case COPSPrObjBase.PR_PRID:
276 prid = obj.getData().str();
278 case COPSPrObjBase.PR_EPD:
279 removeDecs.put(prid, obj.getData().str());
288 //** Apply decisions to the configuration
289 _process.setDecisions(this, removeDecs, installDecs, errorDecs);
293 if (_process.isFailReport(this)) {
294 // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");
295 _sender.sendFailReport(_process.getReportData(this));
297 // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");
298 _sender.sendSuccessReport(_process.getReportData(this));
303 _sender.sendSyncComplete();
305 _status = ST_SYNCALL;
310 * Process the message NewRequestState
312 * @throws COPSPepException
315 protected void processOpenNewRequestState()
316 throws COPSPepException {
318 if (_process != null)
319 _process.newRequestState(this);
325 * Process the message DeleteRequestState
327 * @param dMsg a COPSDecisionMsg
329 * @throws COPSPepException
332 protected void processDeleteRequestState(COPSDecisionMsg dMsg)
333 throws COPSPepException {
334 if (_process != null)
335 _process.closeRequestState(this);
341 * Process the message SycnStateRequest.
342 * The message SycnStateRequest indicates that the remote PDP
343 * wishes the client (which appears in the common header)
344 * to re-send its state.
346 * @param ssMsg a COPSSyncStateMsg
348 * @throws COPSPepException
351 protected void processSyncStateRequest(COPSSyncStateMsg ssMsg)
352 throws COPSPepException {
354 // If an object for retrieving PEP features exists,
355 // use it for retrieving them
357 if (_process != null)
358 clientSIs = _process.getClientData(this);
363 _sender.sendRequest(clientSIs);
368 protected void processClosedConnection(COPSError error)
369 throws COPSPepException {
370 if (_process != null)
371 _process.notifyClosedConnection(this, error);
376 protected void processNoKAConnection()
377 throws COPSPepException {
378 if (_process != null)
379 _process.notifyNoKAliveReceived(this);
384 protected void processAcctReport()
385 throws COPSPepException {
387 Hashtable report = new Hashtable();
388 if (_process != null)
389 report = _process.getAcctData(this);
391 _sender.sendAcctReport(report);