2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.model.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
29 * DTO capturing the YANG source definition which lead to a {@link GeneratedType} being emitted.
31 * @author Robert Varga
35 public abstract class YangSourceDefinition {
36 public static final class Multiple extends YangSourceDefinition {
37 private final List<? extends SchemaNode> nodes;
39 Multiple(final ModuleEffectiveStatement module, final Collection<? extends SchemaNode> nodes) {
41 this.nodes = ImmutableList.copyOf(nodes);
45 * Return the defining SchemaNodes. Each node is guaranteed to implement {@link EffectiveStatement} and have
46 * a corresponding declared statement.
48 * @return defining SchemaNodes, guaranteed to be non-empty
50 public List<? extends SchemaNode> getNodes() {
51 return nodes.stream().filter(node -> node instanceof EffectiveStatement
52 && ((EffectiveStatement<?, ?>) node).getDeclared() != null).collect(Collectors.toList());
56 public static final class Single extends YangSourceDefinition {
57 private final DocumentedNode node;
59 Single(final ModuleEffectiveStatement module, final DocumentedNode node) {
61 this.node = requireNonNull(node);
65 * Return the defining DocumentedNode. The node is guaranteed to implement {@link EffectiveStatement} and have
66 * a corresponding declared statement.
68 * @return defining SchemaNodes, guaranteed to be non-empty
70 public DocumentedNode getNode() {
75 private final ModuleEffectiveStatement module;
77 private YangSourceDefinition(final ModuleEffectiveStatement module) {
78 this.module = requireNonNull(module);
81 public static Optional<YangSourceDefinition> of(final Module module) {
82 if (module instanceof ModuleEffectiveStatement) {
83 final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
84 final ModuleStatement declared = effective.getDeclared();
85 if (declared != null) {
86 return Optional.of(new Single(effective, module));
89 return Optional.empty();
92 public static Optional<YangSourceDefinition> of(final Module module, final SchemaNode node) {
93 if (module instanceof ModuleEffectiveStatement) {
94 final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
95 if (node instanceof EffectiveStatement) {
96 final DeclaredStatement<?> declared = ((EffectiveStatement<?, ?>) node).getDeclared();
97 if (declared != null) {
98 return Optional.of(new Single(effective, node));
102 return Optional.empty();
105 public static Optional<YangSourceDefinition> of(final Module module, final Collection<? extends SchemaNode> nodes) {
106 checkArgument(!nodes.isEmpty());
108 if (module instanceof ModuleEffectiveStatement) {
109 final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
110 final boolean anyDeclared = nodes.stream().anyMatch(node ->
111 node instanceof EffectiveStatement && ((EffectiveStatement<?, ?>) node).getDeclared() != null);
113 return Optional.of(new Multiple(effective, nodes));
116 return Optional.empty();
120 * Return the defining YANG module.
122 * @return Defining YANG module.
124 public final ModuleEffectiveStatement getModule() {