Fix eos entity lookups with YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / main / java / org / opendaylight / controller / eos / akka / owner / supervisor / command / GetEntitiesReply.java
index ea8a5a8cc1007f1ec5266f494ede5b23a1a6aa89..1cfa31ab0052a4eb2be52f7f08c75f0175a637aa 100644 (file)
@@ -20,6 +20,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.EntityName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.EntityType;
@@ -27,6 +28,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controll
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.GetEntitiesOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.NodeName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.get.entities.output.EntitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.core.general.entity.rev150930.Entity;
 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -46,7 +48,7 @@ public final class GetEntitiesReply extends OwnerSupervisorReply implements Seri
         this.owners = ImmutableMap.copyOf(owners);
     }
 
-    public @NonNull GetEntitiesOutput toOutput() {
+    public @NonNull GetEntitiesOutput toOutput(final BindingInstanceIdentifierCodec iidCodec) {
         final Set<DOMEntity> entities = new HashSet<>();
         entities.addAll(owners.keySet());
         entities.addAll(candidates.keySet());
@@ -56,7 +58,7 @@ public final class GetEntitiesReply extends OwnerSupervisorReply implements Seri
                 .map(entity -> {
                     final EntitiesBuilder eb = new EntitiesBuilder()
                         .setType(new EntityType(entity.getType()))
-                        .setName(extractName(entity))
+                        .setName(extractName(entity, iidCodec))
                         .setCandidateNodes(candidates.get(entity).stream()
                             .map(NodeName::new).collect(Collectors.toUnmodifiableList()));
 
@@ -70,8 +72,21 @@ public final class GetEntitiesReply extends OwnerSupervisorReply implements Seri
             .build();
     }
 
-    private static EntityName extractName(final DOMEntity entity) {
-        final PathArgument last = entity.getIdentifier().getLastPathArgument();
+    /**
+     * if the entity is general entity then shorthand the name to only the last path argument, otherwise return
+     * full YIID path encoded as string.
+     *
+     * @param entity Entity to extract the name from
+     * @param iidCodec codec to encode entity name back to InstanceIdentifier if needed
+     * @return Extracted name
+     */
+    private static EntityName extractName(final DOMEntity entity, final BindingInstanceIdentifierCodec iidCodec) {
+        final var id = entity.getIdentifier();
+        if (id.isEmpty() || !id.getPathArguments().get(0).getNodeType().equals(Entity.QNAME)) {
+            return new EntityName(iidCodec.toBinding(id));
+        }
+
+        final PathArgument last = id.getLastPathArgument();
         verify(last instanceof NodeIdentifierWithPredicates, "Unexpected last argument %s", last);
         final Object value = Iterables.getOnlyElement(((NodeIdentifierWithPredicates) last).values());
         verify(value instanceof String, "Unexpected predicate value %s", value);