2 * Copyright (c) 2015 Cable Television Laboratories, Inc. and others. All rights reserved.
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
9 package org.pcmm.gates.impl;
11 import org.pcmm.base.impl.PCMMBaseObject;
12 import org.pcmm.gates.IPCMMError;
13 import org.umu.cops.stack.COPSMsgParser;
16 * Implementation of the IPCMMError interface
18 public class PCMMError extends PCMMBaseObject implements IPCMMError {
21 * The error code (cannot be NA)
23 private final ErrorCode errorCode;
26 * The error sub-code (defaults to NA)
28 private final short subErrCode;
31 * Constructor without a sub-code which will then be set to NA
32 * @param errorCode - the error code (required and not NA)
34 public PCMMError(final ErrorCode errorCode) {
35 this(errorCode, (short)0);
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)
43 public PCMMError(final ErrorCode errorCode, final short 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 this.subErrCode = subErrCode;
52 public ErrorCode getErrorCode() {
57 public short getErrorSubcode() {
64 * @see org.pcmm.gates.IPCError#getDescription()
67 public String getDescription() {
68 //Error-Subcode is a 2-byte unsigned integer field used to provide further information about the error.
69 // In the case of Error-Codes 6, 7 and 17, this 16-bit field MUST contain, as two 8-bit values the
70 // S-Num and S-Type of the object that is missing or in error.
76 byte[] sParts = COPSMsgParser.shortToBytes(subErrCode);
77 subcode = String.format("S-Num: %d, S-Type: %d", sParts[0], sParts[1]);
80 subcode = "Subcode: " + Integer.toHexString(subErrCode & 0xFFFF);
83 return "Error Code: " + errorCode.getCode() + " " + subcode + " " + errorCode.getDescription();
87 public String toString() {
88 return getDescription();
92 protected byte[] getBytes() {
93 final byte[] errorCodeBytes = COPSMsgParser.shortToBytes(errorCode.getCode());
94 final byte[] subErrCodeBytes = COPSMsgParser.shortToBytes(subErrCode);
95 final byte[] data = new byte[errorCodeBytes.length + subErrCodeBytes.length];
96 System.arraycopy(errorCodeBytes, 0, data, 0, errorCodeBytes.length);
97 System.arraycopy(subErrCodeBytes, 0, data, errorCodeBytes.length, subErrCodeBytes.length);
102 public boolean equals(final Object o) {
106 if (!(o instanceof PCMMError)) {
109 if (!super.equals(o)) {
112 final PCMMError pcmmError = (PCMMError) o;
113 return errorCode == pcmmError.errorCode && subErrCode == pcmmError.subErrCode;
117 public int hashCode() {
118 int result = super.hashCode();
119 result = 31 * result + errorCode.hashCode();
120 result = 31 * result + subErrCode;
125 * Returns a PCMMError object from a byte array
126 * @param data - the data to parse
127 * @return - the object
128 * TODO - make me more robust as RuntimeExceptions can be thrown here.
130 public static PCMMError parse(final byte[] data) {
131 return new PCMMError(ErrorCode.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])),
132 (COPSMsgParser.bytesToShort(data[2], data[3])));