2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.yang.parser.rfc7950.stmt.unique;
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.HashSet;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueStatement;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 public final class UniqueStatementSupport
27 extends AbstractStatementSupport<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement,
28 EffectiveStatement<Collection<SchemaNodeIdentifier.Relative>, UniqueStatement>> {
29 private static final Splitter SPACE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
30 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
31 YangStmtMapping.UNIQUE)
33 private static final UniqueStatementSupport INSTANCE = new UniqueStatementSupport();
35 private UniqueStatementSupport() {
36 super(YangStmtMapping.UNIQUE);
39 public static UniqueStatementSupport getInstance() {
44 public Collection<SchemaNodeIdentifier.Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx,
46 final Collection<Relative> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
47 SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
48 "Invalid argument value '%s' of unique statement. The value must contains at least "
49 + "one descendant schema node identifier.", value);
50 return uniqueConstraints;
54 public UniqueStatement createDeclared(final StmtContext<Collection<Relative>, UniqueStatement, ?> ctx) {
55 return new UniqueStatementImpl(ctx);
59 public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective(
60 final StmtContext<Collection<Relative>, UniqueStatement,
61 EffectiveStatement<Collection<Relative>, UniqueStatement>> ctx) {
62 return new UniqueEffectiveStatementImpl(ctx);
66 protected SubstatementValidator getSubstatementValidator() {
67 return SUBSTATEMENT_VALIDATOR;
70 private static Collection<SchemaNodeIdentifier.Relative> parseUniqueConstraintArgument(
71 final StmtContext<?, ?, ?> ctx, final String argumentValue) {
72 final Set<SchemaNodeIdentifier.Relative> uniqueConstraintNodes = new HashSet<>();
73 for (final String uniqueArgToken : SPACE_SPLITTER.split(argumentValue)) {
74 final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
75 SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
76 "Unique statement argument '%s' contains schema node identifier '%s' "
77 + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
78 uniqueConstraintNodes.add((SchemaNodeIdentifier.Relative) nodeIdentifier);
80 return ImmutableSet.copyOf(uniqueConstraintNodes);