Cleanup test format
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / main / java / org / opendaylight / controller / eos / akka / owner / checker / command / GetEntitiesReply.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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 package org.opendaylight.controller.eos.akka.owner.checker.command;
9
10 import static com.google.common.base.Verify.verify;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSetMultimap;
14 import com.google.common.collect.Iterables;
15 import java.io.Serializable;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.eos.akka.owner.supervisor.command.GetEntitiesBackendReply;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
23 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.EntityName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.EntityType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.GetEntitiesOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.GetEntitiesOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.NodeName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.get.entities.output.EntitiesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.core.general.entity.rev150930.Entity;
31 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34
35 public final class GetEntitiesReply extends StateCheckerReply implements Serializable {
36     private static final long serialVersionUID = 1L;
37
38     private final ImmutableSetMultimap<DOMEntity, String> candidates;
39     private final ImmutableMap<DOMEntity, String> owners;
40
41     public GetEntitiesReply(final GetEntitiesBackendReply response) {
42         this.owners = response.getOwners();
43         this.candidates = response.getCandidates();
44     }
45
46     public GetEntitiesReply(final Map<DOMEntity, Set<String>> candidates, final Map<DOMEntity, String> owners) {
47         final ImmutableSetMultimap.Builder<DOMEntity, String> builder = ImmutableSetMultimap.builder();
48         for (Map.Entry<DOMEntity, Set<String>> entry : candidates.entrySet()) {
49             builder.putAll(entry.getKey(), entry.getValue());
50         }
51         this.candidates = builder.build();
52         this.owners = ImmutableMap.copyOf(owners);
53     }
54
55     public @NonNull GetEntitiesOutput toOutput(final BindingInstanceIdentifierCodec iidCodec) {
56         final Set<DOMEntity> entities = new HashSet<>();
57         entities.addAll(owners.keySet());
58         entities.addAll(candidates.keySet());
59
60         return new GetEntitiesOutputBuilder()
61             .setEntities(entities.stream()
62                 .map(entity -> {
63                     final EntitiesBuilder eb = new EntitiesBuilder()
64                         .setType(new EntityType(entity.getType()))
65                         .setName(extractName(entity, iidCodec))
66                         .setCandidateNodes(candidates.get(entity).stream()
67                             .map(NodeName::new).collect(Collectors.toUnmodifiableList()));
68
69                     final String owner = owners.get(entity);
70                     if (owner != null) {
71                         eb.setOwnerNode(new NodeName(owner));
72                     }
73                     return eb.build();
74                 })
75                 .collect(BindingMap.toMap()))
76             .build();
77     }
78
79     /**
80      * if the entity is general entity then shorthand the name to only the last path argument, otherwise return
81      * full YIID path encoded as string.
82      *
83      * @param entity Entity to extract the name from
84      * @param iidCodec codec to encode entity name back to InstanceIdentifier if needed
85      * @return Extracted name
86      */
87     private static EntityName extractName(final DOMEntity entity, final BindingInstanceIdentifierCodec iidCodec) {
88         final var id = entity.getIdentifier();
89         if (id.isEmpty() || !id.getPathArguments().get(0).getNodeType().equals(Entity.QNAME)) {
90             return new EntityName(iidCodec.toBinding(id));
91         }
92
93         final PathArgument last = id.getLastPathArgument();
94         verify(last instanceof NodeIdentifierWithPredicates, "Unexpected last argument %s", last);
95         final Object value = Iterables.getOnlyElement(((NodeIdentifierWithPredicates) last).values());
96         verify(value instanceof String, "Unexpected predicate value %s", value);
97         return new EntityName((String) value);
98     }
99 }