-package org.umu.cops.ospep;\r
-\r
-import java.net.Socket;\r
-import java.util.Vector;\r
-\r
-import org.umu.cops.stack.COPSData;\r
-import org.umu.cops.stack.COPSDecisionMsg;\r
-import org.umu.cops.stack.COPSError;\r
-import org.umu.cops.stack.COPSHandle;\r
-import org.umu.cops.stack.COPSSyncStateMsg;\r
-\r
-/**\r
- * State manager class for outsourcing requests, at the PEP side.\r
- */\r
-public class COPSPepOSReqStateMan {\r
- /**\r
- * Request State created\r
- */\r
- public final static short ST_CREATE = 1;\r
- /**\r
- * Request sent\r
- */\r
- public final static short ST_INIT = 2;\r
- /**\r
- * Decisions received\r
- */\r
- public final static short ST_DECS = 3;\r
- /**\r
- * Report sent\r
- */\r
- public final static short ST_REPORT = 4;\r
- /**\r
- * Request State finalized\r
- */\r
- public final static short ST_FINAL = 5;\r
- /**\r
- * New Request State solicited\r
- */\r
- public final static short ST_NEW = 6;\r
- /**\r
- * Delete Request State solicited\r
- */\r
- public final static short ST_DEL = 7;\r
- /**\r
- * SYNC request received\r
- */\r
- public final static short ST_SYNC = 8;\r
- /**\r
- * Sync completed\r
- */\r
- public final static short ST_SYNCALL = 9;\r
- /**\r
- * Close connection received\r
- */\r
- public final static short ST_CCONN = 10;\r
- /**\r
- * Keep-alive timeout\r
- */\r
- public final static short ST_NOKA = 11;\r
- /**\r
- * Accounting timeout\r
- */\r
- public final static short ST_ACCT = 12;\r
-\r
- /**\r
- * COPS client-type that identifies the policy client\r
- */\r
- protected short _clientType;\r
-\r
- /**\r
- * COPS client handle used to uniquely identify a particular\r
- * PEP's request for a client-type\r
- */\r
- protected COPSHandle _handle;\r
-\r
- /**\r
- Object for performing policy data processing\r
- */\r
- protected COPSPepOSDataProcess _process;\r
-\r
- /**\r
- * ClientSI data from signaling.\r
- */\r
- protected Vector _clientSIs;\r
-\r
- /**\r
- * Current state of the request being managed\r
- */\r
- protected short _status;\r
-\r
- /**\r
- COPS message transceiver used to send COPS messages\r
- */\r
- protected COPSPepOSMsgSender _sender;\r
-\r
- /**\r
- * Sync state\r
- */\r
- protected boolean _syncState;\r
-\r
- /**\r
- * Creates a state request manager\r
- * @param clientType Client-type\r
- * @param clientHandle Client's <tt>COPSHandle</tt>\r
- */\r
- public COPSPepOSReqStateMan(short clientType, String clientHandle) {\r
- // COPS Handle\r
- _handle = new COPSHandle();\r
- COPSData id = new COPSData(clientHandle);\r
- _handle.setId(id);\r
- // client-type\r
- _clientType = clientType;\r
- _syncState = true;\r
- _status = ST_CREATE;\r
- _clientSIs = null;\r
- }\r
-\r
- /**\r
- * Gets the client handle\r
- * @return Client's <tt>COPSHandle</tt>\r
- */\r
- public COPSHandle getClientHandle() {\r
- return _handle;\r
- }\r
-\r
- /**\r
- * Sets the client SI data.\r
- * @param someClientSIs Client SI data built by the event listener\r
- */\r
- public void setClientSI(Vector someClientSIs) {\r
- _clientSIs = someClientSIs;\r
- }\r
-\r
- /**\r
- * Gets the client-type\r
- * @return Client-type value\r
- */\r
- public short getClientType() {\r
- return _clientType;\r
- }\r
-\r
- /**\r
- * Gets the request status\r
- * @return Request status value\r
- */\r
- public short getStatus() {\r
- return _status;\r
- }\r
-\r
- /**\r
- * Gets the policy data processing object\r
- *\r
- * @return Policy data processing object\r
- *\r
- */\r
- public COPSPepOSDataProcess getDataProcess() {\r
- return _process;\r
- }\r
-\r
- /**\r
- * Sets the policy data processing object\r
- *\r
- * @param process Policy data processing object\r
- *\r
- */\r
- public void setDataProcess(COPSPepOSDataProcess process) {\r
- _process = process;\r
- }\r
-\r
- /**\r
- * Initializes a new request state over a socket\r
- * @param sock Socket to the PDP\r
- * @throws COPSPepException\r
- */\r
- protected void initRequestState(Socket sock) throws COPSPepException {\r
- // Inits an object for sending COPS messages to the PDP\r
- _sender = new COPSPepOSMsgSender(_clientType, _handle, sock);\r
-\r
- // If an object exists for retrieving the PEP features,\r
- // use it for retrieving them.\r
- /* Hashtable clientSIs;\r
- if (_process != null)\r
- clientSIs = _process.getClientData(this);\r
- else\r
- clientSIs = null;*/\r
-\r
- // Semd the request\r
- _sender.sendRequest(_clientSIs);\r
-\r
- // Initial state\r
- _status = ST_INIT;\r
- }\r
-\r
- /**\r
- * Deletes the request state\r
- * @throws COPSPepException\r
- */\r
- protected void finalizeRequestState() throws COPSPepException {\r
- _sender.sendDeleteRequest();\r
- _status = ST_FINAL;\r
- }\r
-\r
- /**\r
- * Processes the decision message\r
- * @param dMsg Decision message from the PDP\r
- * @throws COPSPepException\r
- */\r
- protected void processDecision(COPSDecisionMsg dMsg) throws COPSPepException {\r
- // COPSDebug.out(getClass().getName(), "ClientId:" + getClientHandle().getId().str());\r
-\r
- //Hashtable decisionsPerContext = dMsg.getDecisions();\r
-\r
- //** Applies decisions to the configuration\r
- //_process.setDecisions(this, removeDecs, installDecs, errorDecs);\r
- // second param changed to dMsg so that the data processor\r
- // can check the 'solicited' flag\r
- boolean isFailReport = _process.setDecisions(this, dMsg /*decisionsPerContext*/);\r
- _status = ST_DECS;\r
-\r
- if (isFailReport) {\r
- // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");\r
- _sender.sendFailReport(_process.getReportData(this));\r
- } else {\r
- // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");\r
- _sender.sendSuccessReport(_process.getReportData(this));\r
- }\r
- _status = ST_REPORT;\r
-\r
- if (!_syncState) {\r
- _sender.sendSyncComplete();\r
- _syncState = true;\r
- _status = ST_SYNCALL;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Processes a COPS delete message\r
- * @param dMsg <tt>COPSDeleteMsg</tt> received from the PDP\r
- * @throws COPSPepException\r
- */\r
- protected void processDeleteRequestState(COPSDecisionMsg dMsg) throws COPSPepException {\r
- if (_process != null)\r
- _process.closeRequestState(this);\r
-\r
- _status = ST_DEL;\r
- }\r
-\r
- /**\r
- * Processes the message SycnStateRequest.\r
- * The message SycnStateRequest indicates that the remote PDP\r
- * wishes the client (which appears in the common header)\r
- * to re-send its state.\r
- *\r
- * @param ssMsg The sync request from the PDP\r
- *\r
- * @throws COPSPepException\r
- *\r
- */\r
- protected void processSyncStateRequest(COPSSyncStateMsg ssMsg) throws COPSPepException {\r
- _syncState = false;\r
- // If an object exists for retrieving the PEP features,\r
- // use it for retrieving them.\r
-\r
- // Send the request\r
- _sender.sendRequest(_clientSIs);\r
-\r
- _status = ST_SYNC;\r
- }\r
-\r
- /**\r
- * Called when connection is closed\r
- * @param error Reason\r
- * @throws COPSPepException\r
- */\r
- protected void processClosedConnection(COPSError error) throws COPSPepException {\r
- if (_process != null)\r
- _process.notifyClosedConnection(this, error);\r
-\r
- _status = ST_CCONN;\r
- }\r
-\r
- /**\r
- * Called when no keep-alive is received\r
- * @throws COPSPepException\r
- */\r
- protected void processNoKAConnection() throws COPSPepException {\r
- if (_process != null)\r
- _process.notifyNoKAliveReceived(this);\r
-\r
- _status = ST_NOKA;\r
- }\r
-\r
- /**\r
- * Processes the accounting report\r
- * @throws COPSPepException\r
- */\r
- protected void processAcctReport() throws COPSPepException {\r
- Vector report = new Vector();\r
-\r
- if (_process != null)\r
- report = _process.getAcctData(this);\r
-\r
- _sender.sendAcctReport(report);\r
-\r
- _status = ST_ACCT;\r
- }\r
-}\r
+package org.umu.cops.ospep;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.umu.cops.prpep.COPSPepReqStateMan;
+import org.umu.cops.stack.*;
+
+import java.net.Socket;
+import java.util.*;
+
+/**
+ * State manager class for outsourcing requests, at the PEP side.
+ */
+public class COPSPepOSReqStateMan extends COPSPepReqStateMan {
+
+ private final static Logger logger = LoggerFactory.getLogger(COPSPepOSReqStateMan.class);
+
+ /**
+ * ClientSI data from signaling.
+ */
+ protected final Set<COPSClientSI> _clientSIs;
+
+ /**
+ Object for performing policy data processing
+ */
+ protected final COPSPepOSDataProcess _process;
+
+ /**
+ COPS message transceiver used to send COPS messages
+ */
+ protected transient COPSPepOSMsgSender _sender;
+
+ /**
+ * Sync state
+ */
+ protected transient boolean _syncState;
+
+ /**
+ * Creates a state request manager
+ * @param clientType Client-type
+ * @param clientHandle Client's <tt>COPSHandle</tt>
+ */
+ public COPSPepOSReqStateMan(final short clientType, final COPSHandle clientHandle, final COPSPepOSDataProcess process,
+ final Collection<COPSClientSI> clientSIs) {
+ super(clientType, clientHandle, process);
+ this._process = process;
+ this._clientSIs = new HashSet<>(clientSIs);
+ _syncState = true;
+ }
+
+ @Override
+ protected void initRequestState(final Socket sock) throws COPSException {
+ // Inits an object for sending COPS messages to the PDP
+ _sender = new COPSPepOSMsgSender(_clientType, _handle, sock);
+
+ // If an object exists for retrieving the PEP features,
+ // use it for retrieving them.
+ /* Hashtable clientSIs;
+ if (_process != null)
+ clientSIs = _process.getClientData(this);
+ else
+ clientSIs = null;*/
+
+ // Semd the request
+ _sender.sendRequest(_clientSIs);
+
+ // Initial state
+ _status = Status.ST_INIT;
+ }
+
+ /**
+ * Deletes the request state
+ * @throws COPSPepException
+ */
+ protected void finalizeRequestState() throws COPSException {
+ _sender.sendDeleteRequest();
+ _status = Status.ST_FINAL;
+ }
+
+ /**
+ * Processes the decision message
+ * @param dMsg Decision message from the PDP
+ * @throws COPSPepException
+ */
+ protected void processDecision(final COPSDecisionMsg dMsg) throws COPSException {
+ //** Applies decisions to the configuration
+ //_process.setDecisions(this, removeDecs, installDecs, errorDecs);
+ // second param changed to dMsg so that the data processor
+ // can check the 'solicited' flag
+ final boolean isFailReport = _process.setDecisions(this, dMsg);
+ _status = Status.ST_DECS;
+
+ if (isFailReport) {
+ logger.info("Sending FAIL Report");
+ _sender.sendFailReport(_process.getReportData(this));
+ } else {
+ logger.info("Sending SUCCESS Report");
+ _sender.sendSuccessReport(_process.getReportData(this));
+ }
+ _status = Status.ST_REPORT;
+
+ if (!_syncState) {
+ _sender.sendSyncComplete();
+ _syncState = true;
+ _status = Status.ST_SYNCALL;
+ }
+ }
+
+
+ /**
+ * Processes a COPS delete message
+ * @param dMsg <tt>COPSDeleteMsg</tt> received from the PDP
+ * @throws COPSPepException
+ */
+ protected void processDeleteRequestState(final COPSDecisionMsg dMsg) throws COPSPepException {
+ if (_process != null)
+ _process.closeRequestState(this);
+
+ _status = Status.ST_DEL;
+ }
+
+ /**
+ * Processes the message SycnStateRequest.
+ * The message SycnStateRequest indicates that the remote PDP
+ * wishes the client (which appears in the common header)
+ * to re-send its state.
+ *
+ * @param ssMsg The sync request from the PDP
+ *
+ * @throws COPSPepException
+ *
+ */
+ protected void processSyncStateRequest(final COPSSyncStateMsg ssMsg) throws COPSException {
+ _syncState = false;
+ // If an object exists for retrieving the PEP features,
+ // use it for retrieving them.
+
+ // Send the request
+ _sender.sendRequest(_clientSIs);
+
+ _status = Status.ST_SYNC;
+ }
+
+ /**
+ * Called when connection is closed
+ * @param error Reason
+ * @throws COPSPepException
+ */
+ protected void processClosedConnection(final COPSError error) throws COPSPepException {
+ if (_process != null)
+ _process.notifyClosedConnection(this, error);
+
+ _status = Status.ST_CCONN;
+ }
+
+ /**
+ * Called when no keep-alive is received
+ * @throws COPSPepException
+ */
+ protected void processNoKAConnection() throws COPSPepException {
+ if (_process != null)
+ _process.notifyNoKAliveReceived(this);
+
+ _status = Status.ST_NOKA;
+ }
+
+ /**
+ * Processes the accounting report
+ * @throws COPSPepException
+ */
+ protected void processAcctReport() throws COPSPepException {
+ final List<COPSClientSI> report;
+ if (_process != null) report = new ArrayList<>(_process.getAcctData(this));
+ else report = new ArrayList<>();
+
+ _sender.sendAcctReport(report);
+
+ _status = Status.ST_ACCT;
+ }
+}