528bb2357aff19007a5268ead102f053528d3bd5
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / DOCSISServiceClassNameTrafficProfile.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.ITrafficProfile;
9
10 import java.util.Arrays;
11
12 /**
13  * The DOCSIS Service Class Name object defines the preconfigured Service Class Name associated with a Gate.
14  *
15  */
16 public class DOCSISServiceClassNameTrafficProfile extends PCMMBaseObject implements ITrafficProfile {
17
18     public static final byte STYPE = 2;
19     public static final int SCN_MAX_LEN = 16;
20
21     /**
22      * The Service Class Name. REQUIRED and length must be >= 2 and <= 16 characters
23      */
24     private final String scnName;
25
26     /**
27      * The envelope
28      */
29     private final byte envelope;
30
31     /**
32      * Constructor using the default envelope value
33      * @param scnName - the service class name (required characters >=2 && <=16
34      */
35     public DOCSISServiceClassNameTrafficProfile(final String scnName) {
36         this(DEFAULT_ENVELOP, scnName);
37     }
38
39     /**
40      * Constructor to set all values
41      * @param envelope - the envelope value
42      * @param scnName - the service class name (required number of characters >=2 && <=16)
43      */
44     protected DOCSISServiceClassNameTrafficProfile(final byte envelope, final String scnName) {
45         super(SNum.TRAFFIC_PROFILE, STYPE);
46         if (scnName == null || scnName.length() < 2 || scnName.length() > SCN_MAX_LEN)
47             throw new IllegalArgumentException("Service class name must be between 2-16 characters");
48         this.scnName = scnName;
49         this.envelope = envelope;
50     }
51
52     @Override
53     public byte getEnvelop() {
54         return envelope;
55     }
56
57     /**
58      * Returns the service class name value
59      * @return - the SCN value
60      */
61     public String getScnName() {
62         return scnName;
63     }
64
65     @Override
66     protected byte[] getBytes() {
67         final int padLen;
68         if ((scnName.length() % 4) != 0) padLen = 4 - (scnName.length() % 4);
69         else padLen = 0;
70
71         final byte[] data = new byte[4 + scnName.getBytes().length + padLen];
72         Arrays.fill(data, (byte) 0);
73         data[0] = envelope;
74
75         System.arraycopy(scnName.getBytes(), 0, data, 4, scnName.getBytes().length);
76         return data;
77     }
78
79     @Override
80     public boolean equals(final Object o) {
81         if (this == o) {
82             return true;
83         }
84         if (!(o instanceof DOCSISServiceClassNameTrafficProfile)) {
85             return false;
86         }
87         if (!super.equals(o)) {
88             return false;
89         }
90         final DOCSISServiceClassNameTrafficProfile that = (DOCSISServiceClassNameTrafficProfile) o;
91         return envelope == that.envelope && scnName.equals(that.scnName);
92     }
93
94     @Override
95     public int hashCode() {
96         int result = super.hashCode();
97         result = 31 * result + scnName.hashCode();
98         result = 31 * result + (int) envelope;
99         return result;
100     }
101
102     /**
103      * Returns a DOCSISServiceClassNameTrafficProfile object from a byte array
104      * @param data - the data to parse
105      * @return - the object
106      * TODO - make me more robust as RuntimeExceptions can be thrown here.
107      */
108     public static DOCSISServiceClassNameTrafficProfile parse(final byte[] data) {
109         // variable i will denote the index where the data padding starts (if any) or the end of the array
110         int i = 4;
111         for (; i < data.length; i++) {
112             if (data[i] == 0) {
113                 break;
114             }
115         }
116         final int nameLength = i - 4;
117
118         final byte[] scnNameBytes = new byte[nameLength];
119         System.arraycopy(data, 4, scnNameBytes, 0, nameLength);
120         final String scnName = new String(scnNameBytes);
121         return new DOCSISServiceClassNameTrafficProfile(data[0], scnName);
122     }
123
124 }