Refactor argument adaptation
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / KeyStatementImpl.java
index 94d1049890bb3d99bd98c48c8966b9fbf466c44d..6f15bc311ce8201675af7f47f6df0b147ef04700 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,24 +7,29 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
 import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Set;
-import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl;
 
 public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<SchemaNodeIdentifier>> implements
         KeyStatement {
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
+            .KEY)
+            .build();
 
     protected KeyStatementImpl(final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> context) {
         super(context);
@@ -32,30 +37,51 @@ public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<Schem
 
     public static class Definition
             extends
-            AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement, EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
+            AbstractStatementSupport<Collection<SchemaNodeIdentifier>, KeyStatement,
+                    EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> {
 
         public Definition() {
-            super(Rfc6020Mapping.KEY);
+            super(YangStmtMapping.KEY);
         }
 
         @Override
-        public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value)
-                throws SourceException {
-
-            final List<String> keyTokens = StmtContextUtils.LIST_KEY_SPLITTER.splitToList(value);
-
-            // to detect if key contains duplicates
-            if ((new HashSet<>(keyTokens)).size() < keyTokens.size()) {
-                throw new IllegalArgumentException();
+        public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+            final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
+            int tokens = 0;
+            for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
+                builder.add(SchemaNodeIdentifier.create(false, StmtContextUtils.qnameFromArgument(ctx, keyToken)));
+                tokens++;
             }
 
-            // FIXME: would an ImmutableSetBuilder be better?
-            Set<SchemaNodeIdentifier> keyNodes = new LinkedHashSet<>();
-            for (String keyToken : keyTokens) {
-                keyNodes.add(SchemaNodeIdentifier.create(false, Utils.qNameFromArgument(ctx, keyToken)));
+            // Throws NPE on nulls, retains first inserted value, cannot be modified
+            final Collection<SchemaNodeIdentifier> ret = builder.build();
+            SourceException.throwIf(ret.size() != tokens, ctx.getStatementSourceReference(),
+                    "Key argument '%s' contains duplicates", value);
+            return ret;
+        }
+
+        @Override
+        public Collection<SchemaNodeIdentifier> adaptArgumentValue(
+                final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
+                    EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx,
+                final QNameModule targetModule) {
+            final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
+            boolean replaced = false;
+            for (final SchemaNodeIdentifier arg : ctx.getStatementArgument()) {
+                final QName qname = arg.getLastComponent();
+                if (!targetModule.equals(qname)) {
+                    final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class,
+                            QName.create(targetModule, qname.getLocalName()));
+                    builder.add(SchemaNodeIdentifier.create(false, newQname));
+                    replaced = true;
+                } else {
+                    builder.add(arg);
+                }
             }
 
-            return keyNodes;
+            // This makes sure we reuse the collection when a grouping is
+            // instantiated in the same module
+            return replaced ? builder.build() : ctx.getStatementArgument();
         }
 
         @Override
@@ -65,9 +91,14 @@ public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<Schem
 
         @Override
         public EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement> createEffective(
-                final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
+                final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement,
+                        EffectiveStatement<Collection<SchemaNodeIdentifier>, KeyStatement>> ctx) {
             return new KeyEffectiveStatementImpl(ctx);
         }
-    }
 
+        @Override
+        protected SubstatementValidator getSubstatementValidator() {
+            return SUBSTATEMENT_VALIDATOR;
+        }
+    }
 }