Refactor PCMM aspects of COPS message data objects.
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / objects / MMVersionInfo.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.objects;
6
7 import org.pcmm.base.impl.PCMMBaseObject;
8 import org.umu.cops.stack.COPSMsgParser;
9
10 /**
11  * The Version Info object is used to enable Multimedia applications to adapt their interactions with other devices so
12  * that interoperability can be achieved between products supporting different protocol versions. Both the Major
13  * Version Number and the Minor Version Number are 2 byte unsigned integers. Both the PDP and the PEP must include this
14  * object as specified in Section 6.5.1
15  */
16 public class MMVersionInfo extends PCMMBaseObject {
17
18     public static final short DEFAULT_MAJOR_VERSION_INFO = (short) 5;
19     public static final short DEFAULT_MINOR_VERSION_INFO = (short) 0;
20
21     /**
22      * The major version number
23      */
24     private final short majorVersionNB;
25
26     /**
27      * The minor version number
28      */
29     private final short minorVersionNB;
30
31     /**
32      * Constructor
33      * @param majorVersionNB - the major version number
34      * @param minorVersionNB - the minor version number
35      */
36     public MMVersionInfo(short majorVersionNB, short minorVersionNB) {
37         super(SNum.VERSION_INFO, (byte)1);
38         this.majorVersionNB = majorVersionNB;
39         this.minorVersionNB = minorVersionNB;
40     }
41
42     /**
43      * @return the majorVersionNB
44      */
45     public short getMajorVersionNB() {
46         return majorVersionNB;
47     }
48
49     /**
50      * @return the minorVersionNB
51      */
52     public short getMinorVersionNB() {
53         return minorVersionNB;
54     }
55
56     @Override
57     protected byte[] getBytes() {
58         final byte[] majVerBytes = COPSMsgParser.shortToBytes(majorVersionNB);
59         final byte[] minVerBytes = COPSMsgParser.shortToBytes(minorVersionNB);
60         final byte[] data = new byte[majVerBytes.length + minVerBytes.length];
61         System.arraycopy(majVerBytes, 0, data, 0, majVerBytes.length);
62         System.arraycopy(minVerBytes, 0, data, majVerBytes.length, minVerBytes.length);
63         return data;
64     }
65
66     /**
67      * Returns an MMVersionInfo object from a byte array
68      * @param data - the data to parse
69      * @return - the object
70      * TODO - make me more robust as RuntimeExceptions can be thrown here.
71      */
72     public static MMVersionInfo parse(final byte[] data) {
73         return new MMVersionInfo(COPSMsgParser.bytesToShort(data[0], data[1]),
74                 COPSMsgParser.bytesToShort(data[2], data[3]));
75     }
76
77 }