e98b5dc982c1edbcfcd9d4f5de17fa75d56cbf0f
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / SubscriberID.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.gates.impl;
6
7 import org.pcmm.base.impl.PCMMBaseObject;
8 import org.pcmm.gates.ISubscriberID;
9
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12
13 /**
14  * Implementation of the ISubscriberID interface
15  */
16 public class SubscriberID extends PCMMBaseObject implements ISubscriberID {
17
18     /**
19      * The source IPv4 or IPv6 address
20      */
21     private final InetAddress srcIp;
22
23     /**
24      * Constructor
25      * @param srcIp - the source host address
26      */
27     public SubscriberID(final InetAddress srcIp) {
28         super(SNum.SUBSCRIBER_ID, STYPE);
29         if (srcIp == null) throw new IllegalArgumentException("srcIp must not be null");
30         this.srcIp = srcIp;
31     }
32
33     @Override
34     public InetAddress getSourceIPAddress() {
35         return srcIp;
36     }
37
38     @Override
39     protected byte[] getBytes() {
40         return srcIp.getAddress();
41     }
42
43     @Override
44     public boolean equals(final Object o) {
45         if (this == o) {
46             return true;
47         }
48         if (!(o instanceof SubscriberID)) {
49             return false;
50         }
51         if (!super.equals(o)) {
52             return false;
53         }
54         final SubscriberID that = (SubscriberID) o;
55         return srcIp.equals(that.srcIp);
56     }
57
58     @Override
59     public int hashCode() {
60         int result = super.hashCode();
61         result = 31 * result + srcIp.hashCode();
62         return result;
63     }
64
65     /**
66      * Returns a SubscriberID object from a byte array
67      * @param data - the data to parse
68      * @return - the object or null if cannot be parsed
69      * TODO - make me more robust as RuntimeExceptions can be thrown here.
70      */
71     public static SubscriberID parse(final byte[] data) {
72         try {
73             return new SubscriberID(InetAddress.getByAddress(data));
74         } catch (UnknownHostException e) {
75             return null;
76         }
77     }
78 }