7 import org.pcmm.gates.ITransactionID;
8 import org.pcmm.gates.impl.PCMMGateReq;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.umu.cops.prpdp.COPSPdpException;
12 import org.umu.cops.stack.*;
13 import org.umu.cops.stack.COPSReportType.ReportType;
15 import java.net.Socket;
19 * State manager class for provisioning requests, at the PDP side.
21 public class PCMMPdpReqStateMan {
23 public final static Logger logger = LoggerFactory.getLogger(PCMMPdpReqStateMan.class);
26 * Request State created
28 public final static short ST_CREATE = 1;
32 public final static short ST_INIT = 2;
36 public final static short ST_DECS = 3;
40 public final static short ST_REPORT = 4;
42 * Request State finalized
44 public final static short ST_FINAL = 5;
46 * New Request State solicited
48 public final static short ST_NEW = 6;
50 * Delete Request State solicited
52 public final static short ST_DEL = 7;
56 public final static short ST_SYNC = 8;
60 public final static short ST_SYNCALL = 9;
62 * Close connection received
64 public final static short ST_CCONN = 10;
68 public final static short ST_NOKA = 11;
72 public final static short ST_ACCT = 12;
75 * COPS client-type that identifies the policy client
77 protected short _clientType;
80 * COPS client handle used to uniquely identify a particular
81 * PEP's request for a client-type
83 protected COPSHandle _handle;
86 * Object for performing policy data processing
88 protected PCMMPdpDataProcess _process;
91 * Current state of the request being managed
93 protected short _status;
95 /** COPS message transceiver used to send COPS messages */
96 protected PCMMPdpMsgSender _sender;
99 * Creates a request state manager
100 * @param clientType Client-type
101 * @param clientHandle Client handle
103 public PCMMPdpReqStateMan(short clientType, String clientHandle) {
104 _handle = new COPSHandle(new COPSData(clientHandle));
105 _clientType = clientType;
110 * Gets the client handle
111 * @return Client's <tt>COPSHandle</tt>
113 public COPSHandle getClientHandle() {
118 * Gets the client-type
119 * @return Client-type value
121 public short getClientType() {
126 * Gets the status of the request
127 * @return Request state value
129 public short getStatus() {
134 * Gets the policy data processing object
135 * @return Policy data processing object
137 public PCMMPdpDataProcess getDataProcess() {
142 * Sets the policy data processing object
143 * @param process Policy data processing object
145 public void setDataProcess(PCMMPdpDataProcess process) {
150 * Called when COPS sync is completed
151 * @param repMsg COPS sync message
152 * @throws COPSPdpException
154 protected void processSyncComplete(COPSSyncStateMsg repMsg)
155 throws COPSPdpException {
157 _status = ST_SYNCALL;
159 // maybe we should notifySyncComplete ...
163 * Initializes a new request state over a socket
164 * @param sock Socket to the PEP
165 * @throws COPSPdpException
167 protected void initRequestState(Socket sock)
168 throws COPSPdpException {
169 // Inits an object for sending COPS messages to the PEP
170 _sender = new PCMMPdpMsgSender(_clientType, _handle, sock);
179 * Processes a COPS request
180 * @param msg COPS request received from the PEP
181 * @throws COPSPdpException
183 protected void processRequest(COPSReqMsg msg)
184 throws COPSPdpException {
186 COPSHeader hdrmsg = msg.getHeader();
187 COPSHandle handlemsg = msg.getClientHandle();
188 COPSContext contextmsg = msg.getContext();
190 //** Analyze the request
193 /* <Request> ::= <Common Header>
196 * *(<Named ClientSI>)
198 * <Named ClientSI> ::= <*(<PRID> <EPD>)>
200 * Very important, this is actually being treated like this:
201 * <Named ClientSI> ::= <PRID> | <EPD>
205 Vector clientSIs = msg.getClientSI();
206 Hashtable reqSIs = new Hashtable(40);
207 String strobjprid = new String();
208 for (Enumeration e = clientSIs.elements() ; e.hasMoreElements() ;) {
209 COPSClientSI clientSI = (COPSClientSI) e.nextElement();
211 COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
212 switch (obj.getSNum())
214 case COPSPrObjBase.PR_PRID:
215 strobjprid = obj.getData().str();
217 case COPSPrObjBase.PR_EPD:
218 reqSIs.put(strobjprid, obj.getData().str());
219 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
220 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
227 //** Here we must retrieve a decision depending on
228 //** the supplied ClientSIs
229 // reqSIs is a hashtable with the prid and epds
233 Hashtable removeDecs = new Hashtable();
234 Hashtable installDecs = new Hashtable();
235 _process.setClientData(this, reqSIs);
237 removeDecs = _process.getRemovePolicy(this);
238 installDecs = _process.getInstallPolicy(this);
240 //** We create the SOLICITED decision
242 _sender.sendDecision(removeDecs, installDecs);
249 * @param msg Report message from the PEP
250 * @throws COPSPdpException
252 protected void processReport(COPSReportMsg msg)
253 throws COPSPdpException {
255 //** Analyze the report
259 * <Report State> ::= <Common Header>
262 * *(<Named ClientSI>)
264 * <Named ClientSI: Report> ::= <[<GPERR>] *(<report>)>
265 * <report> ::= <ErrorPRID> <CPERR> *(<PRID><EPD>)
267 * Important, <Named ClientSI> is not parsed
270 // COPSHeader hdrmsg = msg.getHeader();
271 // COPSHandle handlemsg = msg.getClientHandle();
273 // WriteBinaryDump("COPSReportMessage", msg.getData().getData());
275 COPSReportType rtypemsg = msg.getReport();
278 Vector clientSIs = msg.getClientSI();
279 COPSClientSI myclientSI = (COPSClientSI) msg.getClientSI().elementAt(0);
280 byte[] data = Arrays.copyOfRange(myclientSI.getData().getData(), 0, myclientSI.getData().getData().length );
282 // PCMMUtils.WriteBinaryDump("COPSReportClientSI", data);
283 logger.info("PCMMGateReq Parse Gate Message");
284 PCMMGateReq gateMsg = new PCMMGateReq(data);
286 // TODO - Determine why this Map is being populated but never used???
287 Map<String, String> repSIs = new HashMap<>();
288 String strobjprid = "";
289 for (Enumeration e = clientSIs.elements() ; e.hasMoreElements() ;) {
290 COPSClientSI clientSI = (COPSClientSI) e.nextElement();
292 COPSPrObjBase obj = new COPSPrObjBase(clientSI.getData().getData());
293 switch (obj.getSNum()) {
294 case COPSPrObjBase.PR_PRID:
295 logger.info("COPSPrObjBase.PR_PRID");
296 strobjprid = obj.getData().str();
298 case COPSPrObjBase.PR_EPD:
299 logger.info("COPSPrObjBase.PR_EPD");
300 repSIs.put(strobjprid, obj.getData().str());
301 // COPSDebug.out(getClass().getName(),"PRID: " + strobjprid);
302 // COPSDebug.out(getClass().getName(),"EPD: " + obj.getData().str());
305 logger.error("Object s-num: " + obj.getSNum() + "stype " + obj.getSType());
306 logger.error("PRID: " + strobjprid);
307 logger.error("EPD: " + obj.getData().str());
312 logger.info("rtypemsg process");
313 //** Here we must act in accordance with
314 //** the report received
315 if (rtypemsg.getReportType().equals(ReportType.SUCCESS)) {
316 logger.info("rtypemsg success");
318 if (_process != null)
319 _process.successReport(this, gateMsg);
321 if ( gateMsg.getTransactionID().getGateCommandType() == ITransactionID.GateDeleteAck ) {
322 logger.info("GateDeleteAck: GateID = " + gateMsg.getGateID().getGateID());
323 if (gateMsg.getGateID().getGateID() == PCMMGlobalConfig.getGateID1())
324 PCMMGlobalConfig.setGateID1(0);
325 if (gateMsg.getGateID().getGateID() == PCMMGlobalConfig.getGateID2())
326 PCMMGlobalConfig.setGateID2(0);
329 if ( gateMsg.getTransactionID().getGateCommandType() == ITransactionID.GateSetAck ) {
330 logger.info("GateSetAck: GateID = " + gateMsg.getGateID().getGateID());
331 if (0 == PCMMGlobalConfig.getGateID1())
332 PCMMGlobalConfig.setGateID1(gateMsg.getGateID().getGateID());
333 if (0 == PCMMGlobalConfig.getGateID2())
334 PCMMGlobalConfig.setGateID2(gateMsg.getGateID().getGateID());
337 } else if (rtypemsg.getReportType().equals(ReportType.FAILURE)) {
338 logger.info("rtypemsg failure");
340 if (_process != null)
341 _process.failReport(this, gateMsg);
343 logger.info("Gate message error - " + gateMsg.getError().toString());
344 } else if (rtypemsg.getReportType().equals(ReportType.ACCOUNTING)) {
345 logger.info("rtypemsg account");
347 if (_process != null)
348 _process.acctReport(this, gateMsg);
350 logger.info("Out processReport");
354 * Called when connection is closed
355 * @param error Reason
356 * @throws COPSPdpException
358 protected void processClosedConnection(COPSError error)
359 throws COPSPdpException {
360 if (_process != null)
361 _process.notifyClosedConnection(this, error);
367 * Called when no keep-alive is received
368 * @throws COPSPdpException
370 protected void processNoKAConnection()
371 throws COPSPdpException {
372 if (_process != null)
373 _process.notifyNoKAliveReceived(this);
379 * Deletes the request state
380 * @throws COPSPdpException
382 protected void finalizeRequestState()
383 throws COPSPdpException {
384 _sender.sendDeleteRequestState();
389 * Asks for a COPS sync
390 * @throws COPSPdpException
392 protected void syncRequestState()
393 throws COPSPdpException {
394 _sender.sendSyncRequestState();
399 * Opens a new request state
400 * @throws COPSPdpException
402 protected void openNewRequestState()
403 throws COPSPdpException {
404 _sender.sendOpenNewRequestState();
409 * Processes a COPS delete message
410 * @param dMsg <tt>COPSDeleteMsg</tt> received from the PEP
411 * @throws COPSPdpException
413 protected void processDeleteRequestState(COPSDeleteMsg dMsg)
414 throws COPSPdpException {
415 if (_process != null)
416 _process.closeRequestState(this);