Completed COPS Message refactoring. Was planning on one additional patch starting...
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / PCMMPdpAgent.java
index 3044b1b2e52f440d6c8878888a457a8353b2754e..ab0e415e61421592c224587171c7b98322e8c642 100644 (file)
@@ -7,19 +7,18 @@ package org.pcmm;
 import org.pcmm.objects.MMVersionInfo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.umu.cops.common.COPSDebug;
-import org.umu.cops.ospep.COPSPepException;
 import org.umu.cops.prpdp.COPSPdpAgent;
 import org.umu.cops.prpdp.COPSPdpException;
 import org.umu.cops.stack.*;
+import org.umu.cops.stack.COPSError.ErrorTypes;
+import org.umu.cops.stack.COPSHeader.ClientType;
+import org.umu.cops.stack.COPSHeader.OPCode;
 
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.Socket;
-import java.net.UnknownHostException;
-
-// import org.umu.cops.prpdp.COPSPdpDataProcess;
-
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Core PDP agent for provisioning
@@ -49,9 +48,14 @@ public class PCMMPdpAgent extends COPSPdpAgent {
      * Policy data processing object
      */
     private PCMMPdpDataProcess _process;
-    private MMVersionInfo _mminfo;
     private COPSHandle _handle;
-    private short _transactionID;
+//    private short _transactionID;
+
+    /**
+     * Temporary until can refactor PdpAgent classes
+     */
+    @Deprecated
+    private final Map<String, PCMMPdpConnection> _connectionMap;
 
     /**
      * Creates a PDP Agent
@@ -61,7 +65,7 @@ public class PCMMPdpAgent extends COPSPdpAgent {
      * @param process
      *            Object to perform policy data processing
      */
-    public PCMMPdpAgent(short clientType, PCMMPdpDataProcess process) {
+    public PCMMPdpAgent(ClientType clientType, PCMMPdpDataProcess process) {
         this(clientType, null, WELL_KNOWN_PDP_PORT, process);
     }
 
@@ -77,10 +81,11 @@ public class PCMMPdpAgent extends COPSPdpAgent {
      * @param process
      *            Object to perform policy data processing
      */
-    public PCMMPdpAgent(short clientType, String psHost, int psPort, PCMMPdpDataProcess process) {
+    public PCMMPdpAgent(ClientType clientType, String psHost, int psPort, PCMMPdpDataProcess process) {
         super(psPort, clientType, null);
         this._process = process;
         this.psHost = psHost;
+        this._connectionMap = new ConcurrentHashMap<>();
     }
 
     /**
@@ -102,11 +107,9 @@ public class PCMMPdpAgent extends COPSPdpAgent {
      * @throws java.net.UnknownHostException
      * @throws java.io.IOException
      * @throws COPSException
-     * @throws COPSPepException
+     * @throws COPSPdpException
      */
-    public boolean connect(String psHost, int psPort)
-    throws UnknownHostException, IOException, COPSException,
-                COPSPdpException {
+    public boolean connect(String psHost, int psPort) throws IOException, COPSException, COPSPdpException {
 
         this.psHost = psHost;
         this.psPort = psPort;
@@ -115,10 +118,10 @@ public class PCMMPdpAgent extends COPSPdpAgent {
         try {
             socket = new Socket(addr, psPort);
         } catch (IOException e) {
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);
+            logger.error("Error creating socket connection", e);
             return (false);
         }
-        COPSDebug.err(getClass().getName(), "PDP Socket Opened");
+        logger.info("PDP Socket Opened");
         // Loop through for Incoming messages
 
         // server infinite loop
@@ -127,32 +130,24 @@ public class PCMMPdpAgent extends COPSPdpAgent {
 
             // We're waiting for an message
             try {
-                COPSDebug.err(getClass().getName(),
-                              "PDP  COPSTransceiver.receiveMsg ");
+                logger.info("PDP  COPSTransceiver.receiveMsg");
                 COPSMsg msg = COPSTransceiver.receiveMsg(socket);
-                if (msg.getHeader().isAClientOpen()) {
-                    COPSDebug.err(getClass().getName(),
-                                  "PDP msg.getHeader().isAClientOpen");
+                if (msg.getHeader().getOpCode().equals(OPCode.OPN)) {
+                    logger.info("PDP msg.getHeader().isAClientOpen");
                     handleClientOpenMsg(socket, msg);
                 } else {
-                    // COPSDebug.err(getClass().getName(),
-                    // COPSDebug.ERROR_NOEXPECTEDMSG);
                     try {
                         socket.close();
                     } catch (Exception ex) {
+                        logger.error("Unexpected exception closing socket", ex);
                     }
-                    ;
                 }
             } catch (Exception e) { // COPSException, IOException
-                // COPSDebug.err(getClass().getName(),
-                // COPSDebug.ERROR_EXCEPTION,
-                // "(" + socket.getInetAddress() + ":" + socket.getPort() + ")",
-                // e);
                 try {
                     socket.close();
                 } catch (Exception ex) {
+                    logger.error("Unexpected exception closing socket", ex);
                 }
-                ;
                 return true;
             }
         }
@@ -169,24 +164,19 @@ public class PCMMPdpAgent extends COPSPdpAgent {
      * @throws COPSException
      * @throws IOException
      */
-    private void handleClientOpenMsg(Socket conn, COPSMsg msg)
-    throws COPSException, IOException {
-        COPSClientOpenMsg cMsg = (COPSClientOpenMsg) msg;
-        COPSPepId pepId = cMsg.getPepId();
+    private void handleClientOpenMsg(final Socket conn, final COPSMsg msg) throws COPSException, IOException {
+        final COPSClientOpenMsg cMsg = (COPSClientOpenMsg) msg;
+        final COPSPepId pepId = cMsg.getPepId();
 
         // Validate Client Type
         if (msg.getHeader().getClientType() != getClientType()) {
             // Unsupported client type
-            COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, msg
-                                             .getHeader().getClientType());
-            COPSError err = new COPSError(
-                COPSError.COPS_ERR_UNSUPPORTED_CLIENT_TYPE, (short) 0);
-            COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-            closeMsg.add(cHdr);
-            closeMsg.add(err);
+            final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(msg.getHeader().getClientType(),
+                    new COPSError(ErrorTypes.UNSUPPORTED_CLIENT_TYPE, ErrorTypes.NA), null, null);
             try {
                 closeMsg.writeData(conn);
             } catch (IOException unae) {
+                logger.error("Unexpected error writing COPS data", unae);
             }
 
             throw new COPSException("Unsupported client type");
@@ -195,16 +185,12 @@ public class PCMMPdpAgent extends COPSPdpAgent {
         // PEPId is mandatory
         if (pepId == null) {
             // Mandatory COPS object missing
-            COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, msg
-                                             .getHeader().getClientType());
-            COPSError err = new COPSError(
-                COPSError.COPS_ERR_MANDATORY_OBJECT_MISSING, (short) 0);
-            COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-            closeMsg.add(cHdr);
-            closeMsg.add(err);
+            final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(msg.getHeader().getClientType(),
+                    new COPSError(ErrorTypes.MANDATORY_OBJECT_MISSING, ErrorTypes.NA), null, null);
             try {
                 closeMsg.writeData(conn);
             } catch (IOException unae) {
+                logger.error("Unexpected error writing COPS data", unae);
             }
 
             throw new COPSException("Mandatory COPS object missing (PEPId)");
@@ -212,24 +198,17 @@ public class PCMMPdpAgent extends COPSPdpAgent {
         setPepId(pepId);
         // Support
         if ((cMsg.getClientSI() != null) ) {
-            _mminfo = new MMVersionInfo(cMsg
-                                        .getClientSI().getData().getData());
-            logger.info("CMTS sent MMVersion info : major:"
-                               + _mminfo.getMajorVersionNB() + "  minor:"
-                               + _mminfo.getMinorVersionNB());
+            final MMVersionInfo _mminfo = new MMVersionInfo(cMsg.getClientSI().getData().getData());
+            logger.info("CMTS sent MMVersion info : major:" + _mminfo.getMajorVersionNB() + "  minor:" +
+                    _mminfo.getMinorVersionNB());
 
         } else {
-            // Unsupported objects
-            COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, msg
-                                             .getHeader().getClientType());
-            COPSError err = new COPSError(COPSError.COPS_ERR_UNKNOWN_OBJECT,
-                                          (short) 0);
-            COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-            closeMsg.add(cHdr);
-            closeMsg.add(err);
+            final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(msg.getHeader().getClientType(),
+                    new COPSError(ErrorTypes.UNKNOWN_OBJECT, ErrorTypes.NA), null, null);
             try {
                 closeMsg.writeData(conn);
             } catch (IOException unae) {
+                logger.error("Unexpected error writing COPS data", unae);
             }
 
             throw new COPSException("Unsupported objects (PdpAddress, Integrity)");
@@ -238,39 +217,34 @@ public class PCMMPdpAgent extends COPSPdpAgent {
         */
 
         // Connection accepted
-        COPSHeader ahdr = new COPSHeader(COPSHeader.COPS_OP_CAT, msg
-                                         .getHeader().getClientType());
-        COPSKATimer katimer = new COPSKATimer(getKaTimer());
-        COPSAcctTimer acctTimer = new COPSAcctTimer(getAcctTimer());
-        COPSClientAcceptMsg acceptMsg = new COPSClientAcceptMsg();
-        acceptMsg.add(ahdr);
-        acceptMsg.add(katimer);
+        final COPSKATimer katimer = new COPSKATimer(getKaTimer());
+        final COPSAcctTimer acctTimer = new COPSAcctTimer(getAcctTimer());
+        final COPSClientAcceptMsg acceptMsg;
         if (getAcctTimer() != 0)
-            acceptMsg.add(acctTimer);
+            acceptMsg = new COPSClientAcceptMsg(msg.getHeader().getClientType(), katimer, acctTimer, null);
+        else
+            acceptMsg = new COPSClientAcceptMsg(msg.getHeader().getClientType(), katimer, null, null);
+
         acceptMsg.writeData(conn);
         // XXX - handleRequestMsg
         try {
-            COPSDebug.err(getClass().getName(), "PDP COPSTransceiver.receiveMsg ");
+            logger.info("PDP COPSTransceiver.receiveMsg");
             COPSMsg rmsg = COPSTransceiver.receiveMsg(socket);
             // Client-Close
-            if (rmsg.getHeader().isAClientClose()) {
+            if (rmsg.getHeader().getOpCode().equals(OPCode.CC)) {
                 logger.info("Client close description - " + ((COPSClientCloseMsg) rmsg).getError().getDescription());
                 // close the socket
-                COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, msg
-                                                 .getHeader().getClientType());
-                COPSError err = new COPSError(COPSError.COPS_ERR_UNKNOWN_OBJECT,
-                                              (short) 0);
-                COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-                closeMsg.add(cHdr);
-                closeMsg.add(err);
+                final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(msg.getHeader().getClientType(),
+                        new COPSError(ErrorTypes.UNKNOWN_OBJECT, ErrorTypes.NA), null, null);
                 try {
                     closeMsg.writeData(conn);
                 } catch (IOException unae) {
+                    logger.error("Unexpected exception writing COPS data", unae);
                 }
                 throw new COPSException("CMTS requetsed Client-Close");
             } else {
                 // Request
-                if (rmsg.getHeader().isARequest()) {
+                if (rmsg.getHeader().getOpCode().equals(OPCode.REQ)) {
                     COPSReqMsg rMsg = (COPSReqMsg) rmsg;
                     _handle = rMsg.getClientHandle();
                 } else
@@ -281,7 +255,7 @@ public class PCMMPdpAgent extends COPSPdpAgent {
             throw new COPSException("Error COPSTransceiver.receiveMsg");
         }
 
-        COPSDebug.err(getClass().getName(), "PDPCOPSConnection");
+        logger.info("PDPCOPSConnection");
         PCMMPdpConnection pdpConn = new PCMMPdpConnection(pepId, conn, _process);
         pdpConn.setKaTimer(getKaTimer());
         if (getAcctTimer() != 0)
@@ -295,12 +269,13 @@ public class PCMMPdpAgent extends COPSPdpAgent {
         try {
             man.initRequestState(conn);
         } catch (COPSPdpException unae) {
+            logger.error("Error initializing the state manager's request state");
         }
         // XXX - End handleRequestMsg
 
-        COPSDebug.err(getClass().getName(), "PDP Thread(pdpConn).start");
+        logger.info("PDP Thread(pdpConn).start");
         new Thread(pdpConn).start();
-        getConnectionMap().put(pepId.getData().str(), pdpConn);
+        _connectionMap.put(pepId.getData().str(), pdpConn);
     }
 
     /**
@@ -311,9 +286,11 @@ public class PCMMPdpAgent extends COPSPdpAgent {
     }
 
     /**
+     * TODO - make the host immutable
      * @param _psHost
      *            the _psHost to set
      */
+    @Deprecated
     public void setPsHost(String _psHost) {
         this.psHost = _psHost;
     }
@@ -326,9 +303,11 @@ public class PCMMPdpAgent extends COPSPdpAgent {
     }
 
     /**
+     * TODO - make the port immutable
      * @param _psPort
      *            the _psPort to set
      */
+    @Deprecated
     public void setPsPort(int _psPort) {
         this.psPort = _psPort;
     }
@@ -341,9 +320,11 @@ public class PCMMPdpAgent extends COPSPdpAgent {
     }
 
     /**
+     * TODO - Ensure socket is not overly transient
      * @param socket
      *            the socket to set
      */
+    @Deprecated
     public void setSocket(Socket socket) {
         this.socket = socket;
     }
@@ -384,9 +365,11 @@ public class PCMMPdpAgent extends COPSPdpAgent {
     }
 
     /**
-      * Sets the PepId
-      * @param   pepId - COPSPepId
-      */
+     * Sets the PepId
+     * TODO - make PEP ID and the associate string immutable or remove altogether
+     * @param   pepId - COPSPepId
+     */
+    @Deprecated
     public void setPepId(COPSPepId pepId) {
         _pepId = pepId;
         _pepIdString = pepId.getData().str();