Populate data/ hierarchy
[yangtools.git] / yang / rfc7952-model-api / src / main / java / org / opendaylight / yangtools / rfc7952 / model / api / AnnotationSchemaNode.java
1 /*
2  * Copyright (c) 2017 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.yangtools.rfc7952.model.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import java.util.Map;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.api.TypeAware;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21
22 /**
23  * Represents the effect of 'annotation' extension, as defined in
24  * <a href="https://tools.ietf.org/html/rfc7952">RFC7952</a>, being attached to a SchemaNode.
25  */
26 @Beta
27 public interface AnnotationSchemaNode extends UnknownSchemaNode, TypeAware {
28     /**
29      * Find specified annotation if it is supported by the specified SchemaContext.
30      *
31      * @param context SchemaContext to search
32      * @param qname Annotation name
33      * @return {@link AnnotationSchemaNode} corresponding to specified name, or empty if it is not supported
34      *         by the SchemaContext..
35      * @throws NullPointerException if any of the arguments is null
36      */
37     static @NonNull Optional<AnnotationSchemaNode> find(final SchemaContext context, final QName qname) {
38         if (context instanceof AnnotationSchemaNodeAware) {
39             return ((AnnotationSchemaNodeAware) context).findAnnotation(qname);
40         }
41
42         return context.findModule(qname.getModule())
43             .flatMap(module -> module.getUnknownSchemaNodes().stream()
44                 .filter(AnnotationSchemaNode.class::isInstance)
45                 .map(AnnotationSchemaNode.class::cast)
46                 .filter(annotation -> qname.equals(annotation.getQName()))
47                 .findAny());
48     }
49
50     /**
51      * Find all annotations supported by a SchemaContext.
52      *
53      * @param context SchemaContext to search
54      * @return {@link AnnotationSchemaNode}s supported by the SchemaContext..
55      * @throws NullPointerException if context is null
56      */
57     static @NonNull Map<QName, AnnotationSchemaNode> findAll(final SchemaContext context) {
58         final Builder<QName, AnnotationSchemaNode> builder = ImmutableMap.builder();
59         for (Module module : context.getModules()) {
60             for (UnknownSchemaNode node : module.getUnknownSchemaNodes()) {
61                 if (node instanceof AnnotationSchemaNode) {
62                     builder.put(node.getQName(), (AnnotationSchemaNode) node);
63                 }
64             }
65         }
66
67         return builder.build();
68     }
69
70     @Override
71     AnnotationEffectiveStatement asEffectiveStatement();
72 }