c3ab8b93fb523928f68baebec181b597ea63b8c2
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / DOCSISServiceClassNameTrafficProfile.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.ITrafficProfile;
13
14 import java.util.Arrays;
15
16 /**
17  * The DOCSIS Service Class Name object defines the preconfigured Service Class Name associated with a Gate.
18  *
19  */
20 public class DOCSISServiceClassNameTrafficProfile extends PCMMBaseObject implements ITrafficProfile {
21
22     public static final byte STYPE = 2;
23     public static final int SCN_MAX_LEN = 16;
24
25     /**
26      * The Service Class Name. REQUIRED and length must be >= 2 and <= 16 characters
27      */
28     private final String scnName;
29
30     /**
31      * The envelope
32      */
33     private final byte envelope;
34
35     /**
36      * Constructor using the default envelope value
37      * @param scnName - the service class name (required characters >=2 && <=16
38      */
39     public DOCSISServiceClassNameTrafficProfile(final String scnName) {
40         this(DEFAULT_ENVELOP, scnName);
41     }
42
43     /**
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)
47      */
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;
54     }
55
56     @Override
57     public byte getEnvelop() {
58         return envelope;
59     }
60
61     /**
62      * Returns the service class name value
63      * @return - the SCN value
64      */
65     public String getScnName() {
66         return scnName;
67     }
68
69     @Override
70     protected byte[] getBytes() {
71         final int padLen;
72         if ((scnName.length() % 4) != 0) padLen = 4 - (scnName.length() % 4);
73         else padLen = 0;
74
75         final byte[] data = new byte[4 + scnName.getBytes().length + padLen];
76         Arrays.fill(data, (byte) 0);
77         data[0] = envelope;
78
79         System.arraycopy(scnName.getBytes(), 0, data, 4, scnName.getBytes().length);
80         return data;
81     }
82
83     @Override
84     public boolean equals(final Object o) {
85         if (this == o) {
86             return true;
87         }
88         if (!(o instanceof DOCSISServiceClassNameTrafficProfile)) {
89             return false;
90         }
91         if (!super.equals(o)) {
92             return false;
93         }
94         final DOCSISServiceClassNameTrafficProfile that = (DOCSISServiceClassNameTrafficProfile) o;
95         return envelope == that.envelope && scnName.equals(that.scnName);
96     }
97
98     @Override
99     public int hashCode() {
100         int result = super.hashCode();
101         result = 31 * result + scnName.hashCode();
102         result = 31 * result + (int) envelope;
103         return result;
104     }
105
106     /**
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.
111      */
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
114         int i = 4;
115         for (; i < data.length; i++) {
116             if (data[i] == 0) {
117                 break;
118             }
119         }
120         final int nameLength = i - 4;
121
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);
126     }
127
128 }