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.IGateSpec;
13 import org.pcmm.gates.ISessionClassID;
14 import org.umu.cops.stack.COPSMsgParser;
17 * Implementation of the IGateSpec interface
19 public class GateSpec extends PCMMBaseObject implements IGateSpec {
22 * The gate's direction
24 private Direction direction;
27 * The DSCP/TOS Overwrite is a 1-byte bit field [8] defined by the following alternative structures, depending upon
28 network management strategy.
30 * When enabled, the CMTS must mark the packets traversing the CMTS DSCP/TOS value
32 private final byte tosOverwrite;
35 * tosOverwrite field with the TOS mask is used to identify particular bits within the IPv4 DSCP/TOS byte
37 private final byte tosMask;
40 * Session Class ID identifies the proper admission control policy or parameters to be applied for this gate
42 private final SessionClassID sessionClassID;
45 * Timer value in seconds. Value of 0 indicates that the CMTS provisioned value for the timer MUST be used.
47 private final short timer1;
50 * DOCSIS Admitted timer in seconds
52 private final short timer2;
55 * DOCSIS Active timer in seconds
57 private final short timer3;
60 * The fourth timer value in seconds
62 private final short timer4;
66 * @param direction - the gate direction
67 * @param tosOverwrite - ENABLE/DISABLE
68 * @param tosMask - the mask
70 public GateSpec(final Direction direction, final byte tosOverwrite, final byte tosMask) {
71 this(direction, tosOverwrite, tosMask, new SessionClassID((byte) 0), (short) 0, (short) 0, (short) 0, (short) 0);
75 * Constructor generally for use when parsing a byte array to create an instance of this object.
76 * @param direction - the gate direction
77 * @param tosOverwrite - ENABLE/DISABLE
78 * @param tosMask - the mask
79 * @param sessionClassID - the session class ID
80 * @param timer1 - timer1 in seconds
81 * @param timer2 - timer2 in seconds
82 * @param timer3 - timer3 in seconds
83 * @param timer4 - timer4 in seconds
85 public GateSpec(final Direction direction, final byte tosOverwrite, final byte tosMask,
86 final SessionClassID sessionClassID, final short timer1, final short timer2, final short timer3,
89 super(SNum.GATE_SPEC, STYPE);
91 if (direction == null) throw new IllegalArgumentException("Direction is required");
92 // if (tosOverwrite == null) throw new IllegalArgumentException("TOS Overwrite is required");
93 if (sessionClassID == null) throw new IllegalArgumentException("Session class ID is required");
95 this.direction = direction;
96 this.tosOverwrite = tosOverwrite;
97 this.tosMask = tosMask;
98 this.sessionClassID = sessionClassID;
100 this.timer2 = timer2;
101 this.timer3 = timer3;
102 this.timer4 = timer4;
106 public Direction getDirection() {
111 public void setDirection(Direction direction) {
112 this.direction = direction;
116 public byte getDSCP_TOSOverwrite() {
121 public byte getDSCP_TOSMask() {
126 public ISessionClassID getSessionClassID() {
127 return sessionClassID;
131 public short getTimerT1() {
136 public short getTimerT2() {
141 public short getTimerT3() {
146 public short getTimerT4() {
151 protected byte[] getBytes() {
152 final byte[] data = new byte[12];
153 data[0] = direction.getValue();
154 data[1] = tosOverwrite;
156 data[3] = sessionClassID.toSingleByte();
158 final byte[] timer1Bytes = COPSMsgParser.shortToBytes(timer1);
159 data[4] = timer1Bytes[0];
160 data[5] = timer1Bytes[1];
162 final byte[] timer2Bytes = COPSMsgParser.shortToBytes(timer2);
163 data[6] = timer2Bytes[0];
164 data[7] = timer2Bytes[1];
166 final byte[] timer3Bytes = COPSMsgParser.shortToBytes(timer3);
167 data[8] = timer3Bytes[0];
168 data[9] = timer3Bytes[1];
170 final byte[] timer4Bytes = COPSMsgParser.shortToBytes(timer4);
171 data[10] = timer4Bytes[0];
172 data[11] = timer4Bytes[1];
178 public boolean equals(final Object o) {
182 if (!(o instanceof GateSpec)) {
185 if (!super.equals(o)) {
188 final GateSpec gateSpec = (GateSpec) o;
189 return tosMask == gateSpec.tosMask && timer1 == gateSpec.timer1 && timer2 == gateSpec.timer2 &&
190 timer3 == gateSpec.timer3 && timer4 == gateSpec.timer4 && direction == gateSpec.direction &&
191 tosOverwrite == gateSpec.tosOverwrite && sessionClassID.equals(gateSpec.sessionClassID);
196 public int hashCode() {
197 int result = super.hashCode();
198 result = 31 * result + direction.hashCode();
199 result = 31 * result + (int) tosOverwrite;
200 result = 31 * result + (int) tosMask;
201 result = 31 * result + sessionClassID.hashCode();
202 result = 31 * result + (int) timer1;
203 result = 31 * result + (int) timer2;
204 result = 31 * result + (int) timer3;
205 result = 31 * result + (int) timer4;
210 * Returns a GateSpec object from a byte array
211 * @param data - the data to parse
212 * @return - the object
213 * TODO - make me more robust as RuntimeExceptions can be thrown here.
215 public static GateSpec parse(final byte[] data) {
216 return new GateSpec(Direction.valueOf(data[0]), data[1], data[2],
217 new SessionClassID(data[3]), COPSMsgParser.bytesToShort(data[4], data[5]),
218 COPSMsgParser.bytesToShort(data[6], data[7]), COPSMsgParser.bytesToShort(data[8], data[9]),
219 COPSMsgParser.bytesToShort(data[10], data[11]));