Fix checkstyle in mdsal-binding-generator-impl
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / util / YangSchemaUtils.java
index 109ff9b126e6e0a9feb98db0748b453cd914598c..5386a77593848be8dea0c6ef27edb942ee0465a6 100644 (file)
@@ -12,11 +12,12 @@ import static com.google.common.base.Preconditions.checkState;
 
 import com.google.common.base.Preconditions;
 import java.net.URI;
-import java.util.Date;
 import java.util.Iterator;
-import javax.annotation.Nullable;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -34,22 +35,22 @@ public final class YangSchemaUtils {
         throw new UnsupportedOperationException("Helper class. Instantiation is prohibited");
     }
 
-    public static QName getAugmentationQName(final AugmentationSchema augmentation) {
+    public static QName getAugmentationQName(final AugmentationSchemaNode augmentation) {
         checkNotNull(augmentation, "Augmentation must not be null.");
         final QName identifier = getAugmentationIdentifier(augmentation);
-        if(identifier != null) {
+        if (identifier != null) {
             return identifier;
         }
         URI namespace = null;
-        Date revision = null;
-        if(augmentation instanceof NamespaceRevisionAware) {
+        Optional<Revision> revision = null;
+        if (augmentation instanceof NamespaceRevisionAware) {
             namespace = ((NamespaceRevisionAware) augmentation).getNamespace();
             revision = ((NamespaceRevisionAware) augmentation).getRevision();
         }
-        if(namespace == null || revision == null) {
-            for(final DataSchemaNode child : augmentation.getChildNodes()) {
+        if (namespace == null || revision == null) {
+            for (DataSchemaNode child : augmentation.getChildNodes()) {
                 // Derive QName from child nodes
-                if(!child.isAugmenting()) {
+                if (!child.isAugmenting()) {
                     namespace = child.getQName().getNamespace();
                     revision = child.getQName().getRevision();
                     break;
@@ -58,34 +59,33 @@ public final class YangSchemaUtils {
         }
         checkState(namespace != null, "Augmentation namespace must not be null");
         checkState(revision != null, "Augmentation revision must not be null");
-        // FIXME: Allways return a qname with module namespace.
-        return QName.create(namespace,revision, "foo_augment");
+        // FIXME: Always return a qname with module namespace.
+        return QName.create(namespace, revision, "foo_augment");
     }
 
-    public static QName getAugmentationIdentifier(final AugmentationSchema augmentation) {
-        for(final UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
-            if(AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
+    public static QName getAugmentationIdentifier(final AugmentationSchemaNode augmentation) {
+        for (final UnknownSchemaNode extension : augmentation.getUnknownSchemaNodes()) {
+            if (AUGMENT_IDENTIFIER.equals(extension.getNodeType().getLocalName())) {
                 return extension.getQName();
             }
         }
         return null;
     }
 
-    @Nullable
-    public static TypeDefinition<?> findTypeDefinition(final SchemaContext context, final SchemaPath path) {
+    public static @Nullable TypeDefinition<?> findTypeDefinition(final SchemaContext context, final SchemaPath path) {
         final Iterator<QName> arguments = path.getPathFromRoot().iterator();
         Preconditions.checkArgument(arguments.hasNext(), "Type Definition path must contain at least one element.");
 
         QName currentArg = arguments.next();
-        DataNodeContainer currentNode = context.findModuleByNamespaceAndRevision(currentArg.getNamespace(), currentArg.getRevision());
-        if(currentNode == null) {
+        DataNodeContainer currentNode = context.findModule(currentArg.getModule()).orElse(null);
+        if (currentNode == null) {
             return null;
         }
         // Last argument is type definition, so we need to cycle until we hit last argument.
-        while(arguments.hasNext()) {
+        while (arguments.hasNext()) {
             // Nested private type - we need to find container/grouping to which type belongs.
             final DataSchemaNode child = currentNode.getDataChildByName(currentArg);
-            if(child instanceof DataNodeContainer) {
+            if (child instanceof DataNodeContainer) {
                 currentNode = (DataNodeContainer) child;
             } else if (child instanceof ChoiceSchemaNode) {
                 final QName caseQName = arguments.next();
@@ -93,7 +93,7 @@ public final class YangSchemaUtils {
                 currentNode = ((ChoiceSchemaNode) child).getCaseNodeByName(caseQName);
             } else {
                 // Search in grouping
-                for (final GroupingDefinition grouping : currentNode.getGroupings()) {
+                for (GroupingDefinition grouping : currentNode.getGroupings()) {
                     if (currentArg.equals(grouping.getQName())) {
                         currentNode = grouping;
                         break;
@@ -103,8 +103,8 @@ public final class YangSchemaUtils {
             currentArg = arguments.next();
         }
 
-        for(final TypeDefinition<?> typedef : currentNode.getTypeDefinitions()) {
-            if(typedef.getQName().equals(currentArg)) {
+        for (TypeDefinition<?> typedef : currentNode.getTypeDefinitions()) {
+            if (typedef.getQName().equals(currentArg)) {
                 return typedef;
             }
         }