From 045ac6c9697537852b42074bfd4f7a47f69fd33b Mon Sep 17 00:00:00 2001 From: Steven Pisarski Date: Fri, 13 Mar 2015 15:49:06 -0600 Subject: [PATCH] Refactor classes designed to hold data to be more immutable, and reducing the probability of throwing NullPointerExceptions as well as adding some unit tests around them. Change-Id: Ibab87ae43226d36b399ceaf987123e7797e719f3 Signed-off-by: Steven Pisarski --- .../main/java/org/pcmm/PCMMPdpMsgSender.java | 70 +-- .../java/org/pcmm/PCMMPdpReqStateMan.java | 30 +- .../pcmm/messages/impl/MessageFactory.java | 526 +++++++++--------- .../umu/cops/ospdp/COPSPdpOSMsgSender.java | 24 +- .../umu/cops/ospdp/COPSPdpOSReqStateMan.java | 18 +- .../umu/cops/ospep/COPSPepOSReqStateMan.java | 13 +- .../org/umu/cops/prpdp/COPSPdpMsgSender.java | 30 +- .../umu/cops/prpdp/COPSPdpReqStateMan.java | 22 +- .../umu/cops/prpep/COPSPepReqStateMan.java | 17 +- .../java/org/umu/cops/stack/COPSData.java | 70 ++- .../java/org/umu/cops/stack/COPSHandle.java | 111 ++-- .../java/org/umu/cops/stack/COPSObjBase.java | 47 +- .../org/umu/cops/stack/COPSObjHeader.java | 179 +++--- .../stack/COPSDataFirstConstructorTest.java | 21 + .../stack/COPSDataSecondConstructorTest.java | 129 +++++ .../stack/COPSDataThirdConstructorTest.java | 53 ++ .../stack/COPSHandleFirstConstructorTest.java | 63 +++ .../COPSHandleSecondConstructorTest.java | 41 ++ .../org/umu/cops/stack/COPSObjHeaderTest.java | 55 ++ .../OpendaylightPacketcableProvider.java | 47 +- 20 files changed, 902 insertions(+), 664 deletions(-) create mode 100644 packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataFirstConstructorTest.java create mode 100644 packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataSecondConstructorTest.java create mode 100644 packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataThirdConstructorTest.java create mode 100644 packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleFirstConstructorTest.java create mode 100644 packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleSecondConstructorTest.java create mode 100644 packetcable-driver/src/test/java/org/umu/cops/stack/COPSObjHeaderTest.java diff --git a/packetcable-driver/src/main/java/org/pcmm/PCMMPdpMsgSender.java b/packetcable-driver/src/main/java/org/pcmm/PCMMPdpMsgSender.java index 89e7f44..a13ec99 100644 --- a/packetcable-driver/src/main/java/org/pcmm/PCMMPdpMsgSender.java +++ b/packetcable-driver/src/main/java/org/pcmm/PCMMPdpMsgSender.java @@ -4,46 +4,19 @@ package org.pcmm; +import org.pcmm.gates.*; +import org.pcmm.gates.IGateSpec.DSCPTOS; +import org.pcmm.gates.IGateSpec.Direction; +import org.pcmm.gates.impl.*; +import org.umu.cops.prpdp.COPSPdpException; +import org.umu.cops.stack.*; + import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; -import org.pcmm.gates.IAMID; -import org.pcmm.gates.IClassifier; -import org.pcmm.gates.IExtendedClassifier; -import org.pcmm.gates.IGateID; -import org.pcmm.gates.IGateSpec; -import org.pcmm.gates.IGateSpec.DSCPTOS; -import org.pcmm.gates.IGateSpec.Direction; -import org.pcmm.gates.IPCMMGate; -import org.pcmm.gates.ISubscriberID; -import org.pcmm.gates.ITrafficProfile; -import org.pcmm.gates.ITransactionID; -import org.pcmm.gates.impl.AMID; -import org.pcmm.gates.impl.BestEffortService; -import org.pcmm.gates.impl.Classifier; -import org.pcmm.gates.impl.ExtendedClassifier; -import org.pcmm.gates.impl.GateID; -import org.pcmm.gates.impl.GateSpec; -import org.pcmm.gates.impl.PCMMGateReq; -import org.pcmm.gates.impl.SubscriberID; -import org.pcmm.gates.impl.TransactionID; -import org.umu.cops.prpdp.COPSPdpException; -import org.umu.cops.stack.COPSClientSI; -import org.umu.cops.stack.COPSContext; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDecision; -import org.umu.cops.stack.COPSDecisionMsg; -import org.umu.cops.stack.COPSException; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSHeader; -import org.umu.cops.stack.COPSMsg; -import org.umu.cops.stack.COPSObjHeader; //temp -import org.umu.cops.stack.COPSReportMsg; -import org.umu.cops.stack.COPSSyncStateMsg; -import org.umu.cops.stack.COPSTransceiver; //pcmm /* * Example of an UNSOLICITED decision @@ -158,12 +131,10 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); ITransactionID trID = new TransactionID(); - handle.setId(getClientHandle().getId()); - // set transaction ID to gate set trID.setGateCommandType(ITransactionID.GateSet); _transactionID = (short) (_transactionID == 0 ? (short) (Math.random() * hashCode()) @@ -225,7 +196,6 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); IPCMMGate gate = new PCMMGateReq(); @@ -293,7 +263,7 @@ public class PCMMPdpMsgSender { // new pcmm specific clientsi COPSClientSI clientSD = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4); - handle.setId(getClientHandle().getId()); + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); // set transaction ID to gate set trID.setGateCommandType(ITransactionID.GateSet); @@ -424,8 +394,6 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, getClientType()); - // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); IPCMMGate gate = new PCMMGateReq(); @@ -487,7 +455,8 @@ public class PCMMPdpMsgSender { COPSClientSI clientSD = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4); - handle.setId(getClientHandle().getId()); + // Client Handle with the same clientHandle as the request + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); // set transaction ID to gate set trID.setGateCommandType(ITransactionID.GateSet); @@ -651,8 +620,6 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, getClientType()); - // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); IPCMMGate gate = new PCMMGateReq(); @@ -678,7 +645,8 @@ public class PCMMPdpMsgSender { COPSClientSI clientSD = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4); - handle.setId(getClientHandle().getId()); + // Client Handle with the same clientHandle as the request + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); // byte[] content = "1234".getBytes(); // handle.setId(new COPSData(content, 0, content.length)); @@ -780,7 +748,6 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); IPCMMGate gate = new PCMMGateReq(); @@ -794,7 +761,7 @@ public class PCMMPdpMsgSender { // new pcmm specific clientsi COPSClientSI clientSD = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4); - handle.setId(getClientHandle().getId()); + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); // set transaction ID to gate set trID.setGateCommandType(ITransactionID.GateDelete); @@ -875,8 +842,7 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); // Decisions // @@ -918,8 +884,7 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_SSQ, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); COPSSyncStateMsg msg = new COPSSyncStateMsg(); try { @@ -953,8 +918,7 @@ public class PCMMPdpMsgSender { COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_SSQ, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); COPSSyncStateMsg msg = new COPSSyncStateMsg(); try { diff --git a/packetcable-driver/src/main/java/org/pcmm/PCMMPdpReqStateMan.java b/packetcable-driver/src/main/java/org/pcmm/PCMMPdpReqStateMan.java index 6ef939b..0693525 100644 --- a/packetcable-driver/src/main/java/org/pcmm/PCMMPdpReqStateMan.java +++ b/packetcable-driver/src/main/java/org/pcmm/PCMMPdpReqStateMan.java @@ -8,28 +8,17 @@ package org.pcmm; import java.io.*; import java.util.UUID.*; */ +import org.pcmm.gates.ITransactionID; +import org.pcmm.gates.impl.PCMMGateReq; +import org.umu.cops.common.COPSDebug; +import org.umu.cops.prpdp.COPSPdpException; +import org.umu.cops.stack.*; + import java.net.Socket; import java.util.Arrays; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; - -import org.pcmm.gates.ITransactionID; -import org.pcmm.gates.impl.PCMMGateReq; -import org.umu.cops.common.COPSDebug; -import org.umu.cops.prpdp.COPSPdpException; -import org.umu.cops.stack.COPSClientSI; -import org.umu.cops.stack.COPSContext; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDeleteMsg; -import org.umu.cops.stack.COPSError; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSHeader; -import org.umu.cops.stack.COPSPrObjBase; -import org.umu.cops.stack.COPSReportMsg; -import org.umu.cops.stack.COPSReportType; -import org.umu.cops.stack.COPSReqMsg; -import org.umu.cops.stack.COPSSyncStateMsg; /* import org.pcmm.base.IPCMMBaseObject; import org.pcmm.gates.IAMID; @@ -130,13 +119,8 @@ public class PCMMPdpReqStateMan { * @param clientHandle Client handle */ public PCMMPdpReqStateMan(short clientType, String clientHandle) { - // COPS Handle - _handle = new COPSHandle(); - COPSData id = new COPSData(clientHandle); - _handle.setId(id); - // client-type + _handle = new COPSHandle(new COPSData(clientHandle)); _clientType = clientType; - _status = ST_CREATE; } diff --git a/packetcable-driver/src/main/java/org/pcmm/messages/impl/MessageFactory.java b/packetcable-driver/src/main/java/org/pcmm/messages/impl/MessageFactory.java index 25d26f9..a110fca 100644 --- a/packetcable-driver/src/main/java/org/pcmm/messages/impl/MessageFactory.java +++ b/packetcable-driver/src/main/java/org/pcmm/messages/impl/MessageFactory.java @@ -3,11 +3,6 @@ */ package org.pcmm.messages.impl; -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.Properties; - import org.pcmm.messages.IMessage.MessageProperties; import org.pcmm.messages.IMessageFactory; import org.pcmm.objects.MMVersionInfo; @@ -15,285 +10,282 @@ import org.pcmm.rcd.ICMTS; import org.pcmm.rcd.IPCMMClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.umu.cops.stack.COPSAcctTimer; -import org.umu.cops.stack.COPSClientAcceptMsg; -import org.umu.cops.stack.COPSClientCloseMsg; -import org.umu.cops.stack.COPSClientOpenMsg; -import org.umu.cops.stack.COPSClientSI; -import org.umu.cops.stack.COPSContext; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDecision; -import org.umu.cops.stack.COPSDecisionMsg; -import org.umu.cops.stack.COPSError; -import org.umu.cops.stack.COPSException; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSHeader; -import org.umu.cops.stack.COPSKAMsg; -import org.umu.cops.stack.COPSKATimer; -import org.umu.cops.stack.COPSMsg; -import org.umu.cops.stack.COPSObjHeader; -import org.umu.cops.stack.COPSPepId; -import org.umu.cops.stack.COPSReqMsg; +import org.umu.cops.stack.*; + +import java.net.InetAddress; +import java.util.Properties; /** - * - * + * + * */ public class MessageFactory implements IMessageFactory { - /** Default keep-alive timer value (secs) */ - public static final short KA_TIMER_VALUE = 30; - /** Default accounting timer value (secs) */ - public static final short ACCT_TIMER_VALUE = 0; + /** Default keep-alive timer value (secs) */ + public static final short KA_TIMER_VALUE = 30; + /** Default accounting timer value (secs) */ + public static final short ACCT_TIMER_VALUE = 0; + + private static final Logger logger = LoggerFactory.getLogger(MessageFactory.class); + + private static final MessageFactory instance = new MessageFactory(); + + private MessageFactory() { + } + + public static MessageFactory getInstance() { + return instance; + } + + /* + * (non-Javadoc) + * + * @see pcmm.messages.IMessageFactory#create(pcmm.messages.MessageType) + */ + public COPSMsg create(final byte messageType) { + return create(messageType, new Properties()); + } + + /* + * (non-Javadoc) + * + * @see org.pcmm.messages.IMessageFactory#create(org.pcmm.messages.IMessage. + * MessageType, java.util.Properties) + */ + public COPSMsg create(final byte messageType, final Properties properties) { + logger.info("Creating message of type - " + messageType); + // return new PCMMMessage(messageType, content); + switch (messageType) { + case COPSHeader.COPS_OP_OPN: + return createOPNMessage(properties); + case COPSHeader.COPS_OP_REQ: + return createREQMessage(properties); + case COPSHeader.COPS_OP_CAT: + return createCATMessage(properties); + case COPSHeader.COPS_OP_CC: + return createCCMessage(properties); + case COPSHeader.COPS_OP_DEC: + return createDECMessage(properties); + case COPSHeader.COPS_OP_DRQ: + break; + case COPSHeader.COPS_OP_KA: + return createKAMessage(properties); + case COPSHeader.COPS_OP_RPT: + break; + case COPSHeader.COPS_OP_SSC: + break; + case COPSHeader.COPS_OP_SSQ: + break; + } + return null; + } + + /** + * + * @param prop - the properties + * @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); + // handle + // context + final COPSContext context = new COPSContext(COPSContext.CONFIG, (short) 0); + // decision + final COPSDecision decision = new COPSDecision(); + if (prop.get(MessageProperties.DECISION_CMD_CODE) != null) + decision.setCmdCode((byte) prop.get(MessageProperties.DECISION_CMD_CODE)); + if (prop.get(MessageProperties.DECISION_FLAG) != null) + decision.setFlags((short) prop.get(MessageProperties.DECISION_FLAG)); + + final COPSClientSI si = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4); + if (prop.get(MessageProperties.GATE_CONTROL) != null) + si.setData((COPSData) prop.get(MessageProperties.GATE_CONTROL)); + 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); - private Logger logger = LoggerFactory.getLogger(getClass().getName()); + // TODO - determine why this block has been commented out + // try { + // msg.dump(System.out); + // } catch (IOException unae) { + // } - private static MessageFactory instance; + } catch (final COPSException e) { + logger.error(e.getMessage()); + } - private MessageFactory() { - } + return msg; + } - public static MessageFactory getInstance() { - if (instance == null) - instance = new MessageFactory(); - return instance; - } + /** + * creates a Client-Open message. + * + * @param prop + * properties + * @return COPS message + */ + protected COPSMsg createOPNMessage(final Properties prop) { + final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, IPCMMClient.CLIENT_TYPE); + final COPSPepId pepId = new COPSPepId(); + // version infor object + short majorVersion = MMVersionInfo.DEFAULT_MAJOR_VERSION_INFO; + short minorVersion = MMVersionInfo.DEFAULT_MINOR_VERSION_INFO; + if (prop.get(MessageProperties.MM_MAJOR_VERSION_INFO) != null) + majorVersion = (Short) prop.get(MessageProperties.MM_MAJOR_VERSION_INFO); + if (prop.get(MessageProperties.MM_MINOR_VERSION_INFO) != null) + minorVersion = (Short) prop.get(MessageProperties.MM_MINOR_VERSION_INFO); + // Mandatory MM version. + final COPSClientSI clientSI = new COPSClientSI((byte) 1); + byte[] versionInfo = new MMVersionInfo(majorVersion, minorVersion).getAsBinaryArray(); + clientSI.setData(new COPSData(versionInfo, 0, versionInfo.length)); + final COPSClientOpenMsg msg = new COPSClientOpenMsg(); + try { + final COPSData d; + if (prop.get(MessageProperties.PEP_ID) != null) + d = new COPSData((String) prop.get(MessageProperties.PEP_ID)); + else + d = new COPSData(InetAddress.getLocalHost().getHostName()); + pepId.setData(d); + msg.add(hdr); + msg.add(pepId); + msg.add(clientSI); + } catch (Exception e) { + logger.error("Error creating OPN message", e); + } + return msg; + } - /* - * (non-Javadoc) - * - * @see pcmm.messages.IMessageFactory#create(pcmm.messages.MessageType) - */ - public COPSMsg create(byte messageType) { - return create(messageType, new Properties()); - } + /** + * creates a Client-Accept message. + * @param prop - properties + * @return COPS message + */ + protected COPSMsg createCATMessage(final Properties prop) { + final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_CAT, IPCMMClient.CLIENT_TYPE); + final COPSKATimer katimer; + if (prop.get(MessageProperties.KA_TIMER) != null) + katimer = new COPSKATimer((short) prop.get(MessageProperties.KA_TIMER)); + else + katimer = new COPSKATimer(KA_TIMER_VALUE); - /* - * (non-Javadoc) - * - * @see org.pcmm.messages.IMessageFactory#create(org.pcmm.messages.IMessage. - * MessageType, java.util.Properties) - */ - public COPSMsg create(byte messageType, Properties properties) { - // return new PCMMMessage(messageType, content); - switch (messageType) { - case COPSHeader.COPS_OP_OPN: - return createOPNMessage(properties); - case COPSHeader.COPS_OP_REQ: - return createREQMessage(properties); - case COPSHeader.COPS_OP_CAT: - return createCATMessage(properties); - case COPSHeader.COPS_OP_CC: - return createCCMessage(properties); - case COPSHeader.COPS_OP_DEC: - return createDECMessage(properties); - case COPSHeader.COPS_OP_DRQ: - break; - case COPSHeader.COPS_OP_KA: - return createKAMessage(properties); - case COPSHeader.COPS_OP_RPT: - break; - case COPSHeader.COPS_OP_SSC: - break; - case COPSHeader.COPS_OP_SSQ: - break; - } - return null; - } + 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); - /** - * - * @param prop - * @return - */ - protected COPSMsg createDECMessage(Properties prop) { - COPSDecisionMsg msg = new COPSDecisionMsg(); - // ===common part between all gate control messages - COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, IPCMMClient.CLIENT_TYPE); - // handle - COPSHandle handle = new COPSHandle(); - // context - COPSContext context = new COPSContext(COPSContext.CONFIG, (short) 0); - // decision - COPSDecision decision = new COPSDecision(); - if (prop.get(MessageProperties.DECISION_CMD_CODE) != null) - decision.setCmdCode((byte) prop.get(MessageProperties.DECISION_CMD_CODE)); - if (prop.get(MessageProperties.DECISION_FLAG) != null) - decision.setFlags((short) prop.get(MessageProperties.DECISION_FLAG)); - COPSClientSI si = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4); - if (prop.get(MessageProperties.GATE_CONTROL) != null) - si.setData((COPSData) prop.get(MessageProperties.GATE_CONTROL)); - try { - msg.add(hdr); - if (prop.get(MessageProperties.CLIENT_HANDLE) != null) - handle.setId(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE))); - msg.add(handle); - msg.addDecision(decision, context); - msg.add(si); - // try { - // msg.dump(System.out); - // } catch (IOException unae) { - // } + 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; + } - } catch (COPSException e) { - logger.error(e.getMessage()); - } + /** + * creates a Client-Close message. + * + * @param prop + * properties + * @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) { + short code = (short) 0; + final short error = (short) prop.get(MessageProperties.ERR_MESSAGE); + if (prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE) != null) + code = (short) prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE); + err = new COPSError(error, code); + } else + err = new COPSError(COPSError.COPS_ERR_UNKNOWN, (short) 0); - return msg; - } + final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(); + try { + closeMsg.add(cHdr); + closeMsg.add(err); + } catch (COPSException e) { + logger.error(e.getMessage()); + } + return closeMsg; + } - /** - * creates a Client-Open message. - * - * @param prop - * properties - * @return COPS message - */ - protected COPSMsg createOPNMessage(Properties prop) { - COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, IPCMMClient.CLIENT_TYPE); - COPSPepId pepId = new COPSPepId(); - // version infor object - short majorVersion = MMVersionInfo.DEFAULT_MAJOR_VERSION_INFO; - short minorVersion = MMVersionInfo.DEFAULT_MINOR_VERSION_INFO; - if (prop.get(MessageProperties.MM_MAJOR_VERSION_INFO) != null) - majorVersion = (Short) prop.get(MessageProperties.MM_MAJOR_VERSION_INFO); - if (prop.get(MessageProperties.MM_MINOR_VERSION_INFO) != null) - minorVersion = (Short) prop.get(MessageProperties.MM_MINOR_VERSION_INFO); - // Mandatory MM version. - COPSClientSI clientSI = new COPSClientSI((byte) 1); - byte[] versionInfo = new MMVersionInfo(majorVersion, minorVersion).getAsBinaryArray(); - clientSI.setData(new COPSData(versionInfo, 0, versionInfo.length)); - COPSClientOpenMsg msg = new COPSClientOpenMsg(); - try { - COPSData d = null; - if (prop.get(MessageProperties.PEP_ID) != null) - d = new COPSData((String) prop.get(MessageProperties.PEP_ID)); - else - d = new COPSData(InetAddress.getLocalHost().getHostName()); - pepId.setData(d); - msg.add(hdr); - msg.add(pepId); - msg.add(clientSI); - } catch (COPSException e) { - logger.error(e.getMessage()); - } catch (UnknownHostException e) { - logger.error(e.getMessage()); - } - return msg; - } + /** + * creates a Request message + * + * @param prop + * properties + * @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(); - /** - * creates a Client-Accept message. - * - * @param prop - * properties - * @return COPS message - */ - protected COPSMsg createCATMessage(Properties prop) { - COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_CAT, IPCMMClient.CLIENT_TYPE); - COPSKATimer katimer = null; - COPSAcctTimer acctTimer = null; - if (prop.get(MessageProperties.KA_TIMER) != null) - katimer = new COPSKATimer((short) prop.get(MessageProperties.KA_TIMER)); - else - katimer = new COPSKATimer((short) KA_TIMER_VALUE); - if (prop.get(MessageProperties.ACCEPT_TIMER) != null) - acctTimer = new COPSAcctTimer((short) prop.get(MessageProperties.ACCEPT_TIMER)); - else - acctTimer = new COPSAcctTimer(ACCT_TIMER_VALUE); - 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; - } + final short rType; + if (prop.get(MessageProperties.R_TYPE) != null) + rType = (Short) prop.get(MessageProperties.R_TYPE); + else rType = ICMTS.DEFAULT_R_TYPE; - /** - * creates a Client-Close message. - * - * @param prop - * properties - * @return COPS message - */ - protected COPSMsg createCCMessage(Properties prop) { - COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, IPCMMClient.CLIENT_TYPE); - COPSError err = null; - if (prop.get(MessageProperties.ERR_MESSAGE) != null) { - short code = (short) 0; - short error = (short) prop.get(MessageProperties.ERR_MESSAGE); - if (prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE) != null) - code = (short) prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE); - err = new COPSError(error, code); - } else - err = new COPSError(COPSError.COPS_ERR_UNKNOWN, (short) 0); - COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(); - try { - closeMsg.add(cHdr); - closeMsg.add(err); - } catch (COPSException e) { - logger.error(e.getMessage()); - } - return closeMsg; - } + final short mType; + if (prop.get(MessageProperties.M_TYPE) != null) + mType = (Short) prop.get(MessageProperties.M_TYPE); + else mType = ICMTS.DEFAULT_M_TYPE; - /** - * creates a Request message - * - * @param prop - * properties - * @return Request message - */ - protected COPSMsg createREQMessage(Properties prop) { - COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_REQ, IPCMMClient.CLIENT_TYPE); - COPSReqMsg req = new COPSReqMsg(); - short rType = ICMTS.DEFAULT_R_TYPE; - short mType = ICMTS.DEFAULT_M_TYPE; - if (prop.get(MessageProperties.R_TYPE) != null) - rType = (Short) prop.get(MessageProperties.R_TYPE); - if (prop.get(MessageProperties.M_TYPE) != null) - mType = (Short) prop.get(MessageProperties.M_TYPE); - COPSContext copsContext = new COPSContext(rType, mType); - COPSHandle copsHandle = new COPSHandle(); - if (prop.get(MessageProperties.CLIENT_HANDLE) != null) - copsHandle.setId(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE))); - else - // just a random handle - copsHandle.setId(new COPSData("" + Math.random() * 82730)); - try { - req.add(cHdr); - req.add(copsContext); - req.add(copsHandle); - } catch (COPSException e) { - logger.error(e.getMessage()); - } - return req; - } + final COPSContext copsContext = new COPSContext(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; + } - /** - * creates a Keep-Alive message. - * - * @param prop - * properties - * @return COPS message - */ - protected COPSMsg createKAMessage(Properties prop) { - COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_KA, (short) 0); - COPSKAMsg kaMsg = new COPSKAMsg(); - COPSKATimer timer = null; - if (prop.get(MessageProperties.KA_TIMER) != null) - timer = new COPSKATimer((Short) prop.get(MessageProperties.KA_TIMER)); - else - timer = new COPSKATimer(); - try { - kaMsg.add(cHdr); - } catch (COPSException e) { - logger.error(e.getMessage()); - } - return kaMsg; - } + /** + * creates a Keep-Alive message. + * + * @param prop + * properties + * @return COPS message + * 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(); + final COPSKATimer timer; + if (prop.get(MessageProperties.KA_TIMER) != null) + timer = new COPSKATimer((Short) prop.get(MessageProperties.KA_TIMER)); + else + timer = new COPSKATimer(); + try { + kaMsg.add(cHdr); + } catch (COPSException e) { + logger.error(e.getMessage()); + } + return kaMsg; + } } diff --git a/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSMsgSender.java b/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSMsgSender.java index c341328..25cacd6 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSMsgSender.java +++ b/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSMsgSender.java @@ -1,20 +1,16 @@ package org.umu.cops.ospdp; +import org.umu.cops.stack.*; + import java.io.IOException; import java.net.Socket; import java.util.Enumeration; import java.util.Vector; -import org.umu.cops.stack.COPSContext; -import org.umu.cops.stack.COPSDecision; -import org.umu.cops.stack.COPSDecisionMsg; -import org.umu.cops.stack.COPSException; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSHeader; -import org.umu.cops.stack.COPSSyncStateMsg; - /** * COPS message transceiver class for outsourcing connections at the PDP side. + * + * TODO - change all references of Vector to List<> */ public class COPSPdpOSMsgSender { /** @@ -99,8 +95,7 @@ public class COPSPdpOSMsgSender { hdr.setFlag(COPSHeader.COPS_FLAG_SOLICITED); // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); - handle.setId(getClientHandle().getId()); + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); try { @@ -176,8 +171,7 @@ public class COPSPdpOSMsgSender { COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); // Decisions // @@ -225,8 +219,7 @@ public class COPSPdpOSMsgSender { COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); // Decisions // @@ -267,8 +260,7 @@ public class COPSPdpOSMsgSender { COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); COPSSyncStateMsg msg = new COPSSyncStateMsg(); try { diff --git a/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSReqStateMan.java b/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSReqStateMan.java index ffd379c..855a14c 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSReqStateMan.java +++ b/packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSReqStateMan.java @@ -1,17 +1,10 @@ package org.umu.cops.ospdp; +import org.umu.cops.stack.*; + import java.net.Socket; import java.util.Vector; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDeleteMsg; -import org.umu.cops.stack.COPSError; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSReportMsg; -import org.umu.cops.stack.COPSReportType; -import org.umu.cops.stack.COPSReqMsg; -import org.umu.cops.stack.COPSSyncStateMsg; - /** * State manager class for outsourcing requests, at the PDP side. */ @@ -95,13 +88,8 @@ public class COPSPdpOSReqStateMan { * @param clientHandle Client handle */ public COPSPdpOSReqStateMan(short clientType, String clientHandle) { - // COPS Handle - _handle = new COPSHandle(); - COPSData id = new COPSData(clientHandle); - _handle.setId(id); - // client-type + _handle = new COPSHandle(new COPSData(clientHandle)); _clientType = clientType; - _status = ST_CREATE; } diff --git a/packetcable-driver/src/main/java/org/umu/cops/ospep/COPSPepOSReqStateMan.java b/packetcable-driver/src/main/java/org/umu/cops/ospep/COPSPepOSReqStateMan.java index aab3bc1..3cc66a3 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/ospep/COPSPepOSReqStateMan.java +++ b/packetcable-driver/src/main/java/org/umu/cops/ospep/COPSPepOSReqStateMan.java @@ -1,14 +1,10 @@ package org.umu.cops.ospep; +import org.umu.cops.stack.*; + import java.net.Socket; import java.util.Vector; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDecisionMsg; -import org.umu.cops.stack.COPSError; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSSyncStateMsg; - /** * State manager class for outsourcing requests, at the PEP side. */ @@ -105,10 +101,7 @@ public class COPSPepOSReqStateMan { */ public COPSPepOSReqStateMan(short clientType, String clientHandle) { // COPS Handle - _handle = new COPSHandle(); - COPSData id = new COPSData(clientHandle); - _handle.setId(id); - // client-type + _handle = new COPSHandle(new COPSData(clientHandle)); _clientType = clientType; _syncState = true; _status = ST_CREATE; diff --git a/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpMsgSender.java b/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpMsgSender.java index a892b7f..956af9c 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpMsgSender.java +++ b/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpMsgSender.java @@ -6,24 +6,17 @@ package org.umu.cops.prpdp; +import org.umu.cops.stack.*; + import java.io.IOException; import java.net.Socket; import java.util.Enumeration; import java.util.Hashtable; -import org.umu.cops.stack.COPSContext; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDecision; -import org.umu.cops.stack.COPSDecisionMsg; -import org.umu.cops.stack.COPSException; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSHeader; -import org.umu.cops.stack.COPSPrEPD; -import org.umu.cops.stack.COPSPrID; -import org.umu.cops.stack.COPSSyncStateMsg; - /** * COPS message transceiver class for provisioning connections at the PDP side. + * + * TODO - Need to continue refactoring by removing all instances of Hashtable (change to Map<>) */ public class COPSPdpMsgSender { @@ -106,8 +99,7 @@ public class COPSPdpMsgSender { hdr.setFlag(COPSHeader.COPS_FLAG_SOLICITED); // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); - handle.setId(getClientHandle().getId()); + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); try { @@ -228,8 +220,7 @@ public class COPSPdpMsgSender { COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle handle = new COPSHandle(); - handle.setId(getClientHandle().getId()); + final COPSHandle handle = new COPSHandle(getClientHandle().getId()); COPSDecisionMsg decisionMsg = new COPSDecisionMsg(); try { @@ -332,8 +323,7 @@ public class COPSPdpMsgSender { COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); // Decisions // @@ -379,8 +369,7 @@ public class COPSPdpMsgSender { COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); // Decisions // @@ -421,8 +410,7 @@ public class COPSPdpMsgSender { COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType()); // Client Handle with the same clientHandle as the request - COPSHandle clienthandle = new COPSHandle(); - clienthandle.setId(_handle.getId()); + final COPSHandle clienthandle = new COPSHandle(_handle.getId()); COPSSyncStateMsg msg = new COPSSyncStateMsg(); try { diff --git a/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpReqStateMan.java b/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpReqStateMan.java index 0698951..2de386c 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpReqStateMan.java +++ b/packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpReqStateMan.java @@ -6,24 +6,13 @@ package org.umu.cops.prpdp; +import org.umu.cops.stack.*; + import java.net.Socket; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; -import org.umu.cops.stack.COPSClientSI; -import org.umu.cops.stack.COPSContext; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDeleteMsg; -import org.umu.cops.stack.COPSError; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSHeader; -import org.umu.cops.stack.COPSPrObjBase; -import org.umu.cops.stack.COPSReportMsg; -import org.umu.cops.stack.COPSReportType; -import org.umu.cops.stack.COPSReqMsg; -import org.umu.cops.stack.COPSSyncStateMsg; - /** * State manager class for provisioning requests, at the PDP side. */ @@ -108,13 +97,8 @@ public class COPSPdpReqStateMan { * @param clientHandle Client handle */ public COPSPdpReqStateMan(short clientType, String clientHandle) { - // COPS Handle - _handle = new COPSHandle(); - COPSData id = new COPSData(clientHandle); - _handle.setId(id); - // client-type + _handle = new COPSHandle(new COPSData(clientHandle)); _clientType = clientType; - _status = ST_CREATE; } diff --git a/packetcable-driver/src/main/java/org/umu/cops/prpep/COPSPepReqStateMan.java b/packetcable-driver/src/main/java/org/umu/cops/prpep/COPSPepReqStateMan.java index c004e82..5d09425 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/prpep/COPSPepReqStateMan.java +++ b/packetcable-driver/src/main/java/org/umu/cops/prpep/COPSPepReqStateMan.java @@ -6,20 +6,13 @@ package org.umu.cops.prpep; +import org.umu.cops.stack.*; + import java.net.Socket; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; -import org.umu.cops.stack.COPSContext; -import org.umu.cops.stack.COPSData; -import org.umu.cops.stack.COPSDecision; -import org.umu.cops.stack.COPSDecisionMsg; -import org.umu.cops.stack.COPSError; -import org.umu.cops.stack.COPSHandle; -import org.umu.cops.stack.COPSPrObjBase; -import org.umu.cops.stack.COPSSyncStateMsg; - /** * COPSPepReqStateMan manages Request State using Client Handle (RFC 2748 pag. 21) * in PEP. @@ -131,11 +124,7 @@ public class COPSPepReqStateMan { * */ public COPSPepReqStateMan(short clientType, String clientHandle) { - // COPS Handle - _handle = new COPSHandle(); - COPSData id = new COPSData(clientHandle); - _handle.setId(id); - // client-type + _handle = new COPSHandle(new COPSData(clientHandle)); _clientType = clientType; _syncState = true; _status = ST_CREATE; diff --git a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSData.java b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSData.java index 7b7ab12..6b05a04 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSData.java +++ b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSData.java @@ -7,30 +7,61 @@ package org.umu.cops.stack; +import java.util.Arrays; /** * COPS Data * + * // TODO - determine a good description of this class + * * @version COPSData.java, v 1.00 2003 * */ public class COPSData { - private byte[] _dataBuf; - private int _dLen; + /** + * TODO - determine a good description for this attribute + */ + private final byte[] _dataBuf; + + /** + * TODO - determine a good description for this attribute + */ + private final int _dLen; + /** + * Default constructor + */ public COPSData() { _dataBuf = null; _dLen = 0; } - public COPSData(byte[] dPtr, int offset, int dLen) { + /** + * Constructor + * @param dPtr - the data + * @param offset - the byte offset + * @param dLen - the data length + * @throws java.lang.IllegalArgumentException + */ + public COPSData(final byte[] dPtr, final int offset, final int dLen) { + if (dPtr == null) throw new IllegalArgumentException("The data array must not be null"); + if (offset < 0) throw new IllegalArgumentException("The offset must not be < 0"); + if (dLen < 0) throw new IllegalArgumentException("The length < 0"); + if (dLen > dPtr.length - offset) + throw new IllegalArgumentException("The length must be less than the dPtr size less the offset"); + _dataBuf = new byte[dLen]; - System.arraycopy(dPtr,offset,_dataBuf,0,dLen); + System.arraycopy(dPtr, offset, _dataBuf, 0, dLen); _dLen = dLen; } - public COPSData(String data) { + /** + * Constructor + * @param data - the data as a String + */ + public COPSData(final String data) { + if (data == null) throw new IllegalArgumentException("Data string must not be null"); _dLen = data.getBytes().length; _dataBuf = new byte[_dLen]; System.arraycopy(data.getBytes(),0,_dataBuf,0,_dLen); @@ -38,9 +69,7 @@ public class COPSData { /** * Method getData - * * @return a byte[] - * */ public byte[] getData() { return _dataBuf; @@ -48,9 +77,7 @@ public class COPSData { /** * Method length - * * @return an int - * */ public int length() { return _dLen; @@ -58,21 +85,36 @@ public class COPSData { /** * Method str - * * @return a String - * */ public String str() { - return new String (_dataBuf); + if (_dataBuf == null) return ""; + else return new String (_dataBuf); } public String toString() { return str(); } - public boolean equals(Object obj) { - return (((COPSData) obj).toString().equals(str())); + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (!(o instanceof COPSData)) { + return false; + } + final COPSData copsData = (COPSData) o; + return _dLen == copsData._dLen && Arrays.equals(_dataBuf, copsData._dataBuf); + } + + @Override + public int hashCode() { + int result = _dataBuf != null ? Arrays.hashCode(_dataBuf) : 0; + result = 31 * result + _dLen; + return result; } + } diff --git a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSHandle.java b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSHandle.java index dc652e3..da4a5f2 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSHandle.java +++ b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSHandle.java @@ -40,107 +40,116 @@ import java.net.Socket; */ public class COPSHandle extends COPSObjBase { - private COPSObjHeader _objHdr; - private COPSData _id; - private COPSData _padding; + private final COPSObjHeader _objHdr; + private final COPSData _id; + private final COPSData _padding; - public COPSHandle() { + /** + * Constructor + * @param id - the identifier (must not be null) + * @throws java.lang.IllegalArgumentException when the id parameter is null + */ + public COPSHandle(final COPSData id) { + if (id == null) throw new IllegalArgumentException("COPSData must not be null"); _objHdr = new COPSObjHeader(); _objHdr.setCNum(COPSObjHeader.COPS_HANDLE); _objHdr.setCType((byte) 1); - _padding = new COPSData(); + _id = id; + if ((_id.length() % 4) != 0) { + final int padLen = 4 - (_id.length() % 4); + _padding = getPadding(padLen); + } else { + _padding = new COPSData(); + } + _objHdr.setDataLength((short) _id.length()); } /** - Parse data and create COPSHandle object + * Constructor + * @param dataPtr - the data to parse for setting this object's attributes */ - protected COPSHandle(byte[] dataPtr) { + protected COPSHandle(final byte[] dataPtr) { + if (dataPtr == null || dataPtr.length < 5) + throw new IllegalArgumentException("Data cannot be null or fewer than 5 bytes"); + _objHdr = new COPSObjHeader(); _objHdr.parse(dataPtr); - // _objHdr.checkDataLength(); //Get the length of data following the obj header - int dLen = _objHdr.getDataLength() - 4; - COPSData d = new COPSData (dataPtr, 4, dLen); - setId(d); - } - - /** - * Set handle value - * - * @param id a COPSData - * - */ - public void setId(COPSData id) { - _id = id; - if ((id.length() % 4) != 0) { - int padLen = 4 - (_id.length() % 4); + final int dLen = _objHdr.getDataLength() - 4; + _id = new COPSData(dataPtr, 4, dLen); + if ((_id.length() % 4) != 0) { + final int padLen = 4 - (_id.length() % 4); _padding = getPadding(padLen); + } else { + _padding = new COPSData(); } _objHdr.setDataLength((short) _id.length()); } /** * Returns size in number of octects, including header - * * @return a short - * */ public short getDataLength() { //Add the size of the header also - int lpadding = 0; + final int lpadding; if (_padding != null) lpadding = _padding.length(); + else lpadding = 0; return ((short) (_objHdr.getDataLength() + lpadding)); } /** * Get handle value - * * @return a COPSData - * */ public COPSData getId() { return _id; } - /** - * Always return true - * - * @return a boolean - * - */ + @Override public boolean isClientHandle() { return true; } - /** - * Write data in network byte order on a given network socket - * - * @param id a Socket - * - * @throws IOException - * - */ - public void writeData(Socket id) throws IOException { - _objHdr.writeData(id); + @Override + public void writeData(final Socket socket) throws IOException { + _objHdr.writeData(socket); - COPSUtil.writeData(id, _id.getData(), _id.length()); + COPSUtil.writeData(socket, _id.getData(), _id.length()); if (_padding != null) { - COPSUtil.writeData(id, _padding.getData(), _padding.length()); + COPSUtil.writeData(socket, _padding.getData(), _padding.length()); } } /** * Write an object textual description in the output stream - * * @param os an OutputStream - * * @throws IOException - * */ - public void dump(OutputStream os) throws IOException { + public void dump(final OutputStream os) throws IOException { _objHdr.dump(os); - os.write(new String("client-handle: " + _id.str() + "\n").getBytes()); + os.write(("client-handle: " + _id.str() + "\n").getBytes()); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof COPSHandle)) { + return false; + } + final COPSHandle that = (COPSHandle) o; + return _id.equals(that._id) && _objHdr.equals(that._objHdr); } + + @Override + public int hashCode() { + int result = _objHdr.hashCode(); + result = 31 * result + _id.hashCode(); + return result; + } + } diff --git a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjBase.java b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjBase.java index bd05be2..ecdd39a 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjBase.java +++ b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjBase.java @@ -13,40 +13,32 @@ import java.util.Arrays; /** * COPS Object * + * TODO - this should be an interface and all of these default return values can be dangerous * @version COPSObjBase.java, v 1.00 2003 * */ public abstract class COPSObjBase { /** * Add padding in the data, if the data does not fall on 32-bit boundary - * * @param len an int - * * @return a COPSData - * */ - static COPSData getPadding(int len) { + static COPSData getPadding(final int len) { byte[] padBuf = new byte[len]; Arrays.fill(padBuf, (byte) 0); - COPSData d = new COPSData(padBuf, 0, len); - return d; + return new COPSData(padBuf, 0, len); } /** - * Writes data to a given network socket - * + * Writes data to a given network _socket * @param id a Socket - * * @throws IOException - * */ public abstract void writeData(Socket id) throws IOException; /** * Method getDataLength - * * @return a short - * */ short getDataLength() { return 0; @@ -54,9 +46,7 @@ public abstract class COPSObjBase { /** * Method isCOPSHeader - * * @return a boolean - * */ boolean isCOPSHeader() { return false; @@ -64,9 +54,7 @@ public abstract class COPSObjBase { /** * Method isClientHandle - * * @return a boolean - * */ boolean isClientHandle() { return false; @@ -74,9 +62,7 @@ public abstract class COPSObjBase { /** * Method isContext - * * @return a boolean - * */ boolean isContext() { return false; @@ -84,9 +70,7 @@ public abstract class COPSObjBase { /** * Method isInterface - * * @return a boolean - * */ boolean isInterface() { return false; @@ -94,9 +78,7 @@ public abstract class COPSObjBase { /** * Method isDecision - * * @return a boolean - * */ boolean isDecision() { return false; @@ -104,9 +86,7 @@ public abstract class COPSObjBase { /** * Method isLocalDecision - * * @return a boolean - * */ boolean isLocalDecision() { return false; @@ -114,9 +94,7 @@ public abstract class COPSObjBase { /** * Method isReport - * * @return a boolean - * */ boolean isReport() { return false; @@ -124,9 +102,7 @@ public abstract class COPSObjBase { /** * Method isError - * * @return a boolean - * */ boolean isError() { return false; @@ -134,9 +110,7 @@ public abstract class COPSObjBase { /** * Method isTimer - * * @return a boolean - * */ boolean isTimer() { return false; @@ -144,9 +118,7 @@ public abstract class COPSObjBase { /** * Method isPepId - * * @return a boolean - * */ boolean isPepId() { return false; @@ -154,9 +126,7 @@ public abstract class COPSObjBase { /** * Method isReason - * * @return a boolean - * */ boolean isReason() { return false; @@ -164,9 +134,7 @@ public abstract class COPSObjBase { /** * Method isPdpAddress - * * @return a boolean - * */ boolean isPdpAddress() { return false; @@ -174,9 +142,7 @@ public abstract class COPSObjBase { /** * Method isClientSI - * * @return a boolean - * */ boolean isClientSI() { return false; @@ -184,13 +150,10 @@ public abstract class COPSObjBase { /** * Method isMessageIntegrity - * * @return a boolean - * */ boolean isMessageIntegrity() { return false; } -}; - +} diff --git a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjHeader.java b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjHeader.java index ae24233..64c1611 100644 --- a/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjHeader.java +++ b/packetcable-driver/src/main/java/org/umu/cops/stack/COPSObjHeader.java @@ -35,98 +35,100 @@ public class COPSObjHeader extends COPSObjBase { public final static byte COPS_ACCT_TIMER = 15; public final static byte COPS_MSG_INTEGRITY = 16; - private short _len; - private byte _cNum; - private byte _cType; + private transient short _len; + private transient byte _cNum; - public COPSObjHeader(byte cNum, byte cType) { + // TODO - This value may be better as an enumeration + private transient byte _cType; + + /** + * Constructor + * @param cNum - the cNum value + * @param cType - the cType value + */ + public COPSObjHeader(final byte cNum, final byte cType) { _len = 4; _cNum = cNum; _cType = cType; } + /** + * Default constructor + */ public COPSObjHeader() { _len = 4; _cNum = 0; _cType = 0; } - protected COPSObjHeader(byte[] data) { + protected COPSObjHeader(final byte[] data) { parse(data); } /** * Get the data length in number of octets - * * @return a short - * */ public short getDataLength() { return _len; - }; + } /** * Get the class information identifier cNum - * * @return a byte - * */ public byte getCNum() { return _cNum; - }; + } /** * Get the type per cNum - * * @return a byte - * */ public byte getCType() { return _cType; - }; + } /** * Get stringified CNum - * * @return a String - * */ public String getStrCNum() { switch (getCNum()) { - case COPS_HANDLE: - return ("Client-handle"); - case COPS_CONTEXT: - return ("Context"); - case COPS_ININTF: - return ("In-Interface"); - case COPS_OUTINTF: - return ("Out-Interface"); - case COPS_REASON_CODE: - return ("Reason"); - case COPS_DEC: - return ("Decision"); - case COPS_LPDP_DEC: - return ("Local-Decision"); - case COPS_ERROR: - return ("Error"); - case COPS_CSI: - return ("Client-SI"); - case COPS_KA: - return ("KA-timer"); - case COPS_PEPID: - return ("PEP-id"); - case COPS_RPT: - return ("Report"); - case COPS_PDP_REDIR: - return ("Redirect PDP addr"); - case COPS_LAST_PDP_ADDR: - return ("Last PDP addr"); - case COPS_ACCT_TIMER: - return ("Account-Timer"); - case COPS_MSG_INTEGRITY: - return ("Message-Integrity"); - default: - return ("Unknown"); + case COPS_HANDLE: + return ("Client-handle"); + case COPS_CONTEXT: + return ("Context"); + case COPS_ININTF: + return ("In-Interface"); + case COPS_OUTINTF: + return ("Out-Interface"); + case COPS_REASON_CODE: + return ("Reason"); + case COPS_DEC: + return ("Decision"); + case COPS_LPDP_DEC: + return ("Local-Decision"); + case COPS_ERROR: + return ("Error"); + case COPS_CSI: + return ("Client-SI"); + case COPS_KA: + return ("KA-timer"); + case COPS_PEPID: + return ("PEP-id"); + case COPS_RPT: + return ("Report"); + case COPS_PDP_REDIR: + return ("Redirect PDP addr"); + case COPS_LAST_PDP_ADDR: + return ("Last PDP addr"); + case COPS_ACCT_TIMER: + return ("Account-Timer"); + case COPS_MSG_INTEGRITY: + return ("Message-Integrity"); + default: + return ("Unknown"); } } @@ -134,71 +136,54 @@ public class COPSObjHeader extends COPSObjBase { * Set the obj length, the length is the length of the data following * the object header.The length of the object header (4 bytes) is added * to the length passed. - * * @param len a short - * */ - public void setDataLength(short len) { + public void setDataLength(final short len) { //Add the length of the header also _len = (short) (len + 4); } /** * Set the class of information cNum - * * @param cNum a byte - * */ - public void setCNum(byte cNum) { + public void setCNum(final byte cNum) { _cNum = cNum; - } ; + } /** * Set the type defined per cNum - * * @param cType a byte - * */ - public void setCType(byte cType) { + public void setCType(final byte cType) { _cType = cType; - } ; - - /** - * Method checkDataLength - * - * @throws COPSException - * - */ - public void checkDataLength() throws COPSException { - } /** - * Writes data to a given network socket - * + * Writes data to a given network _socket * @param id a Socket - * * @throws IOException - * */ - public void writeData(Socket id) throws IOException { - byte[] buf = new byte[4]; + public void writeData(final Socket id) throws IOException { + final byte[] buf = new byte[4]; buf[0] = (byte) (_len >> 8); buf[1] = (byte) _len; - buf[2] = (byte) _cNum; - buf[3] = (byte) _cType; + buf[2] = _cNum; + buf[3] = _cType; COPSUtil.writeData(id, buf, 4); } /** * Method parse - * * @param data a byte[] - * */ - public void parse(byte[] data) { + public void parse(final byte[] data) { + if (data == null || data.length < 4) + throw new IllegalArgumentException("Data cannot be null or fewer than 2 bytes"); + + // TODO - Determine what setting the _len value from the data buffer really means _len = 0; _len |= ((short) data[0]) << 8; _len |= ((short) data[1]) & 0xFF; @@ -208,18 +193,34 @@ public class COPSObjHeader extends COPSObjBase { /** * Write an object textual description in the output stream - * * @param os an OutputStream - * * @throws IOException - * */ - public void dump(OutputStream os) throws IOException { - os.write(new String("**" + getStrCNum() + "**" + "\n").getBytes()); - os.write(new String("Length: " + _len + "\n").getBytes()); - os.write(new String("C-num: " + _cNum + "\n").getBytes()); - os.write(new String("C-type: " + _cType + "\n").getBytes()); + public void dump(final OutputStream os) throws IOException { + os.write(("**" + getStrCNum() + "**" + "\n").getBytes()); + os.write(("Length: " + _len + "\n").getBytes()); + os.write(("C-num: " + _cNum + "\n").getBytes()); + os.write(("C-type: " + _cType + "\n").getBytes()); } + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (!(o instanceof COPSObjHeader)) { + return false; + } + final COPSObjHeader that = (COPSObjHeader) o; + return _cNum == that._cNum && _cType == that._cType && _len == that._len; + } + + @Override + public int hashCode() { + int result = (int) _len; + result = 31 * result + (int) _cNum; + result = 31 * result + (int) _cType; + return result; + } } diff --git a/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataFirstConstructorTest.java b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataFirstConstructorTest.java new file mode 100644 index 0000000..252814b --- /dev/null +++ b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataFirstConstructorTest.java @@ -0,0 +1,21 @@ +package org.umu.cops.stack; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for the first (default) constructor of the COPSData class. + * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSHandle had been + * released and my assumptions may be incorrect. + */ +public class COPSDataFirstConstructorTest { + + @Test + public void defaultConstructor() { + final COPSData data = new COPSData(); + Assert.assertEquals(null, data.getData()); + Assert.assertEquals(0, data.length()); + Assert.assertEquals("", data.str()); + Assert.assertTrue(data.equals(new COPSData())); + } +} diff --git a/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataSecondConstructorTest.java b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataSecondConstructorTest.java new file mode 100644 index 0000000..4bb2828 --- /dev/null +++ b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataSecondConstructorTest.java @@ -0,0 +1,129 @@ +package org.umu.cops.stack; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Tests for the second constructor of the COPSData class. + * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSHandle had been + * released and my assumptions may be incorrect. + */ +public class COPSDataSecondConstructorTest { + + final byte[] bytes = ("hello world").getBytes(); + + @Test(expected = IllegalArgumentException.class) + public void nullBytes() { + new COPSData(null, 0, 0); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyBytesWithNegOffsetZeroLength() { + new COPSData(new byte[]{}, -1, 0); + } + + @Test + public void emptyBytesWithZeroOffsetZeroLength() { + final COPSData data = new COPSData(new byte[]{}, 0, 0); + Assert.assertEquals(0, data.length()); + Assert.assertEquals(0, data.getData().length); + Assert.assertEquals("", data.str()); + + final COPSData eqHash = new COPSData(bytes, 1, 0); + final COPSData neHash = new COPSData(bytes, 2, 2); + + Assert.assertTrue(data.equals(eqHash)); + Assert.assertEquals(data.hashCode(), eqHash.hashCode()); + Assert.assertFalse(data.equals(neHash)); + Assert.assertNotEquals(data.hashCode(), neHash.hashCode()); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyBytesWithPosOffsetZeroLength() { + new COPSData(new byte[]{}, 1, 0); + } + + @Test(expected = IllegalArgumentException.class) + public void validBytesWithNegOffsetZeroLength() { + new COPSData(bytes, -1, 0); + } + + @Test + public void validBytesWithZeroOffsetZeroLength() { + final COPSData data = new COPSData(bytes, 0, 0); + Assert.assertEquals(0, data.length()); + Assert.assertEquals(0, data.getData().length); + Assert.assertEquals("", data.str()); + + final COPSData eqHash = new COPSData(bytes, 1, 0); + final COPSData neHash = new COPSData(bytes, 2, 2); + + Assert.assertTrue(data.equals(eqHash)); + Assert.assertEquals(data.hashCode(), eqHash.hashCode()); + Assert.assertFalse(data.equals(neHash)); + Assert.assertNotEquals(data.hashCode(), neHash.hashCode()); + } + + @Test(expected = IllegalArgumentException.class) + public void validBytesWithPosOffsetNegLength() { + new COPSData(bytes, 1, -1); + } + + @Test + public void validBytesWithPosOffsetZeroLength() { + final COPSData data = new COPSData(bytes, 1, 0); + Assert.assertEquals(0, data.length()); + Assert.assertEquals(0, data.getData().length); + Assert.assertEquals("", data.str()); + + final COPSData eqHash = new COPSData(bytes, 1, 0); + final COPSData neHash = new COPSData(bytes, 2, 2); + + Assert.assertTrue(data.equals(eqHash)); + Assert.assertEquals(data.hashCode(), eqHash.hashCode()); + Assert.assertFalse(data.equals(neHash)); + Assert.assertNotEquals(data.hashCode(), neHash.hashCode()); + } + + @Test + public void validBytesWithPosOffsetPosLength() { + final COPSData data = new COPSData(bytes, 1, 1); + Assert.assertEquals(1, data.length()); + Assert.assertEquals(1, data.getData().length); + Assert.assertEquals(bytes[1], data.getData()[0]); + Assert.assertEquals("e", data.str()); + + final COPSData eqHash = new COPSData(bytes, 1, 1); + final COPSData neHash = new COPSData(bytes, 2, 2); + + Assert.assertTrue(data.equals(eqHash)); + Assert.assertEquals(data.hashCode(), eqHash.hashCode()); + Assert.assertFalse(data.equals(neHash)); + Assert.assertNotEquals(data.hashCode(), neHash.hashCode()); + } + + @Test(expected = IllegalArgumentException.class) + public void validBytesWithPosOffsetLengthTooLarge() { + new COPSData(bytes, 5, 8); + } + + @Test + public void likelyScenario() { + final String expectedData = "lo wo"; + + final COPSData data = new COPSData(bytes, 3, 5); + Assert.assertTrue(Arrays.equals(expectedData.getBytes(), data.getData())); + Assert.assertEquals(expectedData.length(), data.length()); + Assert.assertEquals(expectedData, data.str()); + + final COPSData eqHash = new COPSData(bytes, 3, 5); + final COPSData neHash = new COPSData(bytes, 2, 4); + + Assert.assertTrue(data.equals(eqHash)); + Assert.assertEquals(data.hashCode(), eqHash.hashCode()); + Assert.assertFalse(data.equals(neHash)); + Assert.assertNotEquals(data.hashCode(), neHash.hashCode()); + } +} diff --git a/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataThirdConstructorTest.java b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataThirdConstructorTest.java new file mode 100644 index 0000000..f3ae4d6 --- /dev/null +++ b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSDataThirdConstructorTest.java @@ -0,0 +1,53 @@ +package org.umu.cops.stack; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Tests for the third constructor of the COPSData class. + * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSHandle had been + * released and my assumptions may be incorrect. + */ +public class COPSDataThirdConstructorTest { + + @Test(expected = IllegalArgumentException.class) + public void nullString() { + new COPSData(null); + } + + @Test + public void emptyString() { + final COPSData data = new COPSData(""); + Assert.assertEquals(0, data.length()); + Assert.assertEquals(0, data.getData().length); + Assert.assertEquals("", data.str()); + + final COPSData eqHash = new COPSData(""); + final COPSData neHash = new COPSData("foo"); + + Assert.assertTrue(data.equals(eqHash)); + Assert.assertEquals(data.hashCode(), eqHash.hashCode()); + Assert.assertFalse(data.equals(neHash)); + Assert.assertNotEquals(data.hashCode(), neHash.hashCode()); + } + + @Test + public void likelyScenario() { + final String theData = "Hello World"; + + final COPSData data = new COPSData(theData); + Assert.assertTrue(Arrays.equals(theData.getBytes(), data.getData())); + Assert.assertEquals(theData.length(), data.length()); + Assert.assertEquals(theData, data.str()); + + final COPSData eqHash = new COPSData(theData); + final COPSData neHash = new COPSData("foo"); + + Assert.assertTrue(data.equals(eqHash)); + Assert.assertEquals(data.hashCode(), eqHash.hashCode()); + Assert.assertFalse(data.equals(neHash)); + Assert.assertNotEquals(data.hashCode(), neHash.hashCode()); + } +} diff --git a/packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleFirstConstructorTest.java b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleFirstConstructorTest.java new file mode 100644 index 0000000..c583081 --- /dev/null +++ b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleFirstConstructorTest.java @@ -0,0 +1,63 @@ +package org.umu.cops.stack; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for the first constructor of the COPSHandle class. + * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSHandle had been + * released and my assumptions may be incorrect. + */ +public class COPSHandleFirstConstructorTest { + + @Test (expected = IllegalArgumentException.class) + public void constructWithNullId() { + final COPSData id = null; + new COPSHandle(id); + } + + @Test + public void constructWithDefaultId() { + final COPSData id = new COPSData(); + final COPSHandle handle = new COPSHandle(id); + Assert.assertEquals(4, handle.getDataLength()); + Assert.assertTrue(id.equals(handle.getId())); + validateSuperMethods(handle); + + final COPSHandle eqHash = new COPSHandle(id); + Assert.assertTrue(handle.equals(eqHash)); + Assert.assertEquals(handle.hashCode(), eqHash.hashCode()); + } + + @Test + public void constructWithData() { + final COPSData id = new COPSData("12345678"); + final COPSHandle handle = new COPSHandle(id); + + // TODO - need to determine if 12 is indeed correct given the value "123456778" + Assert.assertEquals(12, handle.getDataLength()); + Assert.assertTrue(id.equals(handle.getId())); + validateSuperMethods(handle); + + final COPSHandle eqHash = new COPSHandle(id); + Assert.assertTrue(handle.equals(eqHash)); + Assert.assertEquals(handle.hashCode(), eqHash.hashCode()); + } + + private void validateSuperMethods(final COPSHandle handle) { + Assert.assertTrue(handle.isClientHandle()); + Assert.assertFalse(handle.isMessageIntegrity()); + Assert.assertFalse(handle.isClientSI()); + Assert.assertFalse(handle.isContext()); + Assert.assertFalse(handle.isCOPSHeader()); + Assert.assertFalse(handle.isDecision()); + Assert.assertFalse(handle.isError()); + Assert.assertFalse(handle.isInterface()); + Assert.assertFalse(handle.isLocalDecision()); + Assert.assertFalse(handle.isPdpAddress()); + Assert.assertFalse(handle.isPepId()); + Assert.assertFalse(handle.isReason()); + Assert.assertFalse(handle.isReport()); + Assert.assertFalse(handle.isTimer()); + } +} diff --git a/packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleSecondConstructorTest.java b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleSecondConstructorTest.java new file mode 100644 index 0000000..c863f15 --- /dev/null +++ b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSHandleSecondConstructorTest.java @@ -0,0 +1,41 @@ +package org.umu.cops.stack; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for the second constructor of the COPSHandle class. + * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSHandle had been + * released and my assumptions may be incorrect. + */ +public class COPSHandleSecondConstructorTest { + + @Test(expected = IllegalArgumentException.class) + public void nullBytes() { + final byte[] bytes = null; + new COPSHandle(bytes); + } + + @Test(expected = IllegalArgumentException.class) + public void emptyBytes() { + new COPSHandle(new byte[]{}); + } + + @Test(expected = IllegalArgumentException.class) + public void oneByte() { + new COPSHandle(new byte[]{(byte)1}); + } + +// @Test + // TODO - Determine what values this byte array should contain??? As written, an exception is thrown in COPSData + // TODO - when attempting to set the _len attribute. + public void fourBytes() { + final byte[] bytes = new byte[]{(byte)1, (byte)2, (byte)3, (byte)4, (byte)5}; + final COPSHandle handle = new COPSHandle(bytes); + Assert.assertEquals(4, handle.getDataLength()); + Assert.fail("Implement me"); + } + + // TODO - implement more tests once we can determine how exactly to properly instantiate a COPSHandle object + +} diff --git a/packetcable-driver/src/test/java/org/umu/cops/stack/COPSObjHeaderTest.java b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSObjHeaderTest.java new file mode 100644 index 0000000..d4daa8a --- /dev/null +++ b/packetcable-driver/src/test/java/org/umu/cops/stack/COPSObjHeaderTest.java @@ -0,0 +1,55 @@ +package org.umu.cops.stack; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for the proper construction of the COPSObjHeader class as well as changes to the transient state. + */ +public class COPSObjHeaderTest { + + @Test + public void defaultConstructor() { + final COPSObjHeader header = new COPSObjHeader(); + Assert.assertEquals(0, header.getCNum()); + Assert.assertEquals(0, header.getCType()); + Assert.assertEquals(4, header.getDataLength()); + + final COPSObjHeader eqHash = new COPSObjHeader(); + Assert.assertTrue(header.equals(eqHash)); + Assert.assertEquals(header.hashCode(), eqHash.hashCode()); + } + + @Test + public void settersWithDefaultConstructor() { + final COPSObjHeader header = new COPSObjHeader(); + header.setDataLength((short)5); + header.setCNum((byte) 8); + header.setCType((byte) 7); + Assert.assertEquals(9, header.getDataLength()); + Assert.assertEquals(8, header.getCNum()); + Assert.assertEquals(7, header.getCType()); + + final COPSObjHeader eqHash = new COPSObjHeader((byte)8, (byte)7); + eqHash.setDataLength((short)5); + Assert.assertTrue(header.equals(eqHash)); + Assert.assertEquals(header.hashCode(), eqHash.hashCode()); + } + + @Test + public void cNumTypeConstructor() { + final COPSObjHeader header = new COPSObjHeader((byte)5, (byte)6); + Assert.assertEquals(5, header.getCNum()); + Assert.assertEquals(6, header.getCType()); + Assert.assertEquals(4, header.getDataLength()); + + final COPSObjHeader eqHash = new COPSObjHeader((byte)5, (byte)6); + Assert.assertTrue(header.equals(eqHash)); + Assert.assertEquals(header.hashCode(), eqHash.hashCode()); + + header.setCNum((byte)7); + header.setCType((byte)8); + Assert.assertEquals(7, header.getCNum()); + Assert.assertEquals(8, header.getCType()); + } +} diff --git a/packetcable-provider/src/main/java/org/opendaylight/controller/packetcable/provider/OpendaylightPacketcableProvider.java b/packetcable-provider/src/main/java/org/opendaylight/controller/packetcable/provider/OpendaylightPacketcableProvider.java index 3aa978d..c330ef3 100644 --- a/packetcable-provider/src/main/java/org/opendaylight/controller/packetcable/provider/OpendaylightPacketcableProvider.java +++ b/packetcable-provider/src/main/java/org/opendaylight/controller/packetcable/provider/OpendaylightPacketcableProvider.java @@ -1,14 +1,10 @@ package org.opendaylight.controller.packetcable.provider; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicReference; - +import com.google.common.base.Optional; +import com.google.common.collect.Lists; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; @@ -18,20 +14,12 @@ import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.packetcable.provider.processors.PCMMDataProcessor; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration; import org.opendaylight.controller.sal.binding.api.BindingAwareProvider; import org.opendaylight.controller.sal.binding.api.NotificationProviderService; import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.*; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.traffic.profile.rev140908.TrafficProfileBestEffortAttributes; @@ -48,12 +36,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeCon import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; -import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.broker.rev140909.CmtsAdded; -import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.broker.rev140909.CmtsAddedBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.broker.rev140909.CmtsRemoved; -import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.broker.rev140909.CmtsRemovedBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.broker.rev140909.CmtsUpdated; -import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.broker.rev140909.CmtsUpdatedBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.broker.rev140909.*; import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.rev140909.CmtsCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.node.cmts.rev140909.nodes.node.CmtsNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140909.UdpMatchRangesRpcRemoveFlow; @@ -64,7 +47,6 @@ import org.opendaylight.yangtools.concepts.CompositeObjectRegistration.Composite import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.RpcService; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.pcmm.gates.IClassifier; @@ -72,11 +54,13 @@ import org.pcmm.gates.ITrafficProfile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Optional; -import com.google.common.collect.Lists; -import com.google.common.util.concurrent.CheckedFuture; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("unused") public class OpendaylightPacketcableProvider implements DataChangeListener, @@ -100,6 +84,7 @@ public class OpendaylightPacketcableProvider implements DataChangeListener, private PCMMDataProcessor pcmmDataProcessor; public OpendaylightPacketcableProvider() { + logger.info("Instantiating"); executor = Executors.newCachedThreadPool(); cmtsInstances = Lists.newArrayList(); pcmmDataProcessor = new PCMMDataProcessor(); @@ -118,6 +103,7 @@ public class OpendaylightPacketcableProvider implements DataChangeListener, */ @Override public void close() throws ExecutionException, InterruptedException { + logger.info("Closing"); executor.shutdown(); if (dataProvider != null) { for (Iterator> iter = cmtsInstances.iterator(); iter.hasNext();) { @@ -252,6 +238,7 @@ public class OpendaylightPacketcableProvider implements DataChangeListener, @Override public void onSessionInitiated(ProviderContext session) { + logger.info("Initiating session"); providerContext = session; notificationService = session.getSALService(NotificationProviderService.class); dataBroker = session.getSALService(DataBroker.class); -- 2.36.6