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.prpdp;
9 import org.umu.cops.stack.*;
11 import java.net.Socket;
12 import java.util.HashMap;
16 * State manager class for provisioning requests, at the PDP side.
18 public class COPSPdpReqStateMan {
21 * Request State created
23 public final static short ST_CREATE = 1;
27 public final static short ST_INIT = 2;
31 public final static short ST_DECS = 3;
35 public final static short ST_REPORT = 4;
37 * Request State finalized
39 public final static short ST_FINAL = 5;
41 * New Request State solicited
43 public final static short ST_NEW = 6;
45 * Delete Request State solicited
47 public final static short ST_DEL = 7;
51 public final static short ST_SYNC = 8;
55 public final static short ST_SYNCALL = 9;
57 * Close connection received
59 public final static short ST_CCONN = 10;
63 public final static short ST_NOKA = 11;
67 public final static short ST_ACCT = 12;
70 * COPS client-type that identifies the policy client
72 protected short _clientType;
75 * COPS client handle used to uniquely identify a particular
76 * PEP's request for a client-type
78 protected COPSHandle _handle;
81 * Object for performing policy data processing
83 protected COPSPdpDataProcess _process;
86 * Current state of the request being managed
88 protected short _status;
90 /** COPS message transceiver used to send COPS messages */
91 protected COPSPdpMsgSender _sender;
94 * Creates a request state manager
95 * @param clientType Client-type
96 * @param clientHandle Client handle
98 public COPSPdpReqStateMan(short clientType, String clientHandle) {
99 _handle = new COPSHandle(new COPSData(clientHandle));
100 _clientType = clientType;
105 * Gets the client handle
106 * @return Client's <tt>COPSHandle</tt>
108 public COPSHandle getClientHandle() {
113 * Gets the client-type
114 * @return Client-type value
116 public int getClientType() {
121 * Gets the status of the request
122 * @return Request state value
124 public short getStatus() {
129 * Gets the policy data processing object
130 * @return Policy data processing object
132 public COPSPdpDataProcess getDataProcess() {
137 * Sets the policy data processing object
138 * @param process Policy data processing object
140 public void setDataProcess(COPSPdpDataProcess process) {
145 * Called when COPS sync is completed
146 * @param repMsg COPS sync message
147 * @throws COPSPdpException
149 protected void processSyncComplete(COPSSyncStateMsg repMsg)
150 throws COPSPdpException {
152 _status = ST_SYNCALL;
154 // maybe we should notifySyncComplete ...
158 * Initializes a new request state over a socket
159 * @param sock Socket to the PEP
160 * @throws COPSPdpException
162 protected void initRequestState(Socket sock)
163 throws COPSPdpException {
164 // Inits an object for sending COPS messages to the PEP
165 _sender = new COPSPdpMsgSender(_clientType, _handle, sock);
172 * Processes a COPS request
173 * @param msg COPS request received from the PEP
174 * @throws COPSPdpException
176 protected void processRequest(COPSReqMsg msg)
177 throws COPSPdpException {
179 COPSHeader hdrmsg = msg.getHeader();
180 COPSHandle handlemsg = msg.getClientHandle();
181 COPSContext contextmsg = msg.getContext();
183 //** Analyze the request
186 /* <Request> ::= <Common Header>
189 * *(<Named ClientSI>)
191 * <Named ClientSI> ::= <*(<PRID> <EPD>)>
193 * Very important, this is actually being treated like this:
194 * <Named ClientSI> ::= <PRID> | <EPD>
198 Vector clientSIs = msg.getClientSI();
199 Hashtable reqSIs = new Hashtable(40);
200 String strobjprid = new String();
201 for (Enumeration e = clientSIs.elements() ; e.hasMoreElements() ;) {
202 COPSClientSI clientSI = (COPSClientSI) e.nextElement();
204 COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
205 switch (obj.getSNum())
207 case COPSPrObjBase.PR_PRID:
208 strobjprid = obj.getData().str();
210 case COPSPrObjBase.PR_EPD:
211 reqSIs.put(strobjprid, obj.getData().str());
212 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
213 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
220 //** Here we must retrieve a decision depending on
221 //** the supplied ClientSIs
222 // reqSIs is a hashtable with the prid and epds
226 Hashtable removeDecs = new Hashtable();
227 Hashtable installDecs = new Hashtable();
228 _process.setClientData(this, reqSIs);
230 removeDecs = _process.getRemovePolicy(this);
231 installDecs = _process.getInstallPolicy(this);
233 //** We create the SOLICITED decision
235 _sender.sendDecision(removeDecs, installDecs);
242 * @param msg Report message from the PEP
243 * @throws COPSPdpException
245 protected void processReport(final COPSReportMsg msg) throws COPSPdpException {
247 //** Analyze the report
251 * <Report State> ::= <Common Header>
254 * *(<Named ClientSI>)
256 * <Named ClientSI: Report> ::= <[<GPERR>] *(<report>)>
257 * <report> ::= <ErrorPRID> <CPERR> *(<PRID><EPD>)
259 * Important, <Named ClientSI> is not parsed
262 // COPSHeader hdrmsg = msg.getHeader();
263 // COPSHandle handlemsg = msg.getClientHandle();
265 if (msg.getClientSI() != null) {
267 final COPSReportType rtypemsg = msg.getReport();
270 // final Set<COPSClientSI> clientSIs = msg.getClientSI();
271 final Map<String, String> repSIs = new HashMap<>();
272 String strobjprid = "";
273 // for (final COPSClientSI clientSI : clientSIs) {
274 final COPSPrObjBase obj = new COPSPrObjBase(msg.getClientSI().getData().getData());
275 switch (obj.getSNum()) {
276 case COPSPrObjBase.PR_PRID:
277 strobjprid = obj.getData().str();
279 case COPSPrObjBase.PR_EPD:
280 repSIs.put(strobjprid, obj.getData().str());
281 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
282 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
289 //** Here we must act in accordance with
290 //** the report received
291 switch (rtypemsg.getReportType()) {
294 _process.successReport(this, repSIs);
298 _process.failReport(this, repSIs);
302 _process.acctReport(this, repSIs);
311 * Called when connection is closed
312 * @param error Reason
313 * @throws COPSPdpException
315 protected void processClosedConnection(COPSError error)
316 throws COPSPdpException {
317 if (_process != null)
318 _process.notifyClosedConnection(this, error);
324 * Called when no keep-alive is received
325 * @throws COPSPdpException
327 protected void processNoKAConnection()
328 throws COPSPdpException {
329 if (_process != null)
330 _process.notifyNoKAliveReceived(this);
336 * Deletes the request state
337 * @throws COPSPdpException
339 protected void finalizeRequestState()
340 throws COPSPdpException {
341 _sender.sendDeleteRequestState();
346 * Asks for a COPS sync
347 * @throws COPSPdpException
349 protected void syncRequestState()
350 throws COPSPdpException {
351 _sender.sendSyncRequestState();
356 * Opens a new request state
357 * @throws COPSPdpException
359 protected void openNewRequestState()
360 throws COPSPdpException {
361 _sender.sendOpenNewRequestState();
366 * Processes a COPS delete message
367 * @param dMsg <tt>COPSDeleteMsg</tt> received from the PEP
368 * @throws COPSPdpException
370 protected void processDeleteRequestState(COPSDeleteMsg dMsg)
371 throws COPSPdpException {
372 if (_process != null)
373 _process.closeRequestState(this);