String performance and maintenability
[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  * A clustered Entity is something which is shared by multiple applications across a cluster. An Entity has a type
19  * and an identifier.
20  *
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  *
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 is stored in the data store.
33  *
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  *
38  * @deprecated Use org.opendaylight.mdsal.binding.api.clustering.Entity or
39  *             org.opendaylight.mdsal.eos.dom.api.DOMEntity instead
40  */
41 @Deprecated
42 public final class Entity implements Serializable {
43     private static final long serialVersionUID = 1L;
44
45     private static final QName ENTITY_QNAME =
46         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.core.general.entity.rev150820.Entity.QNAME;
47     private static final QName ENTITY_NAME = QName.create(ENTITY_QNAME, "name");
48
49     private final String type;
50     private final YangInstanceIdentifier id;
51
52     /**
53      * Construct an Entity with a YangInstanceIdentifier.
54      *
55      * @param type the type of the entity
56      * @param id the identifier of the entity
57      */
58     public Entity(@Nonnull String type, @Nonnull YangInstanceIdentifier id) {
59         this.type = Preconditions.checkNotNull(type, "type should not be null");
60         this.id = Preconditions.checkNotNull(id, "id should not be null");
61     }
62
63     /**
64      * Construct an Entity with an with a name. The general-entity schema is used to construct the
65      * YangInstanceIdentifier.
66      *
67      * @param type the type of the entity
68      * @param entityName the name of the entity used to construct a general-entity YangInstanceIdentifier
69      */
70     public Entity(@Nonnull String type, @Nonnull String entityName) {
71         this.type = Preconditions.checkNotNull(type, "type should not be null");
72         this.id = YangInstanceIdentifier.builder().node(ENTITY_QNAME).nodeWithKey(ENTITY_QNAME, ENTITY_NAME,
73                         Preconditions.checkNotNull(entityName, "entityName should not be null")).build();
74     }
75
76     /**
77      *
78      * @return id of entity
79      */
80     @Nonnull
81     public YangInstanceIdentifier getId(){
82         return id;
83     }
84
85     /**
86      *
87      * @return type of entity
88      */
89     @Nonnull
90     public String getType(){
91         return type;
92     }
93
94     @Override
95     public boolean equals(Object o) {
96         if (this == o) {
97             return true;
98         }
99
100         if (o == null || getClass() != o.getClass()) {
101             return false;
102         }
103
104         Entity entity = (Entity) o;
105
106         if (!id.equals(entity.id)) {
107             return false;
108         }
109
110         if (!type.equals(entity.type)) {
111             return false;
112         }
113
114         return true;
115     }
116
117     @Override
118     public int hashCode() {
119         return 31 * type.hashCode() + id.hashCode();
120     }
121
122     @Override
123     public String toString() {
124         return "Entity{" + "type='" + type + '\'' + ", id=" + id + '}';
125     }
126 }