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