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 com.google.common.primitives.Bytes;
12 import org.pcmm.base.impl.PCMMBaseObject;
13 import org.pcmm.gates.ITrafficProfile;
15 import java.util.ArrayList;
16 import java.util.List;
19 * The Best Effort object defines the Traffic Profile associated with a gate through an upstream DOCSIS-specific
20 * parameterization scheme.
22 public class BestEffortService extends PCMMBaseObject implements ITrafficProfile {
24 public static final byte STYPE = 3;
25 // XXX -> 60=0x3C, 112 = 0x70, 164=0xA4
26 // Length = 44=0x2C, 80=0x50 or 116=0x74
28 public static final byte DEFAULT_TRAFFIC_PRIORITY = 0;
30 public static final int DEFAULT_MAX_TRAFFIC_BURST = 3044;
35 private final byte envelope;
38 * The authorized envelope. See BEEnvelope for description of the attributes. MUST NOT be NULL.
40 private final BEEnvelop authorizedEnvelop;
43 * The reserved envelope. See BEEnvelope for description of the attributes. CAN BE NULL.
45 private final BEEnvelop reservedEnvelop;
48 * The committed envelope. See BEEnvelope for description of the attributes. CAN BE NULL.
50 private final BEEnvelop committedEnvelop;
53 * General use constructor
54 * @param auth - the authorized envelope (required)
55 * @param reserved - the reserved envelope (optional)
56 * @param committed - the committed envelope (optional)
58 public BestEffortService(final BEEnvelop auth, final BEEnvelop reserved, final BEEnvelop committed) {
59 this(DEFAULT_ENVELOP, auth, reserved, committed);
63 * Constructor generally used for byte parsing only.
64 * @param envelope - the envelope value
65 * @param auth - the authorized envelope (required)
66 * @param reserved - the reserved envelope (optional)
67 * @param committed - the committed envelope (optional)
69 protected BestEffortService(final byte envelope, final BEEnvelop auth, final BEEnvelop reserved,
70 final BEEnvelop committed) {
71 super(SNum.TRAFFIC_PROFILE, STYPE);
72 if (auth == null) throw new IllegalArgumentException("AUTHORIZED envelope must not be null");
74 // TODO - Cannot figure out any other means to parse the bytes unless this is true. Determine if correct???
75 if (reserved == null && committed != null)
76 throw new IllegalArgumentException("Cannot have a committed envelope without a reserved");
78 this.envelope = envelope;
79 this.authorizedEnvelop = auth;
80 this.reservedEnvelop = reserved;
81 this.committedEnvelop = committed;
85 public byte getEnvelop() {
90 public BEEnvelop getAuthorizedEnvelop() {
91 return authorizedEnvelop;
94 public BEEnvelop getReservedEnvelop() {
95 return reservedEnvelop;
98 public BEEnvelop getCommittedEnvelop() {
99 return committedEnvelop;
103 public byte[] getBytes() {
104 final List<Byte> byteList = new ArrayList<>();
105 byteList.addAll(Bytes.asList(envelope, (byte) 0, (byte) 0, (byte) 0));
106 byteList.addAll(authorizedEnvelop.getBytes());
107 if (reservedEnvelop != null) byteList.addAll(reservedEnvelop.getBytes());
108 if (committedEnvelop != null) byteList.addAll(committedEnvelop.getBytes());
109 return Bytes.toArray(byteList);
113 public boolean equals(final Object o) {
117 if (!(o instanceof BestEffortService)) {
120 if (!super.equals(o)) {
123 final BestEffortService that = (BestEffortService) o;
124 return envelope == that.envelope && authorizedEnvelop.equals(that.authorizedEnvelop) &&
125 !(reservedEnvelop != null ? !reservedEnvelop.equals(that.reservedEnvelop) :
126 that.reservedEnvelop != null) &&
127 !(committedEnvelop != null ? !committedEnvelop.equals(that.committedEnvelop) :
128 that.committedEnvelop != null);
133 public int hashCode() {
134 int result = super.hashCode();
135 result = 31 * result + (int) envelope;
136 result = 31 * result + authorizedEnvelop.hashCode();
137 result = 31 * result + (reservedEnvelop != null ? reservedEnvelop.hashCode() : 0);
138 result = 31 * result + (committedEnvelop != null ? committedEnvelop.hashCode() : 0);
143 * Returns a BestEffortService object from a byte array
144 * @param data - the data to parse
145 * @return - the object or null if cannot be parsed
146 * TODO - make me more robust as RuntimeExceptions can be thrown here.
148 public static BestEffortService parse(final byte[] data) {
149 final List<Byte> bytes = Bytes.asList(data);
150 bytes.subList(0, 51);
151 if (data.length >= 56 && data.length < 108)
152 return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))), null, null);
153 else if (data.length >= 108 && data.length < 160)
154 return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))),
155 BEEnvelop.parse(Bytes.toArray(bytes.subList(56, 108))), null);
156 else if (data.length >= 160)
157 return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))),
158 BEEnvelop.parse(Bytes.toArray(bytes.subList(56, 108))),
159 BEEnvelop.parse(Bytes.toArray(bytes.subList(108, 160))));