Cleanup GroupingUtils
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / GroupingUtils.java
index 83a37469650deff8b982956ef3f5064000cb8729..6bb165fd6aa73942e2d3e4f03184d982dcce849d 100644 (file)
@@ -7,13 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
-
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
-import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
-import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
+import com.google.common.base.Preconditions;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
@@ -23,16 +17,22 @@ import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 
 public final class GroupingUtils {
 
     private GroupingUtils() {
+        throw new UnsupportedOperationException();
     }
 
     /**
@@ -41,10 +41,9 @@ public final class GroupingUtils {
      * @param usesNode uses node
      * @throws SourceException instance of SourceException
      */
-    public static void copyFromSourceToTarget(
-            StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
-            StatementContextBase<?, ?, ?> targetCtx,
-            StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode)
+    public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
+            final StatementContextBase<?, ?, ?> targetCtx,
+            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode)
             throws SourceException {
 
         QNameModule newQNameModule = getNewQNameModule(targetCtx,
@@ -52,21 +51,16 @@ public final class GroupingUtils {
         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
         copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
                 newQNameModule);
-
     }
 
-    public static void copyDeclaredStmts(
-            StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
-            StatementContextBase<?, ?, ?> targetCtx,
-            StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            QNameModule newQNameModule) throws SourceException {
-        Collection<? extends StatementContextBase<?, ?, ?>> declaredSubstatements = sourceGrpStmtCtx
-                .declaredSubstatements();
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : declaredSubstatements) {
+    public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
+            final StatementContextBase<?, ?, ?> targetCtx,
+            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
+            final QNameModule newQNameModule) throws SourceException {
+        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
             if (needToCopyByUses(originalStmtCtx)) {
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx
-                        .createCopy(newQNameModule, targetCtx,
-                                TypeOfCopy.ADDED_BY_USES);
+                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
+                    TypeOfCopy.ADDED_BY_USES);
                 targetCtx.addEffectiveSubstatement(copy);
                 usesNode.addAsEffectOfStatement(copy);
             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
@@ -76,18 +70,14 @@ public final class GroupingUtils {
         }
     }
 
-    public static void copyEffectiveStmts(
-            StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
-            StatementContextBase<?, ?, ?> targetCtx,
-            StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            QNameModule newQNameModule) throws SourceException {
-        Collection<? extends StatementContextBase<?, ?, ?>> effectiveSubstatements = sourceGrpStmtCtx
-                .effectiveSubstatements();
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : effectiveSubstatements) {
+    public static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
+            final StatementContextBase<?, ?, ?> targetCtx,
+            final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
+            final QNameModule newQNameModule) throws SourceException {
+        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
             if (needToCopyByUses(originalStmtCtx)) {
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx
-                        .createCopy(newQNameModule, targetCtx,
-                                TypeOfCopy.ADDED_BY_USES);
+                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
+                    TypeOfCopy.ADDED_BY_USES);
                 targetCtx.addEffectiveSubstatement(copy);
                 usesNode.addAsEffectOfStatement(copy);
             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
@@ -97,30 +87,20 @@ public final class GroupingUtils {
         }
     }
 
-    public static QNameModule getNewQNameModule(
-            StatementContextBase<?, ?, ?> targetCtx,
-            StmtContext<?, ?, ?> stmtContext) {
+    public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
+            final StmtContext<?, ?, ?> stmtContext) {
         if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
             if (targetCtx.isRootContext()) {
-                return targetCtx.getFromNamespace(
-                        ModuleCtxToModuleQName.class,
-                        targetCtx);
+                return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
             }
-            if(targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
-                RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
-                return targetCtx.getFromNamespace(
-                        ModuleCtxToModuleQName.class,
-                        root);
+            if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
+                return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
             }
 
             Object targetStmtArgument = targetCtx.getStatementArgument();
             Object sourceStmtArgument = stmtContext.getStatementArgument();
-            if (targetStmtArgument instanceof QName
-                    && sourceStmtArgument instanceof QName) {
-                QName targetQName = (QName) targetStmtArgument;
-                QNameModule targetQNameModule = targetQName.getModule();
-
-                return targetQNameModule;
+            if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
+                return ((QName) targetStmtArgument).getModule();
             } else {
                 return null;
             }
@@ -129,12 +109,11 @@ public final class GroupingUtils {
         }
     }
 
-    public static boolean needToCreateNewQName(
-            StatementDefinition publicDefinition) {
+    public static boolean needToCreateNewQName(final StatementDefinition publicDefinition) {
         return true;
     }
 
-    public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
+    public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
 
         Set<StatementDefinition> noCopyDefSet = new HashSet<>();
         noCopyDefSet.add(Rfc6020Mapping.USES);
@@ -152,7 +131,7 @@ public final class GroupingUtils {
         return !noCopyDefSet.contains(def) && !dontCopyFromParentGrouping;
     }
 
-    public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
+    public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
 
         Set<StatementDefinition> reusedDefSet = new HashSet<>();
         reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
@@ -163,7 +142,7 @@ public final class GroupingUtils {
         return reusedDefSet.contains(def);
     }
 
-    public static boolean isReusedByUsesOnTop(StmtContext<?, ?, ?> stmtContext) {
+    public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
 
         Set<StatementDefinition> reusedDefSet = new HashSet<>();
         reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
@@ -174,53 +153,38 @@ public final class GroupingUtils {
     }
 
     public static void resolveUsesNode(
-            Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            StatementContextBase<?, ?, ?> targetNodeStmtCtx)
+            final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
+            final StatementContextBase<?, ?, ?> targetNodeStmtCtx)
             throws SourceException {
 
-        Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode
-                .declaredSubstatements();
+        Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode.declaredSubstatements();
         for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
-            if (StmtContextUtils.producesDeclared(subStmtCtx,
-                    RefineStatement.class)) {
+            if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
                 performRefine(subStmtCtx, targetNodeStmtCtx);
             }
         }
     }
 
-    private static void performRefine(StatementContextBase<?, ?, ?> refineCtx,
-            StatementContextBase<?, ?, ?> usesParentCtx) {
+    private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
+            final StatementContextBase<?, ?, ?> usesParentCtx) {
 
         Object refineArgument = refineCtx.getStatementArgument();
+        Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
+            "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier", refineArgument);
 
-        SchemaNodeIdentifier refineTargetNodeIdentifier;
-        if (refineArgument instanceof SchemaNodeIdentifier) {
-            refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
-        } else {
-            throw new IllegalArgumentException(
-                    "Invalid refine argument. It must be instance of SchemaNodeIdentifier");
-        }
-
-        StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(
-                usesParentCtx, refineTargetNodeIdentifier);
-
-        if (refineTargetNodeCtx == null) {
-            throw new IllegalArgumentException(
-                    "Refine target node not found. Path: "
-                            + refineTargetNodeIdentifier);
-        }
+        SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
+        StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
+        Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found.",
+                refineTargetNodeIdentifier);
 
         addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
         refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
-
     }
 
-    private static void addOrReplaceNodes(
-            StatementContextBase<?, ?, ?> refineCtx,
-            StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
+    private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
+            final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
-        Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = refineCtx
-                .declaredSubstatements();
+        Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = refineCtx.declaredSubstatements();
         for (StatementContextBase<?, ?, ?> refineSubstatementCtx : declaredSubstatements) {
             if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
                 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
@@ -228,9 +192,8 @@ public final class GroupingUtils {
         }
     }
 
-    private static void addOrReplaceNode(
-            StatementContextBase<?, ?, ?> refineSubstatementCtx,
-            StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
+    private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
+            final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
         StatementDefinition refineSubstatementDef = refineSubstatementCtx
                 .getPublicDefinition();
@@ -241,12 +204,9 @@ public final class GroupingUtils {
             throw new SourceException("Error in module '"
                     + refineSubstatementCtx.getRoot().getStatementArgument()
                     + "' in the refine of uses '"
-                    + refineSubstatementCtx.getParentContext()
-                            .getStatementArgument()
-                    + "': can not perform refine of '"
-                    + refineSubstatementCtx.getPublicDefinition()
-                    + "' for the target '"
-                    + refineTargetNodeCtx.getPublicDefinition() + "'.",
+                    + refineSubstatementCtx.getParentContext().getStatementArgument()
+                    + "': can not perform refine of '" + refineSubstatementCtx.getPublicDefinition()
+                    + "' for the target '" + refineTargetNodeCtx.getPublicDefinition() + "'.",
                     refineSubstatementCtx.getStatementSourceReference());
         }
 
@@ -258,39 +218,30 @@ public final class GroupingUtils {
         }
     }
 
-    private static boolean isAllowedToAddByRefine(
-            StatementDefinition publicDefinition) {
+    private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
         Set<StatementDefinition> allowedToAddByRefineDefSet = new HashSet<>();
         allowedToAddByRefineDefSet.add(Rfc6020Mapping.MUST);
 
         return allowedToAddByRefineDefSet.contains(publicDefinition);
     }
 
-    private static boolean isSupportedRefineSubstatement(
-            StatementContextBase<?, ?, ?> refineSubstatementCtx) {
+    private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
 
-        Collection<?> supportedRefineSubstatements = refineSubstatementCtx
-                .getFromNamespace(ValidationBundlesNamespace.class,
-                        ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
+        Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
+            ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
 
-        return supportedRefineSubstatements == null
-                || supportedRefineSubstatements.isEmpty()
-                || supportedRefineSubstatements.contains(refineSubstatementCtx
-                        .getPublicDefinition())
+        return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
+                || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
                 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
     }
 
-    private static boolean isSupportedRefineTarget(
-            StatementContextBase<?, ?, ?> refineSubstatementCtx,
-            StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
+    private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
+            final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
-        Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS
-                .get(refineSubstatementCtx.getPublicDefinition());
+        Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
+            refineSubstatementCtx.getPublicDefinition());
 
-        return supportedRefineTargets == null
-                || supportedRefineTargets.isEmpty()
-                || supportedRefineTargets.contains(refineTargetNodeCtx
-                        .getPublicDefinition());
+        return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
+                || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
     }
-
 }