2 * Copyright (c) 2014 Cisco Systems, Inc. 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.sal.binding.generator.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import static com.google.common.base.Preconditions.checkState;
15 import java.util.Date;
16 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 public final class YangSchemaUtils {
29 public static final String AUGMENT_IDENTIFIER = "augment-identifier";
31 private YangSchemaUtils() {
32 throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
35 public static QName getAugmentationQName(final AugmentationSchema augmentation) {
36 checkNotNull(augmentation, "Augmentation must not be null.");
37 QName identifier = getAugmentationIdentifier(augmentation);
38 if(identifier != null) {
43 if(augmentation instanceof NamespaceRevisionAware) {
44 namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
45 revision = ((NamespaceRevisionAware) augmentation).getRevision();
47 if(namespace == null || revision == null) {
48 for(DataSchemaNode child : augmentation.getChildNodes()) {
49 // Derive QName from child nodes
50 if(!child.isAugmenting()) {
51 namespace = child.getQName().getNamespace();
52 revision = child.getQName().getRevision();
57 checkState(namespace != null, "Augmentation namespace must not be null");
58 checkState(revision != null, "Augmentation revision must not be null");
59 // FIXME: Allways return a qname with module namespace.
60 return QName.create(namespace,revision, "foo_augment");
63 public static QName getAugmentationIdentifier(final AugmentationSchema augmentation) {
64 for(UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
65 if(AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
66 return extension.getQName();
72 public static TypeDefinition<?> findTypeDefinition(final SchemaContext context, final SchemaPath path) {
73 List<QName> arguments = path.getPath();
74 QName first = arguments.get(0);
75 QName typeQName = arguments.get(arguments.size() -1);
76 DataNodeContainer previous = context.findModuleByNamespaceAndRevision(first.getNamespace(), first.getRevision());
77 if(previous == null) {
80 checkArgument(arguments.size() == 1);
81 for(TypeDefinition<?> typedef : previous.getTypeDefinitions()) {
82 if(typedef.getQName().equals(typeQName)) {