BUG 2302 : odl-clustering-test-app should not be part of the odl-restconf-all feature set
[controller.git] / opendaylight / adsal / hosttracker_new / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / IndexedEntity.java
1 /*
2  * Copyright (c) 2013 Big Switch Networks, Inc.
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the
5  * "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 package org.opendaylight.controller.hosttracker.internal;
18
19 import java.util.EnumSet;
20
21 import org.opendaylight.controller.hosttracker.Entity;
22 import org.opendaylight.controller.hosttracker.IDeviceService;
23 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * This is a thin wrapper around {@link Entity} that allows overriding the
29  * behavior of {@link Object#hashCode()} and {@link Object#equals(Object)} so
30  * that the keying behavior in a hash map can be changed dynamically
31  *
32  * @author readams
33  */
34 public class IndexedEntity {
35     protected EnumSet<DeviceField> keyFields;
36     protected Entity entity;
37     private int hashCode = 0;
38     protected static Logger logger = LoggerFactory
39             .getLogger(IndexedEntity.class);
40
41     /**
42      * Create a new {@link IndexedEntity} for the given {@link Entity} using the
43      * provided key fields.
44      *
45      * @param keyFields
46      *            The key fields that will be used for computing
47      *            {@link IndexedEntity#hashCode()} and
48      *            {@link IndexedEntity#equals(Object)}
49      * @param entity
50      *            the entity to wrap
51      */
52     public IndexedEntity(EnumSet<DeviceField> keyFields, Entity entity) {
53         super();
54         this.keyFields = keyFields;
55         this.entity = entity;
56     }
57
58     /**
59      * Check whether this entity has non-null values in any of its key fields
60      *
61      * @return true if any key fields have a non-null value
62      */
63     public boolean hasNonNullKeys() {
64         for (DeviceField f : keyFields) {
65             switch (f) {
66             case MAC:
67                 return true;
68             case IPV4:
69                 if (entity.getIpv4Address() != null)
70                     return true;
71                 break;
72             case SWITCHPORT:
73                 if (entity.getPort() != null)
74                     return true;
75                 break;
76             case VLAN:
77                 if (entity.getVlan() != null)
78                     return true;
79                 break;
80             }
81         }
82         return false;
83     }
84
85     @Override
86     public int hashCode() {
87
88         if (hashCode != 0) {
89             return hashCode;
90         }
91
92         final int prime = 31;
93         hashCode = 1;
94         for (DeviceField f : keyFields) {
95             switch (f) {
96             case MAC:
97                 hashCode = prime
98                         * hashCode
99                         + (int) (entity.getMacAddress() ^ (entity
100                                 .getMacAddress() >>> 32));
101                 break;
102             case IPV4:
103                 hashCode = prime
104                         * hashCode
105                         + ((entity.getIpv4Address() == null) ? 0 : entity
106                                 .getIpv4Address().hashCode());
107                 break;
108             case SWITCHPORT:
109                 hashCode = prime
110                         * hashCode
111                         + ((entity.getPort() == null) ? 0 : entity.getPort()
112                                 .hashCode());
113                 break;
114             case VLAN:
115                 hashCode = prime
116                         * hashCode
117                         + ((entity.getVlan() == null) ? 0 : entity.getVlan()
118                                 .hashCode());
119                 break;
120             }
121         }
122         return hashCode;
123     }
124
125     @Override
126     public boolean equals(Object obj) {
127         if (this == obj)
128             return true;
129         if (obj == null)
130             return false;
131         if (getClass() != obj.getClass())
132             return false;
133         IndexedEntity other = (IndexedEntity) obj;
134
135         if (!keyFields.equals(other.keyFields))
136             return false;
137
138         for (IDeviceService.DeviceField f : keyFields) {
139             switch (f) {
140             case MAC:
141                 if (entity.getMacAddress() != other.entity.getMacAddress())
142                     return false;
143                 break;
144             case IPV4:
145                 if (entity.getIpv4Address() == null) {
146                     if (other.entity.getIpv4Address() != null)
147                         return false;
148                 } else if (!entity.getIpv4Address().equals(
149                         other.entity.getIpv4Address()))
150                     return false;
151                 break;
152             case SWITCHPORT:
153                 if (entity.getPort() == null) {
154                     if (other.entity.getPort() != null)
155                         return false;
156                 } else if (!entity.getPort().equals(other.entity.getPort()))
157                     return false;
158                 break;
159             case VLAN:
160                 if (entity.getVlan() == null) {
161                     if (other.entity.getVlan() != null)
162                         return false;
163                 } else if (!entity.getVlan().equals(other.entity.getVlan()))
164                     return false;
165                 break;
166             }
167         }
168
169         return true;
170     }
171
172 }