Add missing license headers to packetcable-driver gates
[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 final 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 byte getDSCP_TOSOverwrite() {
112         return tosOverwrite;
113     }
114
115     @Override
116     public byte getDSCP_TOSMask() {
117         return tosMask;
118     }
119
120     @Override
121     public ISessionClassID getSessionClassID() {
122         return sessionClassID;
123     }
124
125     @Override
126     public short getTimerT1() {
127         return timer1;
128     }
129
130     @Override
131     public short getTimerT2() {
132         return timer2;
133     }
134
135     @Override
136     public short getTimerT3() {
137         return timer3;
138     }
139
140     @Override
141     public short getTimerT4() {
142         return timer4;
143     }
144
145     @Override
146     protected byte[] getBytes() {
147         final byte[] data = new byte[12];
148         data[0] = direction.getValue();
149         data[1] = tosOverwrite;
150         data[2] = tosMask;
151         data[3] = sessionClassID.toSingleByte();
152
153         final byte[] timer1Bytes = COPSMsgParser.shortToBytes(timer1);
154         data[4] = timer1Bytes[0];
155         data[5] = timer1Bytes[1];
156
157         final byte[] timer2Bytes = COPSMsgParser.shortToBytes(timer2);
158         data[6] = timer2Bytes[0];
159         data[7] = timer2Bytes[1];
160
161         final byte[] timer3Bytes = COPSMsgParser.shortToBytes(timer3);
162         data[8] = timer3Bytes[0];
163         data[9] = timer3Bytes[1];
164
165         final byte[] timer4Bytes = COPSMsgParser.shortToBytes(timer4);
166         data[10] = timer4Bytes[0];
167         data[11] = timer4Bytes[1];
168
169         return data;
170     }
171
172     @Override
173     public boolean equals(final Object o) {
174         if (this == o) {
175             return true;
176         }
177         if (!(o instanceof GateSpec)) {
178             return false;
179         }
180         if (!super.equals(o)) {
181             return false;
182         }
183         final GateSpec gateSpec = (GateSpec) o;
184         return tosMask == gateSpec.tosMask && timer1 == gateSpec.timer1 && timer2 == gateSpec.timer2 &&
185                 timer3 == gateSpec.timer3 && timer4 == gateSpec.timer4 && direction == gateSpec.direction &&
186                 tosOverwrite == gateSpec.tosOverwrite && sessionClassID.equals(gateSpec.sessionClassID);
187
188     }
189
190     @Override
191     public int hashCode() {
192         int result = super.hashCode();
193         result = 31 * result + direction.hashCode();
194         result = 31 * result + (int) tosOverwrite;
195         result = 31 * result + (int) tosMask;
196         result = 31 * result + sessionClassID.hashCode();
197         result = 31 * result + (int) timer1;
198         result = 31 * result + (int) timer2;
199         result = 31 * result + (int) timer3;
200         result = 31 * result + (int) timer4;
201         return result;
202     }
203
204     /**
205      * Returns a GateSpec object from a byte array
206      * @param data - the data to parse
207      * @return - the object
208      * TODO - make me more robust as RuntimeExceptions can be thrown here.
209      */
210     public static GateSpec parse(final byte[] data) {
211         return new GateSpec(Direction.valueOf(data[0]), data[1], data[2],
212                 new SessionClassID(data[3]), COPSMsgParser.bytesToShort(data[4], data[5]),
213                 COPSMsgParser.bytesToShort(data[6], data[7]), COPSMsgParser.bytesToShort(data[8], data[9]),
214                 COPSMsgParser.bytesToShort(data[10], data[11]));
215     }
216 }