2 * Copyright (c) 2017 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.yangtools.rfc7952.model.api;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
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;
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.
27 public interface AnnotationSchemaNode extends UnknownSchemaNode, TypeAware {
29 * Find specified annotation if it is supported by the specified SchemaContext.
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
37 static @NonNull Optional<AnnotationSchemaNode> find(final SchemaContext context, final QName qname) {
38 if (context instanceof AnnotationSchemaNodeAware) {
39 return ((AnnotationSchemaNodeAware) context).findAnnotation(qname);
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()))
51 * Find all annotations supported by a SchemaContext.
53 * @param context SchemaContext to search
54 * @return {@link AnnotationSchemaNode}s supported by the SchemaContext..
55 * @throws NullPointerException if context is null
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);
67 return builder.build();
71 AnnotationEffectiveStatement asEffectiveStatement();