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.ITrafficProfile;
14 import java.util.Arrays;
17 * The DOCSIS Service Class Name object defines the preconfigured Service Class Name associated with a Gate.
20 public class DOCSISServiceClassNameTrafficProfile extends PCMMBaseObject implements ITrafficProfile {
22 public static final byte STYPE = 2;
23 public static final int SCN_MAX_LEN = 16;
26 * The Service Class Name. REQUIRED and length must be >= 2 and <= 16 characters
28 private final String scnName;
33 private final byte envelope;
36 * Constructor using the default envelope value
37 * @param scnName - the service class name (required characters >=2 && <=16
39 public DOCSISServiceClassNameTrafficProfile(final String scnName) {
40 this(DEFAULT_ENVELOP, scnName);
44 * Constructor to set all values
45 * @param envelope - the envelope value
46 * @param scnName - the service class name (required number of characters >=2 && <=16)
48 protected DOCSISServiceClassNameTrafficProfile(final byte envelope, final String scnName) {
49 super(SNum.TRAFFIC_PROFILE, STYPE);
50 if (scnName == null || scnName.length() < 2 || scnName.length() > SCN_MAX_LEN)
51 throw new IllegalArgumentException("Service class name must be between 2-16 characters");
52 this.scnName = scnName;
53 this.envelope = envelope;
57 public byte getEnvelop() {
62 * Returns the service class name value
63 * @return - the SCN value
65 public String getScnName() {
70 protected byte[] getBytes() {
72 if ((scnName.length() % 4) != 0) padLen = 4 - (scnName.length() % 4);
75 final byte[] data = new byte[4 + scnName.getBytes().length + padLen];
76 Arrays.fill(data, (byte) 0);
79 System.arraycopy(scnName.getBytes(), 0, data, 4, scnName.getBytes().length);
84 public boolean equals(final Object o) {
88 if (!(o instanceof DOCSISServiceClassNameTrafficProfile)) {
91 if (!super.equals(o)) {
94 final DOCSISServiceClassNameTrafficProfile that = (DOCSISServiceClassNameTrafficProfile) o;
95 return envelope == that.envelope && scnName.equals(that.scnName);
99 public int hashCode() {
100 int result = super.hashCode();
101 result = 31 * result + scnName.hashCode();
102 result = 31 * result + (int) envelope;
107 * Returns a DOCSISServiceClassNameTrafficProfile object from a byte array
108 * @param data - the data to parse
109 * @return - the object
110 * TODO - make me more robust as RuntimeExceptions can be thrown here.
112 public static DOCSISServiceClassNameTrafficProfile parse(final byte[] data) {
113 // variable i will denote the index where the data padding starts (if any) or the end of the array
115 for (; i < data.length; i++) {
120 final int nameLength = i - 4;
122 final byte[] scnNameBytes = new byte[nameLength];
123 System.arraycopy(data, 4, scnNameBytes, 0, nameLength);
124 final String scnName = new String(scnNameBytes);
125 return new DOCSISServiceClassNameTrafficProfile(data[0], scnName);