2 * Copyright (c) 2015 Cable Television Laboratories, Inc. and others. All rights reserved.
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
9 package org.pcmm.gates.impl;
11 import com.google.common.primitives.Bytes;
12 import org.pcmm.base.impl.PCMMBaseObject;
13 import org.pcmm.gates.IClassifier;
14 import org.umu.cops.stack.COPSMsgParser;
16 import java.net.Inet4Address;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.ArrayList;
20 import java.util.List;
23 * Implementation of the IClassifier interface
25 public class Classifier extends PCMMBaseObject implements IClassifier {
28 * The classifier's protocol
30 protected final Protocol protocol;
33 * When enabled, the CMTS must mark the packets traversing the CMTS DSCP/TOS value
35 protected final byte tosOverwrite;
40 protected final byte tosMask;
42 // Instances of this and ExtendedClassifier require Inet4Address objects
47 protected final InetAddress srcAddress;
50 * The destination address
52 protected final InetAddress dstAddress;
55 * The source port number
57 protected final short srcPort;
60 * The destination port number
62 protected final short dstPort;
67 protected final byte priority;
70 * Constructor for sub-classes
71 * @param protocol - the protocol being sent through the gate (can be null for IPv6Classifier instances)
72 * @param tosOverwrite - ENABLE/DISABLE
73 * @param tosMask - the mask
74 * @param srcAddress - the source IP
75 * @param dstAddress - the destination IP
76 * @param srcPort - the source port
77 * @param dstPort - the destination port
78 * @param priority - the priority value
80 public Classifier(final Protocol protocol, final byte tosOverwrite, final byte tosMask,
81 final Inet4Address srcAddress, final Inet4Address dstAddress, final short srcPort,
82 final short dstPort, final byte priority) {
83 this(STYPE, protocol, tosOverwrite, tosMask, srcAddress, dstAddress, srcPort, dstPort, priority);
87 * Constructor for sub-classes
88 * @param sType - the type of classifier
89 * @param protocol - the protocol being sent through the gate
90 * @param tosOverwrite - ENABLE/DISABLE
91 * @param tosMask - the mask
92 * @param srcAddress - the source IP
93 * @param dstAddress - the destination IP
94 * @param srcPort - the source port
95 * @param dstPort - the destination port
96 * @param priority - the priority value
98 protected Classifier(final byte sType, final Protocol protocol, final byte tosOverwrite, final byte tosMask,
99 final InetAddress srcAddress, final InetAddress dstAddress, final short srcPort,
100 final short dstPort, final byte priority) {
101 super(SNum.CLASSIFIERS, sType);
103 if (protocol == null && !(this instanceof IPv6Classifier))
104 throw new IllegalArgumentException("Protocol value must not be null");
105 if (srcAddress == null) throw new IllegalArgumentException("Source address value must not be null");
106 if (dstAddress == null) throw new IllegalArgumentException("Destination address value must not be null");
108 this.protocol = protocol;
109 this.tosOverwrite = tosOverwrite;
110 this.tosMask = tosMask;
111 this.srcAddress = srcAddress;
112 this.dstAddress = dstAddress;
113 this.srcPort = srcPort;
114 this.dstPort = dstPort;
115 this.priority = priority;
119 public InetAddress getDestinationIPAddress() {
124 public short getDestinationPort() {
129 public InetAddress getSourceIPAddress() {
134 public short getSourcePort() {
139 public Protocol getProtocol() {
144 public byte getPriority() {
149 public byte getDSCPTOS() {
154 public byte getDSCPTOSMask() {
159 protected byte[] getBytes() {
160 final List<Byte> byteList = new ArrayList<>(Bytes.asList(COPSMsgParser.shortToBytes(protocol.getValue())));
161 byteList.add(tosOverwrite);
162 byteList.add(tosMask);
163 byteList.addAll(Bytes.asList(srcAddress.getAddress()));
164 byteList.addAll(Bytes.asList(dstAddress.getAddress()));
165 byteList.addAll(Bytes.asList(COPSMsgParser.shortToBytes(srcPort)));
166 byteList.addAll(Bytes.asList(COPSMsgParser.shortToBytes(dstPort)));
167 byteList.add(priority);
170 byteList.addAll(Bytes.asList((byte) 0, (byte) 0, (byte) 0));
172 return Bytes.toArray(byteList);
176 public boolean equals(final Object o) {
180 if (!(o instanceof Classifier)) {
183 if (!super.equals(o)) {
186 final Classifier that = (Classifier) o;
187 return tosMask == that.tosMask && srcPort == that.srcPort && dstPort == that.dstPort &&
188 priority == that.priority && protocol == that.protocol && tosOverwrite == that.tosOverwrite &&
189 srcAddress.equals(that.srcAddress) && dstAddress.equals(that.dstAddress);
193 public int hashCode() {
194 int result = super.hashCode();
195 result = 31 * result + (protocol != null ? protocol.hashCode() : 0);
196 result = 31 * result + (int) tosOverwrite;
197 result = 31 * result + (int) tosMask;
198 result = 31 * result + srcAddress.hashCode();
199 result = 31 * result + dstAddress.hashCode();
200 result = 31 * result + (int) srcPort;
201 result = 31 * result + (int) dstPort;
202 result = 31 * result + (int) priority;
207 * Returns a Classifier object from a byte array
208 * @param data - the data to parse
209 * @return - the object or null if cannot be parsed
210 * TODO - make me more robust as exceptions can be swallowed here.
212 public static Classifier parse(final byte[] data) {
213 final List<Byte> bytes = Bytes.asList(data);
216 final byte[] srcAddrBytes = Bytes.toArray(bytes.subList(4, 8));
217 final byte[] dstAddrBytes = Bytes.toArray(bytes.subList(8, 12));
218 return new Classifier(Protocol.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])), data[2], data[3],
219 (Inet4Address)InetAddress.getByAddress(srcAddrBytes),
220 (Inet4Address)InetAddress.getByAddress(dstAddrBytes),
221 COPSMsgParser.bytesToShort(data[12], data[13]), COPSMsgParser.bytesToShort(data[14], data[15]),
223 } catch (UnknownHostException e) {