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.list;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Optional;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
40 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<ListStatement, ListEffectiveStatement> {
45 private static final Logger LOG = LoggerFactory.getLogger(AbstractListStatementSupport.class);
46 private static final ImmutableSet<YangStmtMapping> UNINSTANTIATED_DATATREE_STATEMENTS = ImmutableSet.of(
47 YangStmtMapping.GROUPING, YangStmtMapping.NOTIFICATION, YangStmtMapping.INPUT, YangStmtMapping.OUTPUT);
49 AbstractListStatementSupport() {
50 super(YangStmtMapping.LIST);
54 public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
55 return StmtContextUtils.parseIdentifier(ctx, value);
59 public final void onStatementAdded(final Mutable<QName, ListStatement, ListEffectiveStatement> stmt) {
60 stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
64 protected final ListStatement createDeclared(final StmtContext<QName, ListStatement, ?> ctx,
65 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
66 return new RegularListStatement(ctx.coerceStatementArgument(), substatements);
70 protected final ListStatement createEmptyDeclared(final StmtContext<QName, ListStatement, ?> ctx) {
71 return new EmptyListStatement(ctx.coerceStatementArgument());
75 protected final ListEffectiveStatement createEffective(
76 final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx,
77 final ListStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
78 final StatementSourceReference ref = ctx.getStatementSourceReference();
79 final SchemaPath path = ctx.getSchemaPath().get();
80 final ListSchemaNode original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
83 final ImmutableList<QName> keyDefinition;
84 final KeyEffectiveStatement keyStmt = findFirstStatement(substatements, KeyEffectiveStatement.class);
85 if (keyStmt != null) {
86 final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
87 final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
88 for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
89 if (effectiveStatement instanceof LeafSchemaNode) {
90 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
93 for (final SchemaNodeIdentifier key : keyStmt.argument()) {
94 final QName keyQName = key.getLastComponent();
96 if (!possibleLeafQNamesForKey.contains(keyQName)) {
97 throw new InferenceException(ref, "Key '%s' misses node '%s' in list '%s'",
98 keyStmt.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
100 keyDefinitionInit.add(keyQName);
103 keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
105 keyDefinition = ImmutableList.of();
108 final boolean configuration = ctx.isConfiguration();
109 final int flags = new FlagsBuilder()
110 .setHistory(ctx.getCopyHistory())
111 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
112 .setConfiguration(configuration)
113 .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, "system")
116 if (configuration && keyDefinition.isEmpty() && isInstantied(ctx)) {
117 LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
118 + " this is fine with OpenDaylight, it can cause interoperability issues with other systems "
119 + "[at {}]", ctx.getStatementArgument(), ref);
122 final Optional<ElementCountConstraint> elementCountConstraint =
123 EffectiveStmtUtils.createElementCountConstraint(substatements);
124 return original == null && !elementCountConstraint.isPresent()
125 ? new EmptyListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition)
126 : new RegularListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition,
127 elementCountConstraint.orElse(null), original);
130 private static boolean isInstantied(final StmtContext<?, ?, ?> ctx) {
131 for (StmtContext<?, ?, ?> parent = ctx.getParentContext(); parent != null; parent = parent.getParentContext()) {
132 if (UNINSTANTIATED_DATATREE_STATEMENTS.contains(parent.getPublicDefinition())) {
140 protected final ListEffectiveStatement createEmptyEffective(
141 final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx, final ListStatement declared) {
142 return createEffective(ctx, declared, ImmutableList.of());