Bug 4105: Add general-entities yang model
[controller.git] / opendaylight / md-sal / sal-common-api / src / main / java / org / opendaylight / controller / md / sal / common / api / clustering / Entity.java
index 3b9f8a425eb201d1a1eb5eecb8d832dca8a1c88a..35f23a490b1ef44ab220ed17ee96684454d16f91 100644 (file)
@@ -9,7 +9,9 @@
 package org.opendaylight.controller.md.sal.common.api.clustering;
 
 import com.google.common.base.Preconditions;
+import java.io.Serializable;
 import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
 /**
@@ -28,16 +30,25 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
  * <p>
  * The identifier is a YangInstanceIdentifier. The reason for the choice of YangInstanceIdentifier is because it
  * can easily be used to represent a data node. For example an inventory node represents a shared entity and it is best
- * referenced by the YangInstanceIdentifier if the inventory node stored in the data store.
+ * referenced by the YangInstanceIdentifier if the inventory node is stored in the data store.
+ * </p>
+ * Note that an entity identifier must conform to a valid yang schema. If there is no existing yang schema to
+ * represent an entity, the general-entity yang model can be used.
+ * <p>
  * </p>
  */
-public final class Entity {
+public final class Entity implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private static final QName ENTITY_QNAME =
+        org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.core.general.entity.rev150820.Entity.QNAME;
+    private static final QName ENTITY_NAME = QName.create(ENTITY_QNAME, "name");
 
     private final String type;
     private final YangInstanceIdentifier id;
 
     /**
-     * Construct a new Entity
+     * Construct an Entity with a YangInstanceIdentifier.
      *
      * @param type the type of the entity
      * @param id the identifier of the entity
@@ -47,6 +58,19 @@ public final class Entity {
         this.id = Preconditions.checkNotNull(id, "id should not be null");
     }
 
+    /**
+     * Construct an Entity with an with a name. The general-entity schema is used to construct the
+     * YangInstanceIdentifier.
+     *
+     * @param type the type of the entity
+     * @param entityName the name of the entity used to construct a general-entity YangInstanceIdentifier
+     */
+    public Entity(@Nonnull String type, @Nonnull String entityName) {
+        this.type = Preconditions.checkNotNull(type, "type should not be null");
+        this.id = YangInstanceIdentifier.builder().node(ENTITY_QNAME).nodeWithKey(ENTITY_QNAME, ENTITY_NAME,
+                        Preconditions.checkNotNull(entityName, "entityName should not be null")).build();
+    }
+
     /**
      *
      * @return id of entity
@@ -90,9 +114,7 @@ public final class Entity {
 
     @Override
     public int hashCode() {
-        int result = type != null ? type.hashCode() : 0;
-        result = 31 * result + (id != null ? id.hashCode() : 0);
-        return result;
+        return 31 * type.hashCode() + id.hashCode();
     }
 
     @Override