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