1ecada93c60bf5123703e36684e20c525fccd811
[yangtools.git] / yang / yang-parser-rfc7950 / 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     private static final UniqueStatementSupport INSTANCE = new UniqueStatementSupport();
34
35     private UniqueStatementSupport() {
36         super(YangStmtMapping.UNIQUE);
37     }
38
39     public static UniqueStatementSupport getInstance() {
40         return INSTANCE;
41     }
42
43     @Override
44     public Collection<SchemaNodeIdentifier.Relative> parseArgumentValue(final StmtContext<?, ?, ?> ctx,
45             final String value) {
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;
51     }
52
53     @Override
54     public UniqueStatement createDeclared(final StmtContext<Collection<Relative>, UniqueStatement, ?> ctx) {
55         return new UniqueStatementImpl(ctx);
56     }
57
58     @Override
59     public EffectiveStatement<Collection<Relative>, UniqueStatement> createEffective(
60             final StmtContext<Collection<Relative>, UniqueStatement,
61             EffectiveStatement<Collection<Relative>, UniqueStatement>> ctx) {
62         return new UniqueEffectiveStatementImpl(ctx);
63     }
64
65     @Override
66     protected SubstatementValidator getSubstatementValidator() {
67         return SUBSTATEMENT_VALIDATOR;
68     }
69
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);
79         }
80         return ImmutableSet.copyOf(uniqueConstraintNodes);
81     }
82 }