Merge changes If0630105,I9d2d5e61,I1cea2a32,Icc05b6a7,Ic57eb4f8, ...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpep / COPSPepAgent.java
index 343d40ca9bce0b4a43d3187870b4ac2f1da648c5..e7ab809d9260249aec2701409b8f13f2eb7d9117 100644 (file)
-/*\r
- * Copyright (c) 2004 University of Murcia.  All rights reserved.\r
- * --------------------------------------------------------------\r
- * For more information, please see <http://www.umu.euro6ix.org/>.\r
- */\r
-\r
-package org.umu.cops.prpep;\r
-\r
-import java.io.IOException;\r
-import java.net.InetAddress;\r
-import java.net.Socket;\r
-import java.net.UnknownHostException;\r
-import java.util.Hashtable;\r
-\r
-import org.umu.cops.stack.COPSAcctTimer;\r
-import org.umu.cops.stack.COPSClientAcceptMsg;\r
-import org.umu.cops.stack.COPSClientCloseMsg;\r
-import org.umu.cops.stack.COPSClientOpenMsg;\r
-import org.umu.cops.stack.COPSData;\r
-import org.umu.cops.stack.COPSError;\r
-import org.umu.cops.stack.COPSException;\r
-import org.umu.cops.stack.COPSHeader;\r
-import org.umu.cops.stack.COPSKATimer;\r
-import org.umu.cops.stack.COPSMsg;\r
-import org.umu.cops.stack.COPSPepId;\r
-import org.umu.cops.stack.COPSTransceiver;\r
-\r
-/**\r
- * This is a provisioning COPS PEP. Responsible for making\r
- * connection to the PDP and maintaining it\r
- */\r
-public class COPSPepAgent {\r
-\r
-    /**\r
-        PEP's Identifier\r
-     */\r
-    private String _pepID;\r
-\r
-    /**\r
-        PEP's client-type\r
-     */\r
-    private short _clientType;\r
-\r
-    /**\r
-        PDP host name\r
-     */\r
-    private String _psHost;\r
-\r
-    /**\r
-        PDP port\r
-     */\r
-    private int _psPort;\r
-\r
-    /**\r
-        PEP-PDP connection manager\r
-     */\r
-    private COPSPepConnection _conn;\r
-\r
-    /**\r
-        COPS error returned by PDP\r
-     */\r
-    private COPSError _error;\r
-\r
-    /**\r
-     * Creates a PEP agent\r
-     * @param    pepID              PEP-ID\r
-     * @param    clientType         Client-type\r
-     */\r
-    public COPSPepAgent(String pepID, short clientType) {\r
-        _pepID = pepID;\r
-        _clientType = clientType;\r
-    }\r
-\r
-    /**\r
-     * Creates a PEP agent with a PEP-ID equal to "noname"\r
-     * @param    clientType         Client-type\r
-     */\r
-    public COPSPepAgent(short clientType) {\r
-\r
-        // PEPId\r
-        try {\r
-            _pepID = InetAddress.getLocalHost().getHostName();\r
-        } catch (Exception e) {\r
-            _pepID = "noname";\r
-        }\r
-\r
-        _clientType = clientType;\r
-    }\r
-\r
-    /**\r
-     * Gets the identifier of the PEP\r
-     * @return  PEP-ID\r
-     */\r
-    public String getPepID() {\r
-        return _pepID;\r
-    }\r
-\r
-    /**\r
-     * Gets the COPS client-type\r
-     * @return  PEP's client-type\r
-     */\r
-    public short getClientType() {\r
-        return _clientType;\r
-    }\r
-\r
-    /**\r
-     * Gets PDP host name\r
-     * @return  PDP host name\r
-     */\r
-    public String getPDPName() {\r
-        return _psHost;\r
-    }\r
-\r
-    /**\r
-     * Gets the port of the PDP\r
-     * @return  PDP port\r
-     */\r
-    public int getPDPPort() {\r
-        return _psPort;\r
-    }\r
-\r
-    /**\r
-     * Connects to a PDP\r
-     * @param    psHost              PDP host name\r
-     * @param    psPort              PDP port\r
-     * @return   <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise\r
-     * @throws   java.net.UnknownHostException\r
-     * @throws   java.io.IOException\r
-     * @throws   COPSException\r
-     * @throws   COPSPepException\r
-     */\r
-    public boolean connect(String psHost, int psPort)\r
-    throws UnknownHostException, IOException, COPSException, COPSPepException {\r
-\r
-        // COPSDebug.out(getClass().getName(), "Thread ( " + _pepID + ") - Connecting to PDP");\r
-        _psHost = psHost;\r
-        _psPort = psPort;\r
-\r
-        // Check whether it already exists\r
-        if (_conn == null)\r
-            _conn = processConnection(psHost,psPort);\r
-        else {\r
-            // Check if it's closed\r
-            if (_conn.isClosed()) {\r
-                _conn = processConnection(psHost,psPort);\r
-            } else {\r
-                disconnect(null);\r
-                _conn = processConnection(psHost,psPort);\r
-            }\r
-        }\r
-\r
-        return (_conn != null);\r
-    }\r
-\r
-    /**\r
-     * Gets the connection manager\r
-     * @return  PEP-PDP connection manager object\r
-     */\r
-    public COPSPepConnection getConnection () {\r
-        return (_conn);\r
-    }\r
-\r
-    /**\r
-     * Gets the COPS error returned by the PDP\r
-     * @return   <tt>COPSError</tt> returned by PDP\r
-     */\r
-    public COPSError getConnectionError()   {\r
-        return _error;\r
-    }\r
-\r
-    /**\r
-     * Disconnects from the PDP\r
-     * @param error Reason\r
-     * @throws COPSException\r
-     * @throws IOException\r
-     */\r
-    public void disconnect(COPSError error)\r
-    throws COPSException, IOException {\r
-\r
-        COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, _clientType);\r
-        COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();\r
-        closeMsg.add(cHdr);\r
-        if (error != null)\r
-            closeMsg.add(error);\r
-\r
-        closeMsg.writeData(_conn.getSocket());\r
-        _conn.close();\r
-        _conn = null;\r
-    }\r
-\r
-    /**\r
-     * Adds a request state to the connection manager.\r
-     * @return  The newly created connection manager\r
-     * @throws COPSPepException\r
-     * @throws COPSException\r
-     */\r
-    public COPSPepReqStateMan addRequestState (String handle, COPSPepDataProcess process)\r
-    throws COPSPepException, COPSException {\r
-        if (_conn != null) {\r
-            return _conn.addRequestState(handle, process);\r
-        }\r
-        return null;\r
-    }\r
-\r
-\r
-    /**\r
-     * Queries the connection manager to delete a request state\r
-     * @param man   Request state manager\r
-     * @throws COPSPepException\r
-     * @throws COPSException\r
-     */\r
-    public void deleteRequestState (COPSPepReqStateMan man)\r
-    throws COPSPepException, COPSException {\r
-        if (_conn != null)\r
-            _conn.deleteRequestState(man);\r
-    }\r
-\r
-    /**\r
-     * Gets all the request state managers\r
-     * @return  A <tt>Hashtable</tt> holding all active request state managers\r
-     */\r
-    public Hashtable getReqStateMans() {\r
-        if (_conn != null)\r
-            return _conn.getReqStateMans();\r
-        return null;\r
-    }\r
-\r
-    /**\r
-     * Establish connection to PDP's IP address\r
-     *\r
-     * <Client-Open> ::= <Common Header>\r
-     *                  <PEPID>\r
-     *                  [<ClientSI>]\r
-     *                  [<LastPDPAddr>]\r
-     *                  [<Integrity>]\r
-     *\r
-     * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]\r
-     *\r
-     * <Client-Accept> ::= <Common Header>\r
-     *                      <KA Timer>\r
-     *                      [<ACCT Timer>]\r
-     *                      [<Integrity>]\r
-     *\r
-     * Not send [<Integrity>]\r
-     *\r
-     * <Client-Close> ::= <Common Header>\r
-     *                      <Error>\r
-     *                      [<PDPRedirAddr>]\r
-     *                      [<Integrity>]\r
-     *\r
-     * Not send [<PDPRedirAddr>], [<Integrity>]\r
-     *\r
-     * @throws   UnknownHostException\r
-     * @throws   IOException\r
-     * @throws   COPSException\r
-     * @throws   COPSPepException\r
-     *\r
-     */\r
-    private COPSPepConnection processConnection(String psHost, int psPort)\r
-    throws UnknownHostException, IOException, COPSException, COPSPepException {\r
-        // Build OPN\r
-        COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, _clientType);\r
-\r
-        COPSPepId pepId = new COPSPepId();\r
-        COPSData d = new COPSData(_pepID);\r
-        pepId.setData(d);\r
-\r
-        COPSClientOpenMsg msg = new COPSClientOpenMsg();\r
-        msg.add(hdr);\r
-        msg.add(pepId);\r
-\r
-        // Create Socket and send OPN\r
-        InetAddress addr = InetAddress.getByName(psHost);\r
-        Socket socket = new Socket(addr,psPort);\r
-        msg.writeData(socket);\r
-\r
-        // Receive the response\r
-        COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);\r
-\r
-        if (recvmsg.getHeader().isAClientAccept()) {\r
-            COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;\r
-\r
-            // Support\r
-            if (cMsg.getIntegrity() != null) {\r
-                throw new COPSPepException("Unsupported object (Integrity)");\r
-            }\r
-\r
-            // Mandatory KATimer\r
-            COPSKATimer kt = cMsg.getKATimer();\r
-            if (kt == null)\r
-                throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");\r
-            short _kaTimeVal = kt.getTimerVal();\r
-\r
-            // ACTimer\r
-            COPSAcctTimer at = cMsg.getAcctTimer();\r
-            short _acctTimer = 0;\r
-            if (at != null)\r
-                _acctTimer = at.getTimerVal();\r
-\r
-            // Create the connection manager\r
-            COPSPepConnection conn = new COPSPepConnection(_clientType, socket);\r
-            conn.setKaTimer(_kaTimeVal);\r
-            conn.setAcctTimer(_acctTimer);\r
-            new Thread(conn).start();\r
-\r
-            return conn;\r
-        } else if (recvmsg.getHeader().isAClientClose()) {\r
-            COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;\r
-            _error = cMsg.getError();\r
-            socket.close();\r
-            return null;\r
-        } else { // messages of other types are not expected\r
-            throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());\r
-        }\r
-    }\r
-}\r
-\r
-\r
-\r
+/*
+ * Copyright (c) 2004 University of Murcia.  All rights reserved.
+ * --------------------------------------------------------------
+ * For more information, please see <http://www.umu.euro6ix.org/>.
+ */
+
+package org.umu.cops.prpep;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.umu.cops.stack.*;
+import org.umu.cops.stack.COPSHeader.OPCode;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+/**
+ * This is a provisioning COPS PEP. Responsible for making
+ * connection to the PDP and maintaining it
+ */
+public class COPSPepAgent {
+
+    private final static Logger logger = LoggerFactory.getLogger(COPSPepAgent.class);
+
+    /**
+        PEP's Identifier
+     */
+    private String _pepID;
+
+    /**
+        PEP's client-type
+     */
+    private short _clientType;
+
+    /**
+        PDP host name
+     */
+    private String _psHost;
+
+    /**
+        PDP port
+     */
+    private int _psPort;
+
+    /**
+        PEP-PDP connection manager
+     */
+    private COPSPepConnection _conn;
+
+    /**
+        COPS error returned by PDP
+     */
+    private COPSError _error;
+
+    /**
+     * Creates a PEP agent
+     * @param    pepID              PEP-ID
+     * @param    clientType         Client-type
+     */
+    public COPSPepAgent(final String pepID, final short clientType) {
+        _pepID = pepID;
+        _clientType = clientType;
+    }
+
+    /**
+     * Creates a PEP agent with a PEP-ID equal to "noname"
+     * @param    clientType         Client-type
+     */
+    public COPSPepAgent(final short clientType) {
+
+        // PEPId
+        try {
+            _pepID = InetAddress.getLocalHost().getHostName();
+        } catch (Exception e) {
+            _pepID = "noname";
+        }
+
+        _clientType = clientType;
+    }
+
+    /**
+     * Gets the identifier of the PEP
+     * @return  PEP-ID
+     */
+    public String getPepID() {
+        return _pepID;
+    }
+
+    /**
+     * Gets the COPS client-type
+     * @return  PEP's client-type
+     */
+    public short getClientType() {
+        return _clientType;
+    }
+
+    /**
+     * Gets PDP host name
+     * @return  PDP host name
+     */
+    public String getPDPName() {
+        return _psHost;
+    }
+
+    /**
+     * Gets the port of the PDP
+     * @return  PDP port
+     */
+    public int getPDPPort() {
+        return _psPort;
+    }
+
+    /**
+     * Connects to a PDP
+     * @param    psHost              PDP host name
+     * @param    psPort              PDP port
+     * @return   <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise
+     * @throws   java.io.IOException
+     * @throws   COPSException
+     * @throws   COPSPepException
+     */
+    public boolean connect(String psHost, int psPort) throws IOException, COPSException {
+        logger.info("Thread ( " + _pepID + ") - Connecting to PDP");
+        _psHost = psHost;
+        _psPort = psPort;
+
+        // Check whether it already exists
+        if (_conn == null)
+            _conn = processConnection(psHost,psPort);
+        else {
+            // Check if it's closed
+            if (_conn.isClosed()) {
+                _conn = processConnection(psHost,psPort);
+            } else {
+                disconnect(null);
+                _conn = processConnection(psHost,psPort);
+            }
+        }
+
+        return (_conn != null);
+    }
+
+    /**
+     * Gets the connection manager
+     * @return  PEP-PDP connection manager object
+     */
+    public COPSPepConnection getConnection () {
+        return (_conn);
+    }
+
+    /**
+     * Gets the COPS error returned by the PDP
+     * @return   <tt>COPSError</tt> returned by PDP
+     */
+    public COPSError getConnectionError()   {
+        return _error;
+    }
+
+    /**
+     * Disconnects from the PDP
+     * @param error Reason
+     * @throws COPSException
+     * @throws IOException
+     */
+    public void disconnect(final COPSError error) throws COPSException, IOException {
+        final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(_clientType, error, null, null);
+        closeMsg.writeData(_conn.getSocket());
+        _conn.close();
+    }
+
+    /**
+     * Adds a request state to the connection manager.
+     * @return  The newly created connection manager
+     * @throws COPSPepException
+     * @throws COPSException
+     */
+    public COPSPepReqStateMan addRequestState(final COPSHandle handle, final COPSPepDataProcess process)
+            throws COPSException {
+        if (_conn != null) {
+            return _conn.addRequestState(handle, process);
+        }
+        return null;
+    }
+
+
+    /**
+     * Queries the connection manager to delete a request state
+     * @param man   Request state manager
+     * @throws COPSPepException
+     * @throws COPSException
+     */
+    public void deleteRequestState(final COPSPepReqStateMan man) throws COPSException {
+        if (_conn != null)
+            _conn.deleteRequestState(man);
+    }
+
+    /**
+     * Establish connection to PDP's IP address
+     *
+     * <Client-Open> ::= <Common Header>
+     *                  <PEPID>
+     *                  [<ClientSI>]
+     *                  [<LastPDPAddr>]
+     *                  [<Integrity>]
+     *
+     * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
+     *
+     * <Client-Accept> ::= <Common Header>
+     *                      <KA Timer>
+     *                      [<ACCT Timer>]
+     *                      [<Integrity>]
+     *
+     * Not send [<Integrity>]
+     *
+     * <Client-Close> ::= <Common Header>
+     *                      <Error>
+     *                      [<PDPRedirAddr>]
+     *                      [<Integrity>]
+     *
+     * Not send [<PDPRedirAddr>], [<Integrity>]
+     *
+     * @throws   UnknownHostException
+     * @throws   IOException
+     * @throws   COPSException
+     * @throws   COPSPepException
+     *
+     */
+    private COPSPepConnection processConnection(final String psHost, final int psPort)
+            throws IOException, COPSException {
+        // Build OPN
+        final COPSClientOpenMsg msg = new COPSClientOpenMsg(_clientType, new COPSPepId(new COPSData(_pepID)),
+                null, null, null);
+
+        // Create Socket and send OPN
+        final InetAddress addr = InetAddress.getByName(psHost);
+        final Socket socket = new Socket(addr,psPort);
+        msg.writeData(socket);
+
+        // Receive the response
+        final COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
+
+        if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
+            final COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
+
+            // Support
+            if (cMsg.getIntegrity() != null) {
+                throw new COPSPepException("Unsupported object (Integrity)");
+            }
+
+            // Mandatory KATimer
+            final COPSKATimer kt = cMsg.getKATimer();
+            if (kt == null)
+                throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");
+            short _kaTimeVal = kt.getTimerVal();
+
+            // ACTimer
+            final COPSAcctTimer at = cMsg.getAcctTimer();
+            short _acctTimer = 0;
+            if (at != null)
+                _acctTimer = at.getTimerVal();
+
+            // Create the connection manager
+            final COPSPepConnection conn = new COPSPepConnection(_clientType, socket);
+            conn.setKaTimer(_kaTimeVal);
+            conn.setAcctTimer(_acctTimer);
+            new Thread(conn).start();
+
+            return conn;
+        } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
+            final COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
+            _error = cMsg.getError();
+            socket.close();
+            return null;
+        } else { // messages of other types are not expected
+            throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
+        }
+    }
+}
+
+
+