35f23a490b1ef44ab220ed17ee96684454d16f91
[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.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16
17 /**
18  * <p></p>
19  * A clustered Entity is something which is shared by multiple applications across a cluster. An Entity has a type
20  * and an identifier.
21  * </p>
22  * <p>
23  * The type describes the type of the Entity where examples of a type maybe "openflow" or "netconf"
24  * etc. An Entity type could be tied to how exactly an application shares and "owns" an entity. For example we may want
25  * an application which deals with the openflow entity to be assigned ownership of that entity based on a first come
26  * first served basis. On the other hand for netconf entity types we may want applications to gain ownership based on
27  * a load balancing approach. While this mechanism of assigning a ownership acquisition strategy is not finalized the
28  * intention is that the entity type will play a role in determining the strategy and thus should be put in place.
29  * </p>
30  * <p>
31  * The identifier is a YangInstanceIdentifier. The reason for the choice of YangInstanceIdentifier is because it
32  * can easily be used to represent a data node. For example an inventory node represents a shared entity and it is best
33  * referenced by the YangInstanceIdentifier if the inventory node is stored in the data store.
34  * </p>
35  * Note that an entity identifier must conform to a valid yang schema. If there is no existing yang schema to
36  * represent an entity, the general-entity yang model can be used.
37  * <p>
38  * </p>
39  */
40 public final class Entity implements Serializable {
41     private static final long serialVersionUID = 1L;
42
43     private static final QName ENTITY_QNAME =
44         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.core.general.entity.rev150820.Entity.QNAME;
45     private static final QName ENTITY_NAME = QName.create(ENTITY_QNAME, "name");
46
47     private final String type;
48     private final YangInstanceIdentifier id;
49
50     /**
51      * Construct an Entity with a YangInstanceIdentifier.
52      *
53      * @param type the type of the entity
54      * @param id the identifier of the entity
55      */
56     public Entity(@Nonnull String type, @Nonnull YangInstanceIdentifier id) {
57         this.type = Preconditions.checkNotNull(type, "type should not be null");
58         this.id = Preconditions.checkNotNull(id, "id should not be null");
59     }
60
61     /**
62      * Construct an Entity with an with a name. The general-entity schema is used to construct the
63      * YangInstanceIdentifier.
64      *
65      * @param type the type of the entity
66      * @param entityName the name of the entity used to construct a general-entity YangInstanceIdentifier
67      */
68     public Entity(@Nonnull String type, @Nonnull String entityName) {
69         this.type = Preconditions.checkNotNull(type, "type should not be null");
70         this.id = YangInstanceIdentifier.builder().node(ENTITY_QNAME).nodeWithKey(ENTITY_QNAME, ENTITY_NAME,
71                         Preconditions.checkNotNull(entityName, "entityName should not be null")).build();
72     }
73
74     /**
75      *
76      * @return id of entity
77      */
78     @Nonnull
79     public YangInstanceIdentifier getId(){
80         return id;
81     }
82
83     /**
84      *
85      * @return type of entity
86      */
87     @Nonnull
88     public String getType(){
89         return type;
90     }
91
92     @Override
93     public boolean equals(Object o) {
94         if (this == o) {
95             return true;
96         }
97
98         if (o == null || getClass() != o.getClass()) {
99             return false;
100         }
101
102         Entity entity = (Entity) o;
103
104         if (!id.equals(entity.id)) {
105             return false;
106         }
107
108         if (!type.equals(entity.type)) {
109             return false;
110         }
111
112         return true;
113     }
114
115     @Override
116     public int hashCode() {
117         return 31 * type.hashCode() + id.hashCode();
118     }
119
120     @Override
121     public String toString() {
122         final StringBuilder sb = new StringBuilder("Entity{");
123         sb.append("type='").append(type).append('\'');
124         sb.append(", id=").append(id);
125         sb.append('}');
126         return sb.toString();
127     }
128 }