Updates to support new TrafficProfiles that require a user-specified Direction in...
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / GateSpec.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.IGateSpec;
13 import org.pcmm.gates.ISessionClassID;
14 import org.umu.cops.stack.COPSMsgParser;
15
16 /**
17  * Implementation of the IGateSpec interface
18  */
19 public class GateSpec extends PCMMBaseObject implements IGateSpec {
20
21     /**
22      * The gate's direction
23      */
24     private Direction direction;
25
26     /**
27      * The DSCP/TOS Overwrite is a 1-byte bit field [8] defined by the following alternative structures, depending upon
28      network management strategy.
29      *
30      * When enabled, the CMTS must mark the packets traversing the CMTS DSCP/TOS value
31      */
32     private final byte tosOverwrite;
33
34     /**
35      * tosOverwrite field with the TOS mask is used to identify particular bits within the IPv4 DSCP/TOS byte
36      */
37     private final byte tosMask;
38
39     /**
40      * Session Class ID identifies the proper admission control policy or parameters to be applied for this gate
41      */
42     private final SessionClassID sessionClassID;
43
44     /**
45      * Timer value in seconds. Value of 0 indicates that the CMTS provisioned value for the timer MUST be used.
46      */
47     private final short timer1;
48
49     /**
50      * DOCSIS Admitted timer in seconds
51      */
52     private final short timer2;
53
54     /**
55      * DOCSIS Active timer in seconds
56      */
57     private final short timer3;
58
59     /**
60      * The fourth timer value in seconds
61      */
62     private final short timer4;
63
64     /**
65      * General constructor
66      * @param direction - the gate direction
67      * @param tosOverwrite - ENABLE/DISABLE
68      * @param tosMask - the mask
69      */
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);
72     }
73
74     /**
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
84      */
85     protected GateSpec(final Direction direction, final byte tosOverwrite, final byte tosMask,
86                     final SessionClassID sessionClassID, final short timer1, final short timer2, final short timer3,
87                     final short timer4) {
88
89         super(SNum.GATE_SPEC, STYPE);
90
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");
94
95         this.direction = direction;
96         this.tosOverwrite = tosOverwrite;
97         this.tosMask = tosMask;
98         this.sessionClassID = sessionClassID;
99         this.timer1 = timer1;
100         this.timer2 = timer2;
101         this.timer3 = timer3;
102         this.timer4 = timer4;
103     }
104
105     @Override
106     public Direction getDirection() {
107         return direction;
108     }
109
110     @Override
111     public void setDirection(Direction direction) {
112         this.direction = direction;
113     }
114
115     @Override
116     public byte getDSCP_TOSOverwrite() {
117         return tosOverwrite;
118     }
119
120     @Override
121     public byte getDSCP_TOSMask() {
122         return tosMask;
123     }
124
125     @Override
126     public ISessionClassID getSessionClassID() {
127         return sessionClassID;
128     }
129
130     @Override
131     public short getTimerT1() {
132         return timer1;
133     }
134
135     @Override
136     public short getTimerT2() {
137         return timer2;
138     }
139
140     @Override
141     public short getTimerT3() {
142         return timer3;
143     }
144
145     @Override
146     public short getTimerT4() {
147         return timer4;
148     }
149
150     @Override
151     protected byte[] getBytes() {
152         final byte[] data = new byte[12];
153         data[0] = direction.getValue();
154         data[1] = tosOverwrite;
155         data[2] = tosMask;
156         data[3] = sessionClassID.toSingleByte();
157
158         final byte[] timer1Bytes = COPSMsgParser.shortToBytes(timer1);
159         data[4] = timer1Bytes[0];
160         data[5] = timer1Bytes[1];
161
162         final byte[] timer2Bytes = COPSMsgParser.shortToBytes(timer2);
163         data[6] = timer2Bytes[0];
164         data[7] = timer2Bytes[1];
165
166         final byte[] timer3Bytes = COPSMsgParser.shortToBytes(timer3);
167         data[8] = timer3Bytes[0];
168         data[9] = timer3Bytes[1];
169
170         final byte[] timer4Bytes = COPSMsgParser.shortToBytes(timer4);
171         data[10] = timer4Bytes[0];
172         data[11] = timer4Bytes[1];
173
174         return data;
175     }
176
177     @Override
178     public boolean equals(final Object o) {
179         if (this == o) {
180             return true;
181         }
182         if (!(o instanceof GateSpec)) {
183             return false;
184         }
185         if (!super.equals(o)) {
186             return false;
187         }
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);
192
193     }
194
195     @Override
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;
206         return result;
207     }
208
209     /**
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.
214      */
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]));
220     }
221 }