Add missing license headers to packetcable-driver base
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / base / impl / PCMMBaseObject.java
1 /*
2  * Copyright (c) 2014, 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.base.impl;
10
11 import com.google.common.primitives.Bytes;
12 import org.pcmm.base.IPCMMBaseObject;
13 import org.umu.cops.stack.COPSData;
14 import org.umu.cops.stack.COPSMsgParser;
15 import org.umu.cops.stack.COPSObjectParser;
16
17 import java.io.IOException;
18 import java.net.Socket;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 /**
23  *
24  * Abstract implementation of the base class {@link IPCMMBaseObject}
25  * Implementations of this class are used for sending/receiving PCMM data to/from a COPS client
26  */
27 public abstract class PCMMBaseObject implements IPCMMBaseObject {
28
29         protected static final short HEADER_OFFSET = (short) 4;
30         protected static final short PADDING_OFFSET = (short) 4;
31
32         // The following two attributes denote the type of PCMMBaseObject
33         private final SNum sNum;
34         private final byte sType;
35
36         /**
37          * Constructor
38          * @param sNum - the S-Num
39          * @param sType - the S-Type
40          */
41         protected PCMMBaseObject(final SNum sNum, final byte sType) {
42                 if (sNum == null) throw new IllegalArgumentException("Invalid or null SNum");
43                 this.sNum = sNum;
44                 this.sType = sType;
45         }
46
47         @Override
48         public final void writeData(final Socket socket) throws IOException {
49                 final byte[] data = getAsBinaryArray();
50                 socket.getOutputStream().write(data, 0, data.length);
51         }
52
53         @Override
54         public final byte[] getAsBinaryArray() {
55                 final byte[] data = getBytes();
56                 final COPSData padding;
57                 if ((data.length % PADDING_OFFSET) != 0) {
58                         final int padLen = PADDING_OFFSET - (data.length % PADDING_OFFSET);
59                         padding = COPSObjectParser.getPadding(padLen);
60                 } else {
61                         padding = new COPSData();
62                 }
63
64                 final int payloadSize = data.length + padding.length() + HEADER_OFFSET;
65                 final List<Byte> outBytes = new ArrayList<>();
66                 outBytes.addAll(Bytes.asList(COPSMsgParser.shortToBytes((short) payloadSize)));
67                 outBytes.add(sNum.getValue());
68                 outBytes.add(sType);
69                 outBytes.addAll(Bytes.asList(data));
70                 outBytes.addAll(Bytes.asList(padding.getData()));
71                 return Bytes.toArray(outBytes);
72         }
73
74         @Override
75         public boolean equals(final Object o) {
76                 if (this == o) {
77                         return true;
78                 }
79                 if (!(o instanceof PCMMBaseObject)) {
80                         return false;
81                 }
82
83                 final PCMMBaseObject that = (PCMMBaseObject) o;
84                 return sType == that.sType && sNum == that.sNum;
85         }
86
87         @Override
88         public int hashCode() {
89                 int result = sNum.hashCode();
90                 result = 31 * result + (int) sType;
91                 return result;
92         }
93
94         /**
95          * Returns the byte array consisting of the data contained within the implementation class
96          * @return - the byte array
97          */
98         protected abstract byte[] getBytes();
99
100         /**
101          * Enumeration of SNum values that is used by the client to determine which Object type is being sent
102          */
103         public enum SNum {
104                 TRANSACTION_ID((byte) 1),
105                 AMID((byte) 2),
106                 SUBSCRIBER_ID((byte) 3),
107                 GATE_ID((byte) 4),
108                 GATE_SPEC((byte) 5),
109                 CLASSIFIERS((byte) 6),
110                 TRAFFIC_PROFILE((byte) 7),
111                 EVENT_GEN_INFO((byte) 8),
112                 VOL_BASED_USAGE_LIMIT((byte) 9),
113                 TIME_BASED_USAGE_LIMIT((byte) 10),
114                 OPAQUE_DATA((byte) 11),
115                 GATE_TIME_INFO((byte) 12),
116                 GATE_USAGE_INFO((byte) 13),
117                 PCMM_ERROR((byte) 14),
118                 GATE_STATE((byte) 15),
119                 VERSION_INFO((byte) 16),
120                 PSID((byte) 17),
121                 SYNC_OPTS((byte) 18),
122                 MSG_RECEIPT_KEY((byte) 19),
123                 USER_ID((byte) 20),
124                 SHARED_RES_ID((byte) 21);
125
126                 private byte value;
127
128                 SNum(byte value) {
129                         this.value = value;
130                 }
131
132                 public byte getValue() {
133                         return value;
134                 }
135
136                 public static SNum valueOf(byte v) {
137                         switch (v) {
138                                 case 1:
139                                         return SNum.TRANSACTION_ID;
140                                 case 2:
141                                         return SNum.AMID;
142                                 case 3:
143                                         return SNum.SUBSCRIBER_ID;
144                                 case 4:
145                                         return SNum.GATE_ID;
146                                 case 5:
147                                         return SNum.GATE_SPEC;
148                                 case 6:
149                                         return SNum.CLASSIFIERS;
150                                 case 7:
151                                         return SNum.TRAFFIC_PROFILE;
152                                 case 8:
153                                         return SNum.EVENT_GEN_INFO;
154                                 case 9:
155                                         return SNum.VOL_BASED_USAGE_LIMIT;
156                                 case 10:
157                                         return SNum.TIME_BASED_USAGE_LIMIT;
158                                 case 11:
159                                         return SNum.OPAQUE_DATA;
160                                 case 12:
161                                         return SNum.GATE_TIME_INFO;
162                                 case 13:
163                                         return SNum.GATE_USAGE_INFO;
164                                 case 14:
165                                         return SNum.PCMM_ERROR;
166                                 case 15:
167                                         return SNum.GATE_STATE;
168                                 case 16:
169                                         return SNum.VERSION_INFO;
170                                 case 17:
171                                         return SNum.PSID;
172                                 case 18:
173                                         return SNum.SYNC_OPTS;
174                                 case 19:
175                                         return SNum.MSG_RECEIPT_KEY;
176                                 case 20:
177                                         return SNum.USER_ID;
178                                 case 21:
179                                         return SNum.SHARED_RES_ID;
180                                 default:
181                                         throw new IllegalArgumentException("not supported value");
182                         }
183                 }
184
185         }
186
187
188
189 }