Use singleton NodeIdentifierWithPredicates
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnersModel.java
index 205763a00060b9b903c1d95b879fd0c4e22e25d9..73be12419c59d47ec425e31314f64025cb66eddf 100644 (file)
@@ -8,9 +8,10 @@
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
 import java.util.Map.Entry;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
+import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.Entity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -31,113 +32,116 @@ import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOr
  * @author Thomas Pantelis
  */
 public final class EntityOwnersModel {
-    static final  QName ENTITY_QNAME = org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.
-            md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.Entity.QNAME;
-    static final QName CANDIDATE_NAME_QNAME = QName.create(Candidate.QNAME, "name");
-    static final QName ENTITY_ID_QNAME = QName.create(ENTITY_QNAME, "id");
-    static final QName ENTITY_OWNER_QNAME = QName.create(ENTITY_QNAME, "owner");
-    static final QName ENTITY_TYPE_QNAME = QName.create(EntityType.QNAME, "type");
-
-    static final NodeIdentifier ENTITY_OWNERS_NODE_ID = new NodeIdentifier(EntityOwners.QNAME);
-    static final NodeIdentifier ENTITY_OWNER_NODE_ID = new NodeIdentifier(ENTITY_OWNER_QNAME);
-    static final NodeIdentifier ENTITY_NODE_ID = new NodeIdentifier(ENTITY_QNAME);
-    static final NodeIdentifier ENTITY_ID_NODE_ID = new NodeIdentifier(ENTITY_ID_QNAME);
-    static final NodeIdentifier ENTITY_TYPE_NODE_ID = new NodeIdentifier(ENTITY_TYPE_QNAME);
-    static final NodeIdentifier CANDIDATE_NODE_ID = new NodeIdentifier(Candidate.QNAME);
-    static final NodeIdentifier CANDIDATE_NAME_NODE_ID = new NodeIdentifier(CANDIDATE_NAME_QNAME);
-    static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.of(EntityOwners.QNAME);
-    static final YangInstanceIdentifier ENTITY_TYPES_PATH =
-            YangInstanceIdentifier.of(EntityOwners.QNAME).node(EntityType.QNAME);
-
-    static YangInstanceIdentifier entityPath(String entityType, YangInstanceIdentifier entityId) {
-        return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME).
-                nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME).
-                        nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).build();
-
+    static final QName ENTITY_QNAME = Entity.QNAME;
+    static final QName CANDIDATE_NAME_QNAME = QName.create(Candidate.QNAME, "name").intern();
+    static final QName ENTITY_ID_QNAME = QName.create(ENTITY_QNAME, "id").intern();
+    static final QName ENTITY_OWNER_QNAME = QName.create(ENTITY_QNAME, "owner").intern();
+    static final QName ENTITY_TYPE_QNAME = QName.create(EntityType.QNAME, "type").intern();
+
+    static final NodeIdentifier ENTITY_OWNERS_NODE_ID = NodeIdentifier.create(EntityOwners.QNAME);
+    static final NodeIdentifier ENTITY_OWNER_NODE_ID = NodeIdentifier.create(ENTITY_OWNER_QNAME);
+    static final NodeIdentifier ENTITY_NODE_ID = NodeIdentifier.create(ENTITY_QNAME);
+    static final NodeIdentifier ENTITY_ID_NODE_ID = NodeIdentifier.create(ENTITY_ID_QNAME);
+    static final NodeIdentifier ENTITY_TYPE_NODE_ID = NodeIdentifier.create(ENTITY_TYPE_QNAME);
+    static final NodeIdentifier CANDIDATE_NODE_ID = NodeIdentifier.create(Candidate.QNAME);
+    static final NodeIdentifier CANDIDATE_NAME_NODE_ID = NodeIdentifier.create(CANDIDATE_NAME_QNAME);
+    static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.create(ENTITY_OWNERS_NODE_ID);
+    static final YangInstanceIdentifier ENTITY_TYPES_PATH = ENTITY_OWNERS_PATH.node(EntityType.QNAME).toOptimized();
+
+    private EntityOwnersModel() {
     }
 
-    static YangInstanceIdentifier candidatePath(String entityType, YangInstanceIdentifier entityId,
-            String candidateName) {
-        return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME).
-                nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME).
-                        nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).node(Candidate.QNAME).
-                                nodeWithKey(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
+    static YangInstanceIdentifier entityPath(final String entityType, final YangInstanceIdentifier entityId) {
+        return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
+                .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
+                        .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).build();
+
+    }
 
+    static YangInstanceIdentifier candidatePath(final String entityType, final YangInstanceIdentifier entityId,
+            final String candidateName) {
+        return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
+                .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
+                        .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).node(Candidate.QNAME)
+                                .nodeWithKey(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
     }
 
-    static YangInstanceIdentifier candidatePath(YangInstanceIdentifier entityPath, String candidateName) {
+    static YangInstanceIdentifier candidatePath(final YangInstanceIdentifier entityPath, final String candidateName) {
         return YangInstanceIdentifier.builder(entityPath).node(Candidate.QNAME).nodeWithKey(
                 Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
     }
 
-    static NodeIdentifierWithPredicates candidateNodeKey(String candidateName) {
-        return new NodeIdentifierWithPredicates(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
+    static NodeIdentifierWithPredicates candidateNodeKey(final String candidateName) {
+        return NodeIdentifierWithPredicates.of(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
     }
 
-    static NormalizedNode<?, ?> entityOwnersWithCandidate(String entityType, YangInstanceIdentifier entityId,
-            String candidateName) {
+    static NormalizedNode<?, ?> entityOwnersWithCandidate(final String entityType,
+            final YangInstanceIdentifier entityId, final String candidateName) {
         return entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entityType,
                 entityEntryWithCandidateEntry(entityId, candidateName)));
     }
 
-    static ContainerNode entityOwnersWithEntityTypeEntry(MapEntryNode entityTypeNode) {
+    static ContainerNode entityOwnersWithEntityTypeEntry(final MapEntryNode entityTypeNode) {
         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
-                ENTITY_OWNERS_NODE_ID).addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME).
-                        addChild(entityTypeNode).build()).build();
+                ENTITY_OWNERS_NODE_ID).addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME)
+                        .addChild(entityTypeNode).build()).build();
     }
 
-    static MapEntryNode entityTypeEntryWithEntityEntry(String entityType, MapEntryNode entityNode) {
+    static MapEntryNode entityTypeEntryWithEntityEntry(final String entityType, final MapEntryNode entityNode) {
         return ImmutableNodes.mapEntryBuilder(EntityType.QNAME,
                 ENTITY_TYPE_QNAME, entityType).addChild(ImmutableNodes.mapNodeBuilder(
                         ENTITY_QNAME).addChild(entityNode).build()).build();
     }
 
-    static MapEntryNode entityEntryWithCandidateEntry(YangInstanceIdentifier entityId, String candidateName) {
+    static MapEntryNode entityEntryWithCandidateEntry(final YangInstanceIdentifier entityId,
+            final String candidateName) {
         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
                 candidateEntry(candidateName)).build();
     }
 
-    static MapNode candidateEntry(String candidateName) {
-        return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(Candidate.QNAME)).
-                addChild(candidateMapEntry(candidateName)).build();
+    static MapNode candidateEntry(final String candidateName) {
+        return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(Candidate.QNAME))
+                .addChild(candidateMapEntry(candidateName)).build();
     }
 
-    static MapEntryNode candidateMapEntry(String candidateName) {
+    static MapEntryNode candidateMapEntry(final String candidateName) {
         return ImmutableNodes.mapEntry(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
     }
 
-    static MapEntryNode entityEntryWithOwner(YangInstanceIdentifier entityId, String owner) {
-        return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
-                ImmutableNodes.leafNode(ENTITY_OWNER_QNAME, owner)).build();
+    static MapEntryNode entityEntryWithOwner(final YangInstanceIdentifier entityId, final String owner) {
+        return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId)
+                .addChild(ImmutableNodes.leafNode(ENTITY_OWNER_QNAME, owner != null ? owner : ""))
+                .build();
     }
 
-    public static String entityTypeFromEntityPath(YangInstanceIdentifier entityPath){
+    public static String entityTypeFromEntityPath(final YangInstanceIdentifier entityPath) {
         YangInstanceIdentifier parent = entityPath;
-        while(!parent.isEmpty()) {
+        while (!parent.isEmpty()) {
             if (EntityType.QNAME.equals(parent.getLastPathArgument().getNodeType())) {
-                YangInstanceIdentifier.NodeIdentifierWithPredicates entityTypeLastPathArgument = (YangInstanceIdentifier.NodeIdentifierWithPredicates) parent.getLastPathArgument();
-                return (String) entityTypeLastPathArgument.getKeyValues().get(ENTITY_TYPE_QNAME);
+                YangInstanceIdentifier.NodeIdentifierWithPredicates entityTypeLastPathArgument =
+                        (YangInstanceIdentifier.NodeIdentifierWithPredicates) parent.getLastPathArgument();
+                return (String) entityTypeLastPathArgument.getValue(ENTITY_TYPE_QNAME);
             }
             parent = parent.getParent();
         }
         return null;
     }
 
-    static Entity createEntity(YangInstanceIdentifier entityPath) {
+    static DOMEntity createEntity(final YangInstanceIdentifier entityPath) {
         String entityType = null;
         YangInstanceIdentifier entityId = null;
-        for(PathArgument pathArg: entityPath.getPathArguments()) {
-            if(pathArg instanceof NodeIdentifierWithPredicates) {
+        for (PathArgument pathArg: entityPath.getPathArguments()) {
+            if (pathArg instanceof NodeIdentifierWithPredicates) {
                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
-                Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
-                if(ENTITY_TYPE_QNAME.equals(key.getKey())) {
+                Entry<QName, Object> key = nodeKey.entrySet().iterator().next();
+                if (ENTITY_TYPE_QNAME.equals(key.getKey())) {
                     entityType = key.getValue().toString();
-                } else if(ENTITY_ID_QNAME.equals(key.getKey())) {
+                } else if (ENTITY_ID_QNAME.equals(key.getKey())) {
                     entityId = (YangInstanceIdentifier) key.getValue();
                 }
             }
         }
 
-        return new Entity(entityType, entityId);
+        return new DOMEntity(entityType, entityId);
     }
 }