d7d7e8bc523fb4c302136d6bedce0f0826817de4
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / BestEffortService.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.gates.impl;
6
7 import com.google.common.primitives.Bytes;
8 import org.pcmm.base.impl.PCMMBaseObject;
9 import org.pcmm.gates.ITrafficProfile;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  * The Best Effort object defines the Traffic Profile associated with a gate through an upstream DOCSIS-specific
16  * parameterization scheme.
17  */
18 public class BestEffortService extends PCMMBaseObject implements ITrafficProfile {
19
20         public static final byte STYPE = 3;
21         // XXX -> 60=0x3C, 112 = 0x70, 164=0xA4
22         // Length = 44=0x2C, 80=0x50 or 116=0x74
23
24         public static final byte DEFAULT_TRAFFIC_PRIORITY = 0;
25
26         public static final int DEFAULT_MAX_TRAFFIC_BURST = 3044;
27
28         /**
29          * The envelope
30          */
31         private final byte envelope;
32
33         /**
34          * The authorized envelope. See BEEnvelope for description of the attributes. MUST NOT be NULL.
35          */
36         private final BEEnvelop authorizedEnvelop;
37
38         /**
39          * The reserved envelope. See BEEnvelope for description of the attributes. CAN BE NULL.
40          */
41         private final BEEnvelop reservedEnvelop;
42
43         /**
44          * The committed envelope. See BEEnvelope for description of the attributes. CAN BE NULL.
45          */
46         private final BEEnvelop committedEnvelop;
47
48         /**
49          * General use constructor
50          * @param auth - the authorized envelope (required)
51          * @param reserved - the reserved envelope (optional)
52          * @param committed - the committed envelope (optional)
53          */
54         public BestEffortService(final BEEnvelop auth, final BEEnvelop reserved, final BEEnvelop committed) {
55                 this(DEFAULT_ENVELOP, auth, reserved, committed);
56         }
57
58         /**
59          * Constructor generally used for byte parsing only.
60          * @param envelope - the envelope value
61          * @param auth - the authorized envelope (required)
62          * @param reserved - the reserved envelope (optional)
63          * @param committed - the committed envelope (optional)
64          */
65         protected BestEffortService(final byte envelope, final BEEnvelop auth, final BEEnvelop reserved,
66                                                          final BEEnvelop committed) {
67                 super(SNum.TRAFFIC_PROFILE, STYPE);
68                 if (auth == null) throw new IllegalArgumentException("Authorized envelope must not be null");
69
70                 // TODO - Cannot figure out any other means to parse the bytes unless this is true. Determine if correct???
71                 if (reserved == null && committed != null)
72                         throw new IllegalArgumentException("Cannot have a committed envelope without a reserved");
73
74                 this.envelope = envelope;
75                 this.authorizedEnvelop = auth;
76                 this.reservedEnvelop = reserved;
77                 this.committedEnvelop = committed;
78         }
79
80         @Override
81         public byte getEnvelop() {
82                 return envelope;
83         }
84
85         // Getters
86         public BEEnvelop getAuthorizedEnvelop() {
87                 return authorizedEnvelop;
88         }
89
90         public BEEnvelop getReservedEnvelop() {
91                 return reservedEnvelop;
92         }
93
94         public BEEnvelop getCommittedEnvelop() {
95                 return committedEnvelop;
96         }
97
98         @Override
99         public byte[] getBytes() {
100                 final List<Byte> byteList = new ArrayList<>();
101                 byteList.addAll(Bytes.asList(envelope, (byte) 0, (byte) 0, (byte) 0));
102                 byteList.addAll(authorizedEnvelop.getBytes());
103                 if (reservedEnvelop != null) byteList.addAll(reservedEnvelop.getBytes());
104                 if (committedEnvelop != null) byteList.addAll(committedEnvelop.getBytes());
105                 return Bytes.toArray(byteList);
106         }
107
108         @Override
109         public boolean equals(final Object o) {
110                 if (this == o) {
111                         return true;
112                 }
113                 if (!(o instanceof BestEffortService)) {
114                         return false;
115                 }
116                 if (!super.equals(o)) {
117                         return false;
118                 }
119                 final BestEffortService that = (BestEffortService) o;
120                 return envelope == that.envelope && authorizedEnvelop.equals(that.authorizedEnvelop) &&
121                                 !(reservedEnvelop != null ? !reservedEnvelop.equals(that.reservedEnvelop) :
122                                                 that.reservedEnvelop != null) &&
123                                 !(committedEnvelop != null ? !committedEnvelop.equals(that.committedEnvelop) :
124                                                 that.committedEnvelop != null);
125
126         }
127
128         @Override
129         public int hashCode() {
130                 int result = super.hashCode();
131                 result = 31 * result + (int) envelope;
132                 result = 31 * result + authorizedEnvelop.hashCode();
133                 result = 31 * result + (reservedEnvelop != null ? reservedEnvelop.hashCode() : 0);
134                 result = 31 * result + (committedEnvelop != null ? committedEnvelop.hashCode() : 0);
135                 return result;
136         }
137
138         /**
139          * Returns a BestEffortService object from a byte array
140          * @param data - the data to parse
141          * @return - the object or null if cannot be parsed
142          * TODO - make me more robust as RuntimeExceptions can be thrown here.
143          */
144         public static BestEffortService parse(final byte[] data) {
145                 final List<Byte> bytes = Bytes.asList(data);
146                 bytes.subList(0, 51);
147                 if (data.length >= 56 && data.length < 108)
148                         return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))), null, null);
149                 else if (data.length >= 108 && data.length < 160)
150                         return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))),
151                                         BEEnvelop.parse(Bytes.toArray(bytes.subList(56, 108))), null);
152                 else if (data.length >= 160)
153                                 return new BestEffortService(data[0], BEEnvelop.parse(Bytes.toArray(bytes.subList(4, 56))),
154                                                 BEEnvelop.parse(Bytes.toArray(bytes.subList(56, 108))),
155                                                 BEEnvelop.parse(Bytes.toArray(bytes.subList(108, 160))));
156                 else return null;
157         }
158
159 }