Merge "Copyright"
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospep / COPSPepOSMsgSender.java
index 2f8526ef820d0efb2a24d8b3d4ee80331be11d21..5ddeed14987f29902db68e061b2354d25dba57e8 100644 (file)
-package org.umu.cops.ospep;\r
-\r
-import java.io.IOException;\r
-import java.net.Socket;\r
-import java.util.Enumeration;\r
-import java.util.Vector;\r
-\r
-import org.umu.cops.stack.COPSClientSI;\r
-import org.umu.cops.stack.COPSContext;\r
-import org.umu.cops.stack.COPSDeleteMsg;\r
-import org.umu.cops.stack.COPSException;\r
-import org.umu.cops.stack.COPSHandle;\r
-import org.umu.cops.stack.COPSHeader;\r
-import org.umu.cops.stack.COPSReason;\r
-import org.umu.cops.stack.COPSReportMsg;\r
-import org.umu.cops.stack.COPSReportType;\r
-import org.umu.cops.stack.COPSReqMsg;\r
-import org.umu.cops.stack.COPSSyncStateMsg;\r
-\r
-/**\r
- * COPS message transceiver class for outsourcing connections at the PEP side.\r
- */\r
-public class COPSPepOSMsgSender {\r
-    /**\r
-     * Socket connection to PDP\r
-     */\r
-    protected Socket _sock;\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
-     * Creates a COPSPepMsgSender\r
-     *\r
-     * @param clientType        Client-type\r
-     * @param clientHandle      Client handle\r
-     * @param sock              Socket connected to the PDP\r
-     */\r
-    public COPSPepOSMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {\r
-        // COPS Handle\r
-        _handle = clientHandle;\r
-        _clientType = clientType;\r
-\r
-        _sock = sock;\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
-     * Gets the client-type\r
-     * @return  Client-type value\r
-     */\r
-    public short getClientType() {\r
-        return _clientType;\r
-    }\r
-\r
-    /**\r
-     * Sends a request to the PDP.\r
-     * The PEP establishes a request state client handle for which the\r
-     * remote PDP may maintain state.\r
-     * @param    clientSIs              Client data\r
-     * @throws   COPSPepException\r
-     */\r
-    public void sendRequest(Vector clientSIs) throws COPSPepException {\r
-        // Create COPS Message\r
-        COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_REQ, _clientType);\r
-\r
-        COPSContext cntxt = new COPSContext(COPSContext.CONFIG , (short) 0);\r
-\r
-        COPSHandle handle = _handle;\r
-\r
-        COPSReqMsg msg = new COPSReqMsg();\r
-        try {\r
-            msg.add(hdr) ;\r
-            msg.add(handle) ;\r
-            msg.add(cntxt) ;\r
-\r
-            Enumeration clientSIEnum = clientSIs.elements();\r
-            while (clientSIEnum.hasMoreElements())\r
-                msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
-        } catch (COPSException e) {\r
-            throw new COPSPepException("Error making Request Msg, reason: " + e.getMessage());\r
-        }\r
-\r
-        // Send message\r
-        try {\r
-            msg.writeData(_sock);\r
-        } catch (IOException e) {\r
-            throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Sends a failure report to the PDP. This report message notifies the PDP\r
-     * of failure when carrying out the PDP's decision, or when reporting\r
-     *  an accounting related state change.\r
-     * @param clientSIs Report data\r
-     * @throws   COPSPepException\r
-     */\r
-    public void sendFailReport(Vector clientSIs) throws COPSPepException {\r
-        COPSReportMsg msg = new COPSReportMsg();\r
-        // Report FAIL\r
-        try {\r
-            COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);\r
-            COPSHandle hnd = _handle;\r
-\r
-            COPSReportType report = new COPSReportType(COPSReportType.FAILURE);\r
-\r
-            msg.add(hdr);\r
-            msg.add(hnd);\r
-            msg.add(report);\r
-\r
-            Enumeration clientSIEnum = clientSIs.elements();\r
-            while (clientSIEnum.hasMoreElements())\r
-                msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
-        } catch (COPSException ex) {\r
-            throw new COPSPepException("Error making Msg");\r
-        }\r
-\r
-        try {\r
-            msg.writeData(_sock);\r
-        } catch (IOException e) {\r
-            throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Sends a success report to the PDP. This report message notifies the PDP\r
-     * of success when carrying out the PDP's decision, or when reporting\r
-     *  an accounting related state change.\r
-     * @param   clientSIs   Report data\r
-     * @throws  COPSPepException\r
-     */\r
-    public void sendSuccessReport(Vector clientSIs) throws COPSPepException {\r
-        COPSReportMsg msg = new COPSReportMsg();\r
-        // Report SUCESS\r
-        try {\r
-            COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);\r
-            COPSHandle hnd = _handle;\r
-\r
-            COPSReportType report = new COPSReportType(COPSReportType.SUCCESS);\r
-\r
-            msg.add(hdr);\r
-            msg.add(hnd);\r
-            msg.add(report);\r
-\r
-            Enumeration clientSIEnum = clientSIs.elements();\r
-            while (clientSIEnum.hasMoreElements())\r
-                msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
-        } catch (COPSException ex) {\r
-            throw new COPSPepException("Error making Msg");\r
-        }\r
-\r
-        try {\r
-            msg.writeData(_sock);\r
-        } catch (IOException e) {\r
-            throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Sends an accounting report to the PDP\r
-     * @param clientSIs Report data\r
-     * @throws COPSPepException\r
-     */\r
-    public void sendAcctReport(Vector clientSIs) throws COPSPepException {\r
-        COPSReportMsg msg = new COPSReportMsg();\r
-        // Report SUCCESS\r
-        try {\r
-            COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);\r
-            COPSHandle hnd = _handle;\r
-\r
-            COPSReportType report = new COPSReportType(COPSReportType.ACCT);\r
-\r
-            msg.add(hdr);\r
-            msg.add(hnd);\r
-            msg.add(report);\r
-\r
-            Enumeration clientSIEnum = clientSIs.elements();\r
-            while (clientSIEnum.hasMoreElements())\r
-                msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
-        } catch (COPSException ex) {\r
-            throw new COPSPepException("Error making Msg");\r
-        }\r
-\r
-        try {\r
-            msg.writeData(_sock);\r
-        } catch (IOException e) {\r
-            throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Sends a sync-complete message to the PDP. This indicates the\r
-     * end of a synchronization requested by the PDP.\r
-     * @throws   COPSPepException\r
-     */\r
-    public void sendSyncComplete() throws COPSPepException {\r
-        // Common Header with the same ClientType as the request\r
-        COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSC, _clientType);\r
-\r
-        // Client Handle with the same clientHandle as the request\r
-        COPSHandle clienthandle = _handle;\r
-\r
-        COPSSyncStateMsg msg = new COPSSyncStateMsg();\r
-        try {\r
-            msg.add(hdr);\r
-            msg.add(clienthandle);\r
-        } catch (Exception e) {\r
-            throw new COPSPepException("Error making Msg");\r
-        }\r
-\r
-        try {\r
-            msg.writeData(_sock);\r
-        } catch (IOException e) {\r
-            throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Sends a delete request to the PDP.\r
-     * When sent from the PEP this message indicates to the remote PDP that\r
-     * the state identified by the client handle is no longer\r
-     * available/relevant.\r
-     * @throws   COPSPepException\r
-     */\r
-    public void sendDeleteRequest() throws COPSPepException {\r
-        COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DRQ, _clientType);\r
-        COPSHandle handle = _handle;\r
-\r
-        // *** TODO: use real reason codes\r
-        COPSReason reason = new COPSReason((short) 234, (short) 345);\r
-\r
-        COPSDeleteMsg msg = new COPSDeleteMsg();\r
-        try {\r
-            msg.add(hdr);\r
-            msg.add(handle);\r
-            msg.add(reason);\r
-            msg.writeData(_sock);\r
-        } catch (COPSException ex) {\r
-            throw new COPSPepException("Error making Msg");\r
-        } catch (IOException e) {\r
-            throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());\r
-        }\r
-    }\r
-\r
-}\r
+package org.umu.cops.ospep;
+
+import org.umu.cops.stack.*;
+import org.umu.cops.stack.COPSContext.RType;
+import org.umu.cops.stack.COPSReason.ReasonCode;
+import org.umu.cops.stack.COPSReportType.ReportType;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * COPS message transceiver class for outsourcing connections at the PEP side.
+ */
+public class COPSPepOSMsgSender {
+    /**
+     * Socket connection to PDP
+     */
+    protected Socket _sock;
+
+    /**
+     * COPS client-type that identifies the policy client
+     */
+    protected short _clientType;
+
+    /**
+     * COPS client handle used to uniquely identify a particular
+     * PEP's request for a client-type
+     */
+    protected COPSHandle _handle;
+
+    /**
+     * Creates a COPSPepMsgSender
+     *
+     * @param clientType        Client-type
+     * @param clientHandle      Client handle
+     * @param sock              Socket connected to the PDP
+     */
+    public COPSPepOSMsgSender (final short clientType, final COPSHandle clientHandle, final Socket sock) {
+        // COPS Handle
+        _handle = clientHandle;
+        _clientType = clientType;
+
+        _sock = sock;
+    }
+
+    /**
+     * Gets the client handle
+     * @return  Client's <tt>COPSHandle</tt>
+     */
+    public COPSHandle getClientHandle() {
+        return _handle;
+    }
+
+    /**
+     * Gets the client-type
+     * @return  Client-type value
+     */
+    public int getClientType() {
+        return _clientType;
+    }
+
+    /**
+     * Sends a request to the PDP.
+     * The PEP establishes a request state client handle for which the
+     * remote PDP may maintain state.
+     * @param    clientSIs              Client data
+     * @throws   COPSPepException
+     */
+    public void sendRequest(final Set<COPSClientSI> clientSIs) throws COPSPepException {
+        // Create COPS Message
+        final COPSReqMsg msg = new COPSReqMsg(_clientType, _handle, new COPSContext(RType.CONFIG, (short)0), null,
+                null, null, clientSIs, null);
+
+        // Send message
+        try {
+            msg.writeData(_sock);
+        } catch (IOException e) {
+            throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Sends a failure report to the PDP. This report message notifies the PDP
+     * of failure when carrying out the PDP's decision, or when reporting
+     *  an accounting related state change.
+     * @param clientSIs Report data
+     * @throws   COPSPepException
+     */
+    public void sendFailReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
+        sendReport(clientSIs, new COPSReportType(ReportType.FAILURE));
+    }
+
+    /**
+     * Sends a success report to the PDP. This report message notifies the PDP
+     * of success when carrying out the PDP's decision, or when reporting
+     *  an accounting related state change.
+     * @param   clientSIs   Report data
+     * @throws  COPSPepException
+     */
+    public void sendSuccessReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
+        sendReport(clientSIs, new COPSReportType(ReportType.SUCCESS));
+    }
+
+    /**
+     * Sends an accounting report to the PDP
+     * @param clientSIs Report data
+     * @throws COPSPepException
+     */
+    public void sendAcctReport(final List<COPSClientSI> clientSIs) throws COPSPepException {
+        sendReport(clientSIs, new COPSReportType(ReportType.ACCOUNTING));
+    }
+
+    private void sendReport(final List<COPSClientSI> clientSIs, final COPSReportType type) throws COPSPepException {
+        // Change back to old way if it is ultimately determined that a report may contain more than one COPSClientSI
+        final COPSReportMsg msg = new COPSReportMsg(_clientType, _handle, type, null, null);
+        try {
+            msg.writeData(_sock);
+        } catch (IOException e) {
+            throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Sends a sync-complete message to the PDP. This indicates the
+     * end of a synchronization requested by the PDP.
+     * @throws   COPSPepException
+     */
+    public void sendSyncComplete() throws COPSPepException {
+        // Common Header with the same ClientType as the request
+        // Client Handle with the same clientHandle as the request
+        final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
+        try {
+            msg.writeData(_sock);
+        } catch (IOException e) {
+            throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Sends a delete request to the PDP.
+     * When sent from the PEP this message indicates to the remote PDP that
+     * the state identified by the client handle is no longer
+     * available/relevant.
+     * @throws   COPSPepException
+     */
+    public void sendDeleteRequest() throws COPSPepException {
+        // *** TODO: use real reason codes
+        COPSReason reason = new COPSReason(ReasonCode.UNSPECIFIED, ReasonCode.NA);
+
+        final COPSDeleteMsg msg = new COPSDeleteMsg(_clientType, _handle, reason, null);
+        try {
+            msg.writeData(_sock);
+        } catch (IOException e) {
+            throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());
+        }
+    }
+
+}