ce5cc917fd9d6aab0d8637f66fa2d06ab8b3c399
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / unique / UniqueStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.unique;
9
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.Set;
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;
25
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)
32         .build();
33
34     public UniqueStatementSupport() {
35         super(YangStmtMapping.UNIQUE);
36     }
37
38     @Override
39     public Collection<SchemaNodeIdentifier.Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx,
40             final String value) {
41         final Collection<Relative> uniqueConstraints = parseUniqueConstraintArgument(ctx, value);
42         SourceException.throwIf(uniqueConstraints.isEmpty(), ctx.getStatementSourceReference(),
43                 "Invalid argument value '%s' of unique statement. The value must contains at least "
44                         + "one descendant schema node identifier.", value);
45         return uniqueConstraints;
46     }
47
48     @Override
49     public UniqueStatement createDeclared(final StmtContext<Collection<Relative>, UniqueStatement, ?> ctx) {
50         return new UniqueStatementImpl(ctx);
51     }
52
53     @Override
54     public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective(
55             final StmtContext<Collection<Relative>, UniqueStatement,
56             EffectiveStatement<Collection<Relative>, UniqueStatement>> ctx) {
57         return new UniqueEffectiveStatementImpl(ctx);
58     }
59
60     @Override
61     protected SubstatementValidator getSubstatementValidator() {
62         return SUBSTATEMENT_VALIDATOR;
63     }
64
65     private static Collection<SchemaNodeIdentifier.Relative> parseUniqueConstraintArgument(
66             final StmtContext<?, ?, ?> ctx, final String argumentValue) {
67         final Set<SchemaNodeIdentifier.Relative> uniqueConstraintNodes = new HashSet<>();
68         for (final String uniqueArgToken : SPACE_SPLITTER.split(argumentValue)) {
69             final SchemaNodeIdentifier nodeIdentifier = ArgumentUtils.nodeIdentifierFromPath(ctx, uniqueArgToken);
70             SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
71                     "Unique statement argument '%s' contains schema node identifier '%s' "
72                             + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
73             uniqueConstraintNodes.add((SchemaNodeIdentifier.Relative) nodeIdentifier);
74         }
75         return ImmutableSet.copyOf(uniqueConstraintNodes);
76     }
77 }