From 366d11917e78439aea6dfe0b1cb5aefc9978158d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 5 Mar 2019 10:32:15 +0100 Subject: [PATCH] Add AnnotationSchemaNodeAware interface It seems that having annotations indexed at the SchemaContext level. To support that, add AnnotationSchemaNodeAware interface and make it trivially implemented by SimpleSchemaContext. Also retrofit AnnotationSchemaNode.find(SchemaContext) to recognize AnnotationSchemaNodeAware and defer to the index if its available. JIRA: YANGTOOLS-960 Change-Id: I1f3cd68a8356b20bceb2d0d620992ade10649e5b Signed-off-by: Robert Varga --- .../model/api/AnnotationSchemaNode.java | 12 +++++--- .../model/api/AnnotationSchemaNodeAware.java | 30 +++++++++++++++++++ ...nnotationSchemaNodeAwareSchemaContext.java | 20 +++++++++++++ yang/yang-model-util/pom.xml | 4 +++ .../yang/model/util/SimpleSchemaContext.java | 15 +++++++++- .../model/util/SimpleSchemaContextTest.java | 1 + 6 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAware.java create mode 100644 yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAwareSchemaContext.java diff --git a/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNode.java b/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNode.java index d7e69da3ba..20c3b9bd49 100644 --- a/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNode.java +++ b/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNode.java @@ -12,6 +12,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import java.util.Map; import java.util.Optional; +import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -33,7 +34,11 @@ public interface AnnotationSchemaNode extends UnknownSchemaNode, TypeAware { * by the SchemaContext.. * @throws NullPointerException if any of the arguments is null */ - static Optional find(final SchemaContext context, final QName qname) { + static @NonNull Optional find(final SchemaContext context, final QName qname) { + if (context instanceof AnnotationSchemaNodeAware) { + return ((AnnotationSchemaNodeAware) context).findAnnotation(qname); + } + return context.findModule(qname.getModule()).flatMap(module -> { return module.getUnknownSchemaNodes().stream() .filter(AnnotationSchemaNode.class::isInstance) @@ -49,13 +54,12 @@ public interface AnnotationSchemaNode extends UnknownSchemaNode, TypeAware { * @return {@link AnnotationSchemaNode}s supported by the SchemaContext.. * @throws NullPointerException if context is null */ - static Map findAll(final SchemaContext context) { + static @NonNull Map findAll(final SchemaContext context) { final Builder builder = ImmutableMap.builder(); for (Module module : context.getModules()) { for (UnknownSchemaNode node : module.getUnknownSchemaNodes()) { if (node instanceof AnnotationSchemaNode) { - final AnnotationSchemaNode annotation = (AnnotationSchemaNode) node; - builder.put(annotation.getQName(), annotation); + builder.put(node.getQName(), (AnnotationSchemaNode) node); } } } diff --git a/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAware.java b/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAware.java new file mode 100644 index 0000000000..d7cee7e92f --- /dev/null +++ b/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAware.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.rfc7952.model.api; + +import com.google.common.annotations.Beta; +import java.util.Optional; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.yangtools.yang.common.QName; + +/** + * Interface for entities which can lookup {@link AnnotationSchemaNode}s based on their name. + * + * @author Robert Varga + */ +@Beta +public interface AnnotationSchemaNodeAware { + /** + * Find an annotation based on its QName. + * + * @param qname Annotation name + * @return AnnotationSchemaNode if found + * @throws NullPointerException if {@code qname} is null + */ + @NonNull Optional findAnnotation(QName qname); +} diff --git a/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAwareSchemaContext.java b/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAwareSchemaContext.java new file mode 100644 index 0000000000..886a3ccd8d --- /dev/null +++ b/yang/rfc7952-model-api/src/main/java/org/opendaylight/yangtools/rfc7952/model/api/AnnotationSchemaNodeAwareSchemaContext.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.rfc7952.model.api; + +import com.google.common.annotations.Beta; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; + +/** + * A {@link SchemaContext} which is also {@link AnnotationSchemaNodeAware}. This is a utility capture for users which + * need to deal with SchemaContext, but also require it to have annotation indices. + */ +@Beta +public interface AnnotationSchemaNodeAwareSchemaContext extends AnnotationSchemaNodeAware, SchemaContext { + +} diff --git a/yang/yang-model-util/pom.xml b/yang/yang-model-util/pom.xml index 70113bc366..10343c3cee 100644 --- a/yang/yang-model-util/pom.xml +++ b/yang/yang-model-util/pom.xml @@ -26,6 +26,10 @@ org.opendaylight.yangtools yang-model-api + + org.opendaylight.yangtools + rfc7952-model-api + org.opendaylight.yangtools diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContext.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContext.java index 0dfbb12abd..fe4e17c646 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContext.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContext.java @@ -7,6 +7,8 @@ */ package org.opendaylight.yangtools.yang.model.util; +import static java.util.Objects.requireNonNull; + import com.google.common.annotations.Beta; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; @@ -20,8 +22,12 @@ import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.TreeMap; +import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode; +import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNodeAwareSchemaContext; +import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.Module; @@ -31,11 +37,12 @@ import org.opendaylight.yangtools.yang.model.api.Module; * any extensive analysis to ensure the resulting object complies to SchemaContext interface. */ @Beta -public class SimpleSchemaContext extends AbstractSchemaContext { +public class SimpleSchemaContext extends AbstractSchemaContext implements AnnotationSchemaNodeAwareSchemaContext { private final ImmutableSetMultimap namespaceToModules; private final ImmutableSetMultimap nameToModules; private final ImmutableMap moduleMap; private final ImmutableSet modules; + private final ImmutableMap annotations; protected SimpleSchemaContext(final Set modules) { /* @@ -71,6 +78,7 @@ public class SimpleSchemaContext extends AbstractSchemaContext { namespaceToModules = ImmutableSetMultimap.copyOf(nsMap); nameToModules = ImmutableSetMultimap.copyOf(nameMap); moduleMap = moduleMapBuilder.build(); + annotations = ImmutableMap.copyOf(AnnotationSchemaNode.findAll(this)); } /** @@ -86,6 +94,11 @@ public class SimpleSchemaContext extends AbstractSchemaContext { return modules; } + @Override + public final Optional findAnnotation(final QName qname) { + return Optional.ofNullable(annotations.get(requireNonNull(qname))); + } + @Override protected Map getModuleMap() { return moduleMap; diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContextTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContextTest.java index 54e3c3fa85..411ded51f6 100644 --- a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContextTest.java +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContextTest.java @@ -78,6 +78,7 @@ public class SimpleSchemaContextTest { doReturn(mod.toString()).when(ret).toString(); doReturn(ImmutableSet.of()).when(ret).getImports(); doReturn(ImmutableSet.of()).when(ret).getSubmodules(); + doReturn(ImmutableList.of()).when(ret).getUnknownSchemaNodes(); return ret; } } -- 2.36.6