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 static com.google.common.base.Preconditions.checkNotNull;
13 import java.net.Inet4Address;
14 import org.pcmm.base.impl.PCMMBaseObject;
15 import org.pcmm.gates.ISubscriberID;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
21 * Implementation of the ISubscriberID interface
23 public class SubscriberID extends PCMMBaseObject implements ISubscriberID {
26 * The source IPv4 or IPv6 address
28 private final InetAddress srcIp;
32 * @param srcIp - the source host address
34 public SubscriberID(final InetAddress srcIp) {
35 super(SNum.SUBSCRIBER_ID, chooseSType(srcIp));
36 if (srcIp == null) throw new IllegalArgumentException("srcIp must not be null");
41 public InetAddress getSourceIPAddress() {
46 protected byte[] getBytes() {
47 return srcIp.getAddress();
51 public boolean equals(final Object o) {
55 if (!(o instanceof SubscriberID)) {
58 if (!super.equals(o)) {
61 final SubscriberID that = (SubscriberID) o;
62 return srcIp.equals(that.srcIp);
66 public int hashCode() {
67 int result = super.hashCode();
68 result = 31 * result + srcIp.hashCode();
72 private static byte chooseSType(final InetAddress srcIp) {
74 if (srcIp instanceof Inet4Address) {
83 * Returns a SubscriberID object from a byte array
84 * @param data - the data to parse
85 * @return - the object or null if cannot be parsed
86 * TODO - make me more robust as RuntimeExceptions can be thrown here.
88 public static SubscriberID parse(final byte[] data) {
90 return new SubscriberID(InetAddress.getByAddress(data));
91 } catch (UnknownHostException e) {