9cd856d99ac99697756b2605252e6fda85ea3148
[mdsal.git] / entityownership / mdsal-eos-common-api / src / main / java / org / opendaylight / mdsal / common / api / clustering / GenericEntity.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, 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 package org.opendaylight.mdsal.common.api.clustering;
9
10 import com.google.common.base.Preconditions;
11 import java.io.Serializable;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.concepts.Path;
15
16 /**
17  * A clustered Entity is something which is shared by multiple applications across a cluster. An Entity has a type
18  * and an identifier.
19  * <p>
20  * The type describes the type of the Entity where examples of a type maybe "openflow" or "netconf"
21  * etc. An Entity type could be tied to how exactly an application shares and "owns" an entity. For example we may want
22  * an application which deals with the openflow entity to be assigned ownership of that entity based on a first come
23  * first served basis. On the other hand for netconf entity types we may want applications to gain ownership based on
24  * a load balancing approach. While this mechanism of assigning a ownership acquisition strategy is not finalized the
25  * intention is that the entity type will play a role in determining the strategy and thus should be put in place.
26  * <p>
27  * The identifier is an instance identifier path. The reason for the choice of instance identifier path is because it
28  * can easily be used to represent a data node. For example an inventory node represents a shared entity and it is best
29  * referenced by its instance identifier path if the inventory node is stored in the data store.
30  * <p>
31  * Note that an entity identifier must conform to a valid yang schema. If there is no existing yang schema to
32  * represent an entity, the general-entity yang model can be used.
33  * <p>
34  *
35  * @author Thomas Pantelis
36  *
37  * @param <T> the entity identifier type
38  */
39 public class GenericEntity<T extends Path<T>> implements Serializable, Identifiable<T> {
40     private static final long serialVersionUID = 1L;
41
42     private final String type;
43     private final T id;
44
45     protected GenericEntity(@Nonnull String type, @Nonnull T id) {
46         this.type = Preconditions.checkNotNull(type, "type should not be null");
47         this.id = Preconditions.checkNotNull(id, "id should not be null");
48     }
49
50     /**
51      * @return the id of entity.
52      */
53     @Nonnull
54     @Override
55     public final T getIdentifier() {
56         return id;
57     }
58
59     /**
60      * @return the type of entity.
61      */
62     @Nonnull
63     public final String getType(){
64         return type;
65     }
66
67     @SuppressWarnings("unchecked")
68     @Override
69     public boolean equals(Object o) {
70         if (this == o) {
71             return true;
72         }
73
74         if (o == null || getClass() != o.getClass()) {
75             return false;
76         }
77
78         GenericEntity<T> entity = (GenericEntity<T>) o;
79
80         if (!id.equals(entity.id)) {
81             return false;
82         }
83
84         if (!type.equals(entity.type)) {
85             return false;
86         }
87
88         return true;
89     }
90
91     @Override
92     public int hashCode() {
93         return 31 * type.hashCode() + id.hashCode();
94     }
95
96     @Override
97     public String toString() {
98         return getClass().getSimpleName() + " [type=" + type + ", id=" + id + "]";
99     }
100 }