8c5ec22acf4be4cc74319a46868d8301f0ddd71a
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / SessionClassID.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.gates.impl;
6
7 import org.pcmm.gates.ISessionClassID;
8
9 /**
10  * SessionClassID is a 1-byte unsigned integer value which identifies the proper admission control policy or
11  * parameters to be applied for this Gate. The SessionClassID is a bit field, defined as follows:
12  *   Bit 0-2: Priority, a number from 0 to 7, where 0 is low priority and 7 is high.
13  *   Bit 3: Preemption, set to enable preemption of bandwidth allocated to lower priority sessions if necessary
14  *   (if supported).
15  *   Bit 4-7: Configurable, default to 0
16  */
17 public class SessionClassID implements ISessionClassID {
18
19     /**
20      * The priority field describes the relative importance of the session as compared to other sessions generated by
21      * the same PDP. The PEP MAY use this value to both implement priority based admission (in conjunction with the
22      * Preemption bit), and to defend the resulting flow from being preempted.
23      */
24     private transient byte priority;
25
26     /**
27      * The preemption bit is used to by a PDP to direct the PEP to apply a priority-based admission control. Preemption
28      * support is optional; a PEP MAY ignore this bit. If preemption is not requested by the PDP or not implement by the
29      * PEP, then admission control policy will be on a first-come, first-served basis.
30      */
31     private transient byte preemption;
32
33     /**
34      * The session ID value
35      */
36     private byte session;
37
38     /**
39      * Constructor 1
40      * @param value - the session value
41      */
42     public SessionClassID(byte value) {
43         this.session = value;
44         this.priority = 0;
45         this.preemption = 0;
46         priority |= value >> 2;
47         preemption |= value >> 3;
48     }
49
50     /**
51      * Constructor 2
52      * @param value - the session value
53      * @param priority - overrides the default priority value of 2
54      * @param preemption - overrides the default preemption value of 3
55      */
56     public SessionClassID(byte value, final byte priority, final byte preemption) {
57         this.session = value;
58         this.priority = priority;
59         this.preemption = preemption;
60     }
61
62     @Override
63     public byte getPriority() {
64         return priority;
65     }
66
67     @Override
68     public byte getPreemption() {
69         return preemption;
70     }
71
72     @Override
73     public byte toSingleByte() {
74         return session;
75     }
76
77     @Override
78     public boolean equals(final Object o) {
79         if (this == o) {
80             return true;
81         }
82         if (!(o instanceof SessionClassID)) {
83             return false;
84         }
85         final SessionClassID that = (SessionClassID) o;
86         return priority == that.priority && preemption == that.preemption && session == that.session;
87     }
88
89     @Override
90     public int hashCode() {
91         int result = (int) priority;
92         result = 31 * result + (int) preemption;
93         result = 31 * result + (int) session;
94         return result;
95     }
96 }