Add missing license headers to packetcable-driver messages
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / messages / impl / MessageFactory.java
index 14a88ac478166485e76b90348ed124834fe4b575..6e48a675cb915d20d51b53d1adf3188fa6f5524e 100644 (file)
@@ -1,6 +1,11 @@
-/**
- * @header@
+/*
+ * Copyright (c) 2014, 2015 Cable Television Laboratories, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
+
 package org.pcmm.messages.impl;
 
 import org.pcmm.messages.IMessage.MessageProperties;
@@ -16,9 +21,11 @@ import org.umu.cops.stack.COPSContext.RType;
 import org.umu.cops.stack.COPSDecision.Command;
 import org.umu.cops.stack.COPSDecision.DecisionFlag;
 import org.umu.cops.stack.COPSError.ErrorTypes;
+import org.umu.cops.stack.COPSHeader.OPCode;
+import org.umu.cops.stack.COPSObjHeader.CType;
 
 import java.net.InetAddress;
-import java.util.Properties;
+import java.util.*;
 
 /**
  *
@@ -47,7 +54,7 @@ public class MessageFactory implements IMessageFactory {
      *
      * @see pcmm.messages.IMessageFactory#create(pcmm.messages.MessageType)
      */
-    public COPSMsg create(final byte messageType) {
+    public COPSMsg create(final OPCode messageType) {
         return create(messageType, new Properties());
     }
 
@@ -57,29 +64,29 @@ public class MessageFactory implements IMessageFactory {
      * @see org.pcmm.messages.IMessageFactory#create(org.pcmm.messages.IMessage.
      * MessageType, java.util.Properties)
      */
-    public COPSMsg create(final byte messageType, final Properties properties) {
+    public COPSMsg create(final OPCode messageType, final Properties properties) {
         logger.info("Creating message of type - " + messageType);
         // return new PCMMMessage(messageType, content);
         switch (messageType) {
-            case COPSHeader.COPS_OP_OPN:
+            case OPN:
                 return createOPNMessage(properties);
-            case COPSHeader.COPS_OP_REQ:
+            case REQ:
                 return createREQMessage(properties);
-            case COPSHeader.COPS_OP_CAT:
+            case CAT:
                 return createCATMessage(properties);
-            case COPSHeader.COPS_OP_CC:
+            case CC:
                 return createCCMessage(properties);
-            case COPSHeader.COPS_OP_DEC:
+            case DEC:
                 return createDECMessage(properties);
-            case COPSHeader.COPS_OP_DRQ:
+            case DRQ:
                 break;
-            case COPSHeader.COPS_OP_KA:
+            case KA:
                 return createKAMessage(properties);
-            case COPSHeader.COPS_OP_RPT:
+            case RPT:
                 break;
-            case COPSHeader.COPS_OP_SSC:
+            case SSC:
                 break;
-            case COPSHeader.COPS_OP_SSQ:
+            case SSQ:
                 break;
         }
         return null;
@@ -91,53 +98,52 @@ public class MessageFactory implements IMessageFactory {
      * @return - the message
      */
     protected COPSMsg createDECMessage(final Properties prop) {
-        final COPSDecisionMsg msg = new COPSDecisionMsg();
+
         // ===common part between all gate control messages
-        final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, IPCMMClient.CLIENT_TYPE);
+        //        final COPSHeader hdr = new COPSHeader(OPCode.DEC, IPCMMClient.CLIENT_TYPE);
         // handle
         // context
-        final COPSContext context = new COPSContext(RType.CONFIG, (short) 0);
         // decision
-
-        Command cmd = null;
+        // TODO - the old command and flag codes are not congruent with the ones described in COPSDecision
+        // TODO - what is the correct client type to be using here???
+/*
         if (prop.get(MessageProperties.DECISION_CMD_CODE) != null)
-            cmd = (Command) prop.get(MessageProperties.DECISION_CMD_CODE);
-
-        DecisionFlag flag = null;
+            decision.setCmdCode((byte) prop.get(MessageProperties.DECISION_CMD_CODE));
         if (prop.get(MessageProperties.DECISION_FLAG) != null)
-            flag = (DecisionFlag) prop.get(MessageProperties.DECISION_FLAG);
-
-        final COPSDecision decision = new COPSDecision(cmd, flag);
+            decision.setFlags((short) prop.get(MessageProperties.DECISION_FLAG));
+*/
 
-        COPSData data = null;
+        final COPSData data;
         if (prop.get(MessageProperties.GATE_CONTROL) != null)
             data = (COPSData) prop.get(MessageProperties.GATE_CONTROL);
+        else
+            data = null;
 
-        // TODO - Determine the proper type here.
-        final COPSClientSI si = new COPSClientSI(CSIType.SIGNALED, data);
-        try {
-            msg.add(hdr);
-            final COPSHandle handle;
-            if (prop.get(MessageProperties.CLIENT_HANDLE) != null) {
-                handle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
-            }
-            else {
-                // TODO - This smells wrong to have a null handle ID
-                handle = new COPSHandle(null);
-            }
-            msg.add(handle);
-            msg.addDecision(decision, context);
-            msg.add(si);
-
-            // TODO - determine why this block has been commented out
-            // try {
-            // msg.dump(System.out);
-            // } catch (IOException unae) {
-            // }
+        // TODO - Need to determine is SIGNALED is the correct default CSIType
+        // Decided that CSI object is not what should be encapsulated by the COPSDecisionMsg, therefore placing
+        // data into the COPSDecision object located in the decisionMap
+        //        final COPSClientSI si = new COPSClientSI(CSIType.SIGNALED, data);
 
-        } catch (final COPSException e) {
-            logger.error(e.getMessage());
+        final COPSHandle handle;
+        if (prop.get(MessageProperties.CLIENT_HANDLE) != null) {
+            handle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
         }
+        else {
+            // TODO - This smells wrong to have a null handle ID
+            handle = new COPSHandle(null);
+        }
+        final Set<COPSDecision> decisionSet = new HashSet<>();
+        decisionSet.add(new COPSDecision(CType.DEF, Command.NULL, DecisionFlag.NA, data));
+        final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
+        decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
+
+        final COPSDecisionMsg msg = new COPSDecisionMsg(IPCMMClient.CLIENT_TYPE, handle, decisionMap, null, null);
+
+        // TODO - determine why this block has been commented out
+        // try {
+        // msg.dump(System.out);
+        // } catch (IOException unae) {
+        // }
 
         return msg;
     }
@@ -150,7 +156,6 @@ public class MessageFactory implements IMessageFactory {
      * @return COPS message
      */
     protected COPSMsg createOPNMessage(final Properties prop) {
-        final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, IPCMMClient.CLIENT_TYPE);
         // version infor object
         short majorVersion = MMVersionInfo.DEFAULT_MAJOR_VERSION_INFO;
         short minorVersion = MMVersionInfo.DEFAULT_MINOR_VERSION_INFO;
@@ -160,9 +165,7 @@ public class MessageFactory implements IMessageFactory {
             minorVersion = (Short) prop.get(MessageProperties.MM_MINOR_VERSION_INFO);
         // Mandatory MM version.
         byte[] versionInfo = new MMVersionInfo(majorVersion, minorVersion).getAsBinaryArray();
-        final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED,
-                new COPSData(versionInfo, 0, versionInfo.length));
-        final COPSClientOpenMsg msg = new COPSClientOpenMsg();
+        final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED, new COPSData(versionInfo, 0, versionInfo.length));
         try {
             final COPSData d;
             if (prop.get(MessageProperties.PEP_ID) != null)
@@ -170,13 +173,13 @@ public class MessageFactory implements IMessageFactory {
             else
                 d = new COPSData(InetAddress.getLocalHost().getHostName());
             final COPSPepId pepId = new COPSPepId(d);
-            msg.add(hdr);
-            msg.add(pepId);
-            msg.add(clientSI);
+            return new COPSClientOpenMsg(IPCMMClient.CLIENT_TYPE, pepId, clientSI, null, null);
         } catch (Exception e) {
             logger.error("Error creating OPN message", e);
         }
-        return msg;
+
+        // TODO - this probably should not return null and throw an exception instead
+        return null;
     }
 
     /**
@@ -185,29 +188,23 @@ public class MessageFactory implements IMessageFactory {
      * @return COPS message
      */
     protected COPSMsg createCATMessage(final Properties prop) {
-        final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_CAT, IPCMMClient.CLIENT_TYPE);
+        // TODO - determine what the first constructor parameter really should be???
         final COPSKATimer katimer;
         if (prop.get(MessageProperties.KA_TIMER) != null)
-            katimer = new COPSKATimer((short) prop.get(MessageProperties.KA_TIMER));
+            katimer = new COPSKATimer((short)prop.get(MessageProperties.KA_TIMER));
         else
             katimer = new COPSKATimer(KA_TIMER_VALUE);
 
+        // TODO - determine what the first constructor parameter really should be???
         final COPSAcctTimer acctTimer;
         if (prop.get(MessageProperties.ACCEPT_TIMER) != null)
             acctTimer = new COPSAcctTimer((short) prop.get(MessageProperties.ACCEPT_TIMER));
         else
             acctTimer = new COPSAcctTimer(ACCT_TIMER_VALUE);
 
-        final COPSClientAcceptMsg acceptMsg = new COPSClientAcceptMsg();
-        try {
-            acceptMsg.add(hdr);
-            acceptMsg.add(katimer);
-            if (acctTimer.getTimerVal() != 0)
-                acceptMsg.add(acctTimer);
-        } catch (COPSException e) {
-            logger.error(e.getMessage());
-        }
-        return acceptMsg;
+        if (acctTimer.getTimerVal() != 0)
+            return new COPSClientAcceptMsg(IPCMMClient.CLIENT_TYPE, katimer, acctTimer, null);
+        else return new COPSClientAcceptMsg(IPCMMClient.CLIENT_TYPE, katimer, null, null);
     }
 
     /**
@@ -218,25 +215,18 @@ public class MessageFactory implements IMessageFactory {
      * @return COPS message
      */
     protected COPSMsg createCCMessage(final Properties prop) {
-        final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, IPCMMClient.CLIENT_TYPE);
         final COPSError err;
         if (prop.get(MessageProperties.ERR_MESSAGE) != null) {
             ErrorTypes code = ErrorTypes.NA;
             final ErrorTypes error = (ErrorTypes) prop.get(MessageProperties.ERR_MESSAGE);
             if (prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE) != null)
                 code = (ErrorTypes) prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE);
-            err = new COPSError(error, code);
+            err = new COPSError(COPSError.ERROR_CODE_TO_TYPE.get(error.ordinal()),
+                    COPSError.ERROR_CODE_TO_TYPE.get(code.ordinal()));
         } else
             err = new COPSError(ErrorTypes.UNKNOWN, ErrorTypes.NA);
 
-        final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-        try {
-            closeMsg.add(cHdr);
-            closeMsg.add(err);
-        } catch (COPSException e) {
-            logger.error(e.getMessage());
-        }
-        return closeMsg;
+        return new COPSClientCloseMsg(IPCMMClient.CLIENT_TYPE, err, null, null);
     }
 
     /**
@@ -247,34 +237,25 @@ public class MessageFactory implements IMessageFactory {
      * @return Request message
      */
     protected COPSMsg createREQMessage(final Properties prop) {
-        final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_REQ, IPCMMClient.CLIENT_TYPE);
-        final COPSReqMsg req = new COPSReqMsg();
-
-        final RType rType;
+        final short rType;
         if (prop.get(MessageProperties.R_TYPE) != null)
-            rType = (RType) prop.get(MessageProperties.R_TYPE);
-        else rType = RType.NA; //ICMTS.DEFAULT_R_TYPE;
+            rType = (Short) prop.get(MessageProperties.R_TYPE);
+        else rType = ICMTS.DEFAULT_R_TYPE;
 
         final short mType;
         if (prop.get(MessageProperties.M_TYPE) != null)
             mType = (Short) prop.get(MessageProperties.M_TYPE);
         else mType = ICMTS.DEFAULT_M_TYPE;
 
-        final COPSContext copsContext = new COPSContext(rType, mType);
+        final COPSContext copsContext = new COPSContext(COPSContext.VAL_TO_RTYPE.get((int)rType), mType);
         final COPSHandle copsHandle;
         if (prop.get(MessageProperties.CLIENT_HANDLE) != null)
             copsHandle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
         else
             // just a random handle
             copsHandle = new COPSHandle(new COPSData("" + Math.random() * 82730));
-        try {
-            req.add(cHdr);
-            req.add(copsContext);
-            req.add(copsHandle);
-        } catch (COPSException e) {
-            logger.error(e.getMessage());
-        }
-        return req;
+
+        return new COPSReqMsg(IPCMMClient.CLIENT_TYPE, copsHandle, copsContext, null, null, null, null, null);
     }
 
     /**
@@ -286,18 +267,14 @@ public class MessageFactory implements IMessageFactory {
      * TODO - Why is there a timer being instantiated but never used?
      */
     protected COPSMsg createKAMessage(final Properties prop) {
-        final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_KA, (short) 0);
-        final COPSKAMsg kaMsg = new COPSKAMsg();
+        // TODO - determine why this isn't really doing anything
+        return new COPSKAMsg(null);
+/*
         final COPSKATimer timer;
         if (prop.get(MessageProperties.KA_TIMER) != null)
-            timer = new COPSKATimer((Short) prop.get(MessageProperties.KA_TIMER));
+            timer = new COPSKATimer((short)0, (Short) prop.get(MessageProperties.KA_TIMER));
         else
-            timer = new COPSKATimer((short)1);
-        try {
-            kaMsg.add(cHdr);
-        } catch (COPSException e) {
-            logger.error(e.getMessage());
-        }
-        return kaMsg;
+            timer = new COPSKATimer();
+*/
     }
 }