Add packetcable-driver as a bundle and fix karafe depends. Merge hop-along model...
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / BestEffortService.java
1 /**
2  @header@
3  */
4 package org.pcmm.gates.impl;
5
6 import java.util.Arrays;
7
8 // import org.junit.Assert;
9 import org.pcmm.base.impl.PCMMBaseObject;
10 import org.pcmm.gates.ITrafficProfile;
11 import org.umu.cops.stack.COPSData;
12
13 /**
14  *
15  */
16 public class BestEffortService extends PCMMBaseObject implements
17                 ITrafficProfile {
18         public static final byte STYPE = 3;
19         // XXX -> 60=0x3C, 112 = 0x70, 164=0xA4
20         // Length = 44=0x2C, 80=0x50 or 116=0x74
21         public static final short LENGTH = 44;
22
23         public static final byte DEFAULT_TRAFFIC_PRIORITY = 0;
24         // Authorized
25         public static final byte DEFAULT_ENVELOP = 0x7;
26
27         public static final int DEFAULT_MAX_TRAFFIC_BURST = 3044;
28
29         private BEEnvelop authorizedEnvelop;
30
31         private BEEnvelop reservedEnvelop;
32
33         private BEEnvelop committedEnvelop;
34
35         /**
36          * 
37          * @param e
38          *            envelop
39          */
40         public BestEffortService(byte e) {
41                 super((short) (e == 1 ? LENGTH : (e == 7 ? 116 : 80)), STYPE, SNUM);
42                 setEnvelop(e);
43                 authorizedEnvelop = new BEEnvelop();
44                 if (e > 1) {
45                         reservedEnvelop = new BEEnvelop();
46                         if (e == 7)
47                                 committedEnvelop = new BEEnvelop();
48                 }
49         }
50
51         public BestEffortService(byte[] bytes) {
52                 super(bytes);
53                 byte e = getEnvelop();
54                 authorizedEnvelop = new BEEnvelop(headPadding(offset, Arrays.copyOfRange(bytes, 8, LENGTH)));
55                 if (e > 1) {
56                         reservedEnvelop = new BEEnvelop(headPadding(offset, Arrays.copyOfRange(bytes, LENGTH, 80)));
57                         if (e == 7)
58                                 committedEnvelop = new BEEnvelop(headPadding(offset, Arrays.copyOfRange(bytes, 80, 116)));
59                 }
60         }
61
62         @Override
63         public void setEnvelop(byte e) {
64                 setLength((short) (e == 1 ? LENGTH : (e == 7 ? 116 : 80)));
65                 // reset cops data to fit the new length
66                 byte[] array = new byte[getLength() - offset];
67                 Arrays.fill(array, (byte) 0);
68                 setData(new COPSData(array, 0, array.length));
69                 setByte(e, (short) 0);
70         }
71
72         @Override
73         public byte getEnvelop() {
74                 return getByte((short) 0);
75         }
76
77         public BEEnvelop getAuthorizedEnvelop() {
78                 return authorizedEnvelop;
79         }
80
81         public BEEnvelop getReservedEnvelop() {
82                 return reservedEnvelop;
83         }
84
85         public BEEnvelop getCommittedEnvelop() {
86                 return committedEnvelop;
87         }
88
89         @Override
90         public byte[] getAsBinaryArray() {
91                 byte[] returnBuffer = super.getAsBinaryArray();
92
93                 {// fill buffer with the Authorized Envelop
94                         byte[] authEnv = Arrays.copyOfRange(getAuthorizedEnvelop().getAsBinaryArray(), offset, BEEnvelop.LENGHT);
95                         // offset + 4 since the Envelop data begin from byte nb 8
96                         System.arraycopy(authEnv, 0, returnBuffer, offset + 4, authEnv.length);
97                 }
98                 if (getReservedEnvelop() != null) {
99                         byte[] reservedEnv = Arrays.copyOfRange(getReservedEnvelop().getAsBinaryArray(), offset, BEEnvelop.LENGHT);
100                         System.arraycopy(reservedEnv, 0, returnBuffer, LENGTH, reservedEnv.length);
101                 }
102                 if (getCommittedEnvelop() != null) {
103                         byte[] commitEnv = Arrays.copyOfRange(getCommittedEnvelop().getAsBinaryArray(), offset, BEEnvelop.LENGHT);
104                         System.arraycopy(commitEnv, 0, returnBuffer, LENGTH + 36, commitEnv.length);
105                 }
106                 return returnBuffer;
107         }
108
109         /**
110      *
111      *
112      */
113         public static class BEEnvelop extends PCMMBaseObject {
114                 // basically we need 36 bytes but since PCMMBasedObject needs 4 bytes
115                 // more we allocate 40 bytes and then subtract them when setting BE
116                 // data.
117                 private final static short LENGHT = 40;
118
119                 protected BEEnvelop() {
120                         super(LENGHT, (byte) 0, (byte) 0);
121                         setTrafficPriority(DEFAULT_TRAFFIC_PRIORITY);
122                 }
123
124                 protected BEEnvelop(byte[] buffer) {
125                         super(buffer);
126                 }
127
128                 public void setTrafficPriority(byte p) {
129                         setByte(p, (short) 0);
130                 }
131
132                 public byte getTrafficPriority() {
133                         return getByte((short) 0);
134                 }
135
136                 //
137                 public void setRequestTransmissionPolicy(int p) {
138                         setInt(p, (short) 4);
139                 }
140
141                 public int getRequestTransmissionPolicy() {
142                         return getInt((short) 4);
143                 }
144
145                 public int getMaximumSustainedTrafficRate() {
146                         return getInt((short) 8);
147                 }
148
149                 public void setMaximumSustainedTrafficRate(int p) {
150                         setInt(p, (short) 8);
151                 }
152
153                 public int getMaximumTrafficBurst() {
154                         return getInt((short) 12);
155                 }
156
157                 public void setMaximumTrafficBurst(int p) {
158                         setInt(p, (short) 12);
159                 }
160
161                 public int getMinimumReservedTrafficRate() {
162                         return getInt((short) 16);
163                 }
164
165                 public void setMinimumReservedTrafficRate(int p) {
166                         setInt(p, (short) 16);
167                 }
168
169                 public short getAssumedMinimumReservedTrafficRatePacketSize() {
170                         return getShort((short) 20);
171                 }
172
173                 public void setAssumedMinimumReservedTrafficRatePacketSize(short p) {
174                         setShort(p, (short) 20);
175                 }
176
177                 public short getMaximumConcatenatedBurst() {
178                         return getShort((short) 22);
179                 }
180
181                 public void setMaximumConcatenatedBurst(short p) {
182                         setShort(p, (short) 22);
183                 }
184
185                 public int getRequiredAttributeMask() {
186                         return getInt((short) 24);
187                 }
188
189                 public void setRequiredAttributeMask(int p) {
190                         setInt(p, (short) 24);
191                 }
192
193                 public int getForbiddenAttributeMask() {
194                         return getInt((short) 28);
195                 }
196
197                 public void setForbiddenAttributeMask(int p) {
198                         setInt(p, (short) 28);
199                 }
200
201                 public int getAttributeAggregationRuleMask() {
202                         return getInt((short) 32);
203                 }
204
205                 public void setAttributeAggregationRuleMask(int p) {
206                         setInt(p, (short) 32);
207                 }
208
209         }
210
211 }