Bump to odlparent 3.1.0 and yangtools 2.0.3
[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 short 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, (short)0);
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 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;
49     }
50
51     @Override
52     public ErrorCode getErrorCode() {
53         return errorCode;
54     }
55
56     @Override
57     public short getErrorSubcode() {
58         return subErrCode;
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.pcmm.gates.IPCError#getDescription()
65      */
66     @Override
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.
71         String subcode;
72         switch (errorCode) {
73             case MISSING_REQ_OBJ:
74             case INVALID_OBJ:
75             case INVALID_FIELD:
76                 byte[] sParts = COPSMsgParser.shortToBytes(subErrCode);
77                 subcode = String.format("S-Num: %d, S-Type: %d", sParts[0], sParts[1]);
78                 break;
79             default:
80                 subcode = "Subcode: " + Integer.toHexString(subErrCode & 0xFFFF);
81         }
82
83         return "Error Code: " + errorCode.getCode() + " " + subcode + "  " + errorCode.getDescription();
84     }
85
86     @Override
87     public String toString() {
88         return getDescription();
89     }
90
91     @Override
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);
98         return data;
99     }
100
101     @Override
102     public boolean equals(final Object o) {
103         if (this == o) {
104             return true;
105         }
106         if (!(o instanceof PCMMError)) {
107             return false;
108         }
109         if (!super.equals(o)) {
110             return false;
111         }
112         final PCMMError pcmmError = (PCMMError) o;
113         return errorCode == pcmmError.errorCode && subErrCode == pcmmError.subErrCode;
114     }
115
116     @Override
117     public int hashCode() {
118         int result = super.hashCode();
119         result = 31 * result + errorCode.hashCode();
120         result = 31 * result + subErrCode;
121         return result;
122     }
123
124     /**
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.
129      */
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])));
133
134     }
135
136 }