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