29b2b64e54c52dc65d9da5a26b99b5e747e276cb
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / TransactionID.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.gates.impl;
6
7 import org.pcmm.base.impl.PCMMBaseObject;
8 import org.pcmm.gates.ITransactionID;
9 import org.umu.cops.stack.COPSMsgParser;
10
11 /**
12  * Implementation of the ITransactionID interface
13  */
14 public class TransactionID extends PCMMBaseObject implements ITransactionID {
15
16     /**
17      * The transaction identifier
18      */
19     private final short transId;
20
21     /**
22      * The gate command type
23      */
24     private final GateCommandType gateCommandType;
25
26     /**
27      * Constructor
28      * @param transId - the transaction identifier
29      * @param gateCommandType - the gate command type
30      */
31     public TransactionID(final short transId, final GateCommandType gateCommandType) {
32         super(SNum.TRANSACTION_ID, STYPE);
33         if (gateCommandType == null)
34             throw new IllegalArgumentException("Invalid gate command type");
35         this.transId = transId;
36         this.gateCommandType = gateCommandType;
37     }
38
39     @Override
40     public short getTransactionIdentifier() {
41         return transId;
42     }
43
44     @Override
45     public GateCommandType getGateCommandType() {
46         return gateCommandType;
47     }
48
49     @Override
50     protected byte[] getBytes() {
51         final byte[] transIdBytes = COPSMsgParser.shortToBytes(transId);
52         final byte[] data = new byte[transIdBytes.length + transIdBytes.length];
53         System.arraycopy(transIdBytes, 0, data, 0, transIdBytes.length);
54
55         final byte[] gateCmdBytes = COPSMsgParser.shortToBytes(gateCommandType.getValue());
56         System.arraycopy(gateCmdBytes, 0, data, transIdBytes.length, gateCmdBytes.length);
57         return data;
58     }
59
60     /**
61      * Returns a TransactionID object from a byte array
62      * @param data - the data to parse
63      * @return - the object
64      * TODO - make me more robust as RuntimeExceptions can be thrown here.
65      */
66     public static TransactionID parse(final byte[] data) {
67         return new TransactionID(COPSMsgParser.bytesToShort(data[0], data[1]),
68                 GateCommandType.valueOf(COPSMsgParser.bytesToShort(data[2], data[3])));
69     }
70 }