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.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
27 * DTO capturing the YANG source definition which lead to a {@link GeneratedType} being emitted.
31 public abstract sealed class YangSourceDefinition {
32 public static final class Multiple extends YangSourceDefinition {
33 private final List<? extends SchemaNode> nodes;
35 Multiple(final ModuleEffectiveStatement module, final Collection<? extends SchemaNode> nodes) {
37 this.nodes = ImmutableList.copyOf(nodes);
41 * Return the defining SchemaNodes. Each node is guaranteed to implement {@link EffectiveStatement} and have
42 * a corresponding declared statement.
44 * @return defining SchemaNodes, guaranteed to be non-empty
46 public List<? extends SchemaNode> getNodes() {
48 .filter(YangSourceDefinition::hasDeclaredStatement)
49 .collect(Collectors.toList());
53 public static final class Single extends YangSourceDefinition {
54 private final DocumentedNode node;
56 Single(final ModuleEffectiveStatement module, final DocumentedNode node) {
58 this.node = requireNonNull(node);
62 * Return the defining DocumentedNode. The node is guaranteed to implement {@link EffectiveStatement} and have
63 * a corresponding declared statement.
65 * @return defining SchemaNodes, guaranteed to be non-empty
67 public DocumentedNode getNode() {
72 private final ModuleEffectiveStatement module;
74 private YangSourceDefinition(final ModuleEffectiveStatement module) {
75 this.module = requireNonNull(module);
78 public static Optional<YangSourceDefinition> of(final Module module) {
79 final ModuleEffectiveStatement effective = module.asEffectiveStatement();
80 return effective.getDeclared() != null ? Optional.of(new Single(effective, module)) : Optional.empty();
83 public static Optional<YangSourceDefinition> of(final Module module, final SchemaNode node) {
84 return of(module.asEffectiveStatement(), node);
87 public static Optional<YangSourceDefinition> of(final ModuleEffectiveStatement module, final SchemaNode node) {
88 return hasDeclaredStatement(node) ? Optional.of(new Single(module, node)) : Optional.empty();
91 public static Optional<YangSourceDefinition> of(final ModuleEffectiveStatement module,
92 final EffectiveStatement<?, ?> effective) {
93 return effective instanceof DocumentedNode node && effective.getDeclared() != null
94 ? Optional.of(new Single(module, node)) : Optional.empty();
97 public static Optional<YangSourceDefinition> of(final Module module, final Collection<? extends SchemaNode> nodes) {
98 checkArgument(!nodes.isEmpty());
100 return nodes.stream().anyMatch(YangSourceDefinition::hasDeclaredStatement)
101 ? Optional.of(new Multiple(module.asEffectiveStatement(), nodes))
106 * Return the defining YANG module.
108 * @return Defining YANG module.
110 public final ModuleEffectiveStatement getModule() {
114 private static boolean hasDeclaredStatement(final SchemaNode schemaNode) {
115 return schemaNode instanceof EffectiveStatement<?, ?> effective && effective.getDeclared() != null;