Binding v2 - Use YangTextSnippet for generation
[mdsal.git] / binding2 / mdsal-binding2-generator-api / src / main / java / org / opendaylight / mdsal / binding / javav2 / model / api / YangSourceDefinition.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.mdsal.binding.javav2.model.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
27
28 /**
29  * DTO capturing the YANG source definition which lead to a {@link GeneratedType} being emitted.
30  *
31  * @author Robert Varga
32  */
33 @Beta
34 @NonNullByDefault
35 public abstract class YangSourceDefinition {
36     public static final class Multiple extends YangSourceDefinition {
37         private final List<? extends SchemaNode> nodes;
38
39         Multiple(final ModuleEffectiveStatement module, final Collection<? extends SchemaNode> nodes) {
40             super(module);
41             this.nodes = ImmutableList.copyOf(nodes);
42         }
43
44         /**
45          * Return the defining SchemaNodes. Each node is guaranteed to implement {@link EffectiveStatement} and have
46          * a corresponding declared statement.
47          *
48          * @return defining SchemaNodes, guaranteed to be non-empty
49          */
50         public List<? extends SchemaNode> getNodes() {
51             return nodes.stream().filter(node -> {
52                 return node instanceof EffectiveStatement && ((EffectiveStatement<?, ?>) node).getDeclared() != null;
53             }).collect(Collectors.toList());
54         }
55     }
56
57     public static final class Single extends YangSourceDefinition {
58         private final DocumentedNode node;
59
60         Single(final ModuleEffectiveStatement module, final DocumentedNode node) {
61             super(module);
62             this.node = requireNonNull(node);
63         }
64
65         /**
66          * Return the defining DocumentedNode. The node is guaranteed to implement {@link EffectiveStatement} and have
67          * a corresponding declared statement.
68          *
69          * @return defining SchemaNodes, guaranteed to be non-empty
70          */
71         public DocumentedNode getNode() {
72             return node;
73         }
74     }
75
76     private final ModuleEffectiveStatement module;
77
78     private YangSourceDefinition(final ModuleEffectiveStatement module) {
79         this.module = requireNonNull(module);
80     }
81
82     public static Optional<YangSourceDefinition> of(final Module module) {
83         if (module instanceof ModuleEffectiveStatement) {
84             final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
85             final ModuleStatement declared = effective.getDeclared();
86             if (declared != null) {
87                 return Optional.of(new Single(effective, module));
88             }
89         }
90         return Optional.empty();
91     }
92
93     public static Optional<YangSourceDefinition> of(final Module module, final SchemaNode node) {
94         if (module instanceof ModuleEffectiveStatement) {
95             final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
96             if (node instanceof EffectiveStatement) {
97                 final DeclaredStatement<?> declared = ((EffectiveStatement<?, ?>) node).getDeclared();
98                 if (declared != null) {
99                     return Optional.of(new Single(effective, node));
100                 }
101             }
102         }
103         return Optional.empty();
104     }
105
106     public static Optional<YangSourceDefinition> of(final Module module, final Collection<? extends SchemaNode> nodes) {
107         checkArgument(!nodes.isEmpty());
108
109         if (module instanceof ModuleEffectiveStatement) {
110             final ModuleEffectiveStatement effective = (ModuleEffectiveStatement) module;
111             final boolean anyDeclared = nodes.stream().anyMatch(node -> {
112                 return node instanceof EffectiveStatement && ((EffectiveStatement<?, ?>) node).getDeclared() != null;
113             });
114             if (anyDeclared) {
115                 return Optional.of(new Multiple(effective, nodes));
116             }
117         }
118         return Optional.empty();
119     }
120
121     /**
122      * Return the defining YANG module.
123      *
124      * @return Defining YANG module.
125      */
126     public final ModuleEffectiveStatement getModule() {
127         return module;
128     }
129 }