2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.pcmm.gates.impl;
7 import com.google.common.primitives.Bytes;
8 import org.pcmm.base.impl.PCMMBaseObject;
9 import org.pcmm.gates.ITrafficProfile;
11 import java.util.ArrayList;
12 import java.util.List;
15 * The Best Effort object defines the Traffic Profile associated with a gate through an upstream DOCSIS-specific
16 * parameterization scheme.
18 public class BestEffortService extends PCMMBaseObject implements ITrafficProfile {
20 public static final byte STYPE = 3;
21 // XXX -> 60=0x3C, 112 = 0x70, 164=0xA4
22 // Length = 44=0x2C, 80=0x50 or 116=0x74
24 public static final byte DEFAULT_TRAFFIC_PRIORITY = 0;
26 public static final int DEFAULT_MAX_TRAFFIC_BURST = 3044;
31 private final byte envelope;
34 * The authorized envelope. See BEEnvelope for description of the attributes. MUST NOT be NULL.
36 private final BEEnvelop authorizedEnvelop;
39 * The reserved envelope. See BEEnvelope for description of the attributes. CAN BE NULL.
41 private final BEEnvelop reservedEnvelop;
44 * The committed envelope. See BEEnvelope for description of the attributes. CAN BE NULL.
46 private final BEEnvelop committedEnvelop;
49 * General use constructor
50 * @param auth - the authorized envelope (required)
51 * @param reserved - the reserved envelope (optional)
52 * @param committed - the committed envelope (optional)
54 public BestEffortService(final BEEnvelop auth, final BEEnvelop reserved, final BEEnvelop committed) {
55 this(DEFAULT_ENVELOP, auth, reserved, committed);
59 * Constructor generally used for byte parsing only.
60 * @param envelope - the envelope value
61 * @param auth - the authorized envelope (required)
62 * @param reserved - the reserved envelope (optional)
63 * @param committed - the committed envelope (optional)
65 protected BestEffortService(final byte envelope, final BEEnvelop auth, final BEEnvelop reserved,
66 final BEEnvelop committed) {
67 super(SNum.TRAFFIC_PROFILE, STYPE);
68 if (auth == null) throw new IllegalArgumentException("Authorized envelope must not be null");
70 // TODO - Cannot figure out any other means to parse the bytes unless this is true. Determine if correct???
71 if (reserved == null && committed != null)
72 throw new IllegalArgumentException("Cannot have a committed envelope without a reserved");
74 this.envelope = envelope;
75 this.authorizedEnvelop = auth;
76 this.reservedEnvelop = reserved;
77 this.committedEnvelop = committed;
81 public byte getEnvelop() {
86 public BEEnvelop getAuthorizedEnvelop() {
87 return authorizedEnvelop;
90 public BEEnvelop getReservedEnvelop() {
91 return reservedEnvelop;
94 public BEEnvelop getCommittedEnvelop() {
95 return committedEnvelop;
99 public byte[] getBytes() {
100 final List<Byte> byteList = new ArrayList<>();
101 byteList.addAll(Bytes.asList(envelope, (byte) 0, (byte) 0, (byte) 0));
102 byteList.addAll(authorizedEnvelop.getBytes());
103 if (reservedEnvelop != null) byteList.addAll(reservedEnvelop.getBytes());
104 if (committedEnvelop != null) byteList.addAll(committedEnvelop.getBytes());
105 return Bytes.toArray(byteList);
109 public boolean equals(final Object o) {
113 if (!(o instanceof BestEffortService)) {
116 if (!super.equals(o)) {
119 final BestEffortService that = (BestEffortService) o;
120 return envelope == that.envelope && authorizedEnvelop.equals(that.authorizedEnvelop) &&
121 !(reservedEnvelop != null ? !reservedEnvelop.equals(that.reservedEnvelop) :
122 that.reservedEnvelop != null) &&
123 !(committedEnvelop != null ? !committedEnvelop.equals(that.committedEnvelop) :
124 that.committedEnvelop != null);
129 public int hashCode() {
130 int result = super.hashCode();
131 result = 31 * result + (int) envelope;
132 result = 31 * result + authorizedEnvelop.hashCode();
133 result = 31 * result + (reservedEnvelop != null ? reservedEnvelop.hashCode() : 0);
134 result = 31 * result + (committedEnvelop != null ? committedEnvelop.hashCode() : 0);
139 * Returns a BestEffortService object from a byte array
140 * @param data - the data to parse
141 * @return - the object or null if cannot be parsed
142 * TODO - make me more robust as RuntimeExceptions can be thrown here.
144 public static BestEffortService parse(final byte[] data) {
145 final List<Byte> bytes = Bytes.asList(data);
146 bytes.subList(0, 51);
147 if (data.length >= 56 && data.length < 108)
148 return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))), null, null);
149 else if (data.length >= 108 && data.length < 160)
150 return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))),
151 BEEnvelop.parse(Bytes.toArray(bytes.subList(56, 108))), null);
152 else if (data.length >= 160)
153 return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))),
154 BEEnvelop.parse(Bytes.toArray(bytes.subList(56, 108))),
155 BEEnvelop.parse(Bytes.toArray(bytes.subList(108, 160))));