2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.pcmm.gates.impl;
7 import org.pcmm.base.impl.PCMMBaseObject;
8 import org.pcmm.gates.ITrafficProfile;
10 import java.util.Arrays;
13 * The DOCSIS Service Class Name object defines the preconfigured Service Class Name associated with a Gate.
16 public class DOCSISServiceClassNameTrafficProfile extends PCMMBaseObject implements ITrafficProfile {
18 public static final byte STYPE = 2;
19 public static final int SCN_MAX_LEN = 16;
22 * The Service Class Name. REQUIRED and length must be >= 2 and <= 16 characters
24 private final String scnName;
29 private final byte envelope;
32 * Constructor using the default envelope value
33 * @param scnName - the service class name (required characters >=2 && <=16
35 public DOCSISServiceClassNameTrafficProfile(final String scnName) {
36 this(DEFAULT_ENVELOP, scnName);
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)
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;
53 public byte getEnvelop() {
58 * Returns the service class name value
59 * @return - the SCN value
61 public String getScnName() {
66 protected byte[] getBytes() {
68 if ((scnName.length() % 4) != 0) padLen = 4 - (scnName.length() % 4);
71 final byte[] data = new byte[4 + scnName.getBytes().length + padLen];
72 Arrays.fill(data, (byte) 0);
75 System.arraycopy(scnName.getBytes(), 0, data, 4, scnName.getBytes().length);
80 public boolean equals(final Object o) {
84 if (!(o instanceof DOCSISServiceClassNameTrafficProfile)) {
87 if (!super.equals(o)) {
90 final DOCSISServiceClassNameTrafficProfile that = (DOCSISServiceClassNameTrafficProfile) o;
91 return envelope == that.envelope && scnName.equals(that.scnName);
95 public int hashCode() {
96 int result = super.hashCode();
97 result = 31 * result + scnName.hashCode();
98 result = 31 * result + (int) envelope;
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.
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
111 for (; i < data.length; i++) {
116 final int nameLength = i - 4;
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);