Refactor PCMM aspects of COPS message data objects.
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / PCMMError.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.IPCMMError;
9 import org.umu.cops.stack.COPSMsgParser;
10
11 /**
12  * Implementation of the IPCMMError interface
13  */
14 public class PCMMError extends PCMMBaseObject implements IPCMMError {
15
16     /**
17      * The error code (cannot be NA)
18      */
19     private final ErrorCode errorCode;
20
21     /**
22      * The error sub-code (defaults to NA)
23      */
24     private final ErrorCode subErrCode;
25
26     /**
27      * Constructor without a sub-code which will then be set to NA
28      * @param errorCode - the error code (required and not NA)
29      */
30     public PCMMError(final ErrorCode errorCode) {
31         this(errorCode, null);
32     }
33
34     /**
35      * Constructor with a sub-code
36      * @param errorCode - the error code (required and not NA)
37      * @param subErrCode - the sub-code (defaults to NA when null)
38      */
39     public PCMMError(final ErrorCode errorCode, final ErrorCode subErrCode) {
40         super(SNum.PCMM_ERROR, STYPE);
41         if (errorCode == null || errorCode.equals(ErrorCode.NA))
42             throw new IllegalArgumentException("ErrorCode is required and must not be NA");
43         this.errorCode = errorCode;
44         if (subErrCode == null) this.subErrCode = ErrorCode.NA;
45         else this.subErrCode = subErrCode;
46     }
47
48     @Override
49     public ErrorCode getErrorCode() {
50         return errorCode;
51     }
52
53     @Override
54     public ErrorCode getErrorSubcode() {
55         return subErrCode;
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.pcmm.gates.IPCError#getDescription()
62      */
63     @Override
64     public String getDescription() {
65         String hex = Integer.toHexString(subErrCode.getCode() & 0xFFFF);
66         return "Error Code: " + errorCode.getCode() + " Error Subcode : " + hex + "  " + errorCode.getDescription();
67     }
68
69     @Override
70     public String toString() {
71         return getDescription();
72     }
73
74     @Override
75     protected byte[] getBytes() {
76         final byte[] errorCodeBytes = COPSMsgParser.shortToBytes(errorCode.getCode());
77         final byte[] subErrCodeBytes = COPSMsgParser.shortToBytes(subErrCode.getCode());
78         final byte[] data = new byte[errorCodeBytes.length + subErrCodeBytes.length];
79         System.arraycopy(errorCodeBytes, 0, data, 0, errorCodeBytes.length);
80         System.arraycopy(subErrCodeBytes, 0, data, errorCodeBytes.length, subErrCodeBytes.length);
81         return data;
82     }
83
84     @Override
85     public boolean equals(final Object o) {
86         if (this == o) {
87             return true;
88         }
89         if (!(o instanceof PCMMError)) {
90             return false;
91         }
92         if (!super.equals(o)) {
93             return false;
94         }
95         final PCMMError pcmmError = (PCMMError) o;
96         return errorCode == pcmmError.errorCode && subErrCode == pcmmError.subErrCode;
97     }
98
99     @Override
100     public int hashCode() {
101         int result = super.hashCode();
102         result = 31 * result + errorCode.hashCode();
103         result = 31 * result + subErrCode.hashCode();
104         return result;
105     }
106
107     /**
108      * Returns a PCMMError object from a byte array
109      * @param data - the data to parse
110      * @return - the object
111      * TODO - make me more robust as RuntimeExceptions can be thrown here.
112      */
113     public static PCMMError parse(final byte[] data) {
114         return new PCMMError(ErrorCode.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])),
115                 ErrorCode.valueOf((COPSMsgParser.bytesToShort(data[2], data[3]))));
116
117     }
118
119 }