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 static com.google.common.base.Verify.verify;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.Ordering;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
31 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 abstract class AbstractListStatementSupport extends
49 BaseSchemaTreeStatementSupport<ListStatement, ListEffectiveStatement> {
51 private static final Logger LOG = LoggerFactory.getLogger(AbstractListStatementSupport.class);
52 private static final ImmutableSet<YangStmtMapping> UNINSTANTIATED_DATATREE_STATEMENTS = ImmutableSet.of(
53 YangStmtMapping.GROUPING, YangStmtMapping.NOTIFICATION, YangStmtMapping.INPUT, YangStmtMapping.OUTPUT);
55 AbstractListStatementSupport() {
56 super(YangStmtMapping.LIST);
60 protected final ListStatement createDeclared(final StmtContext<QName, ListStatement, ?> ctx,
61 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
62 return new RegularListStatement(ctx.getArgument(), substatements);
66 protected final ListStatement createEmptyDeclared(final StmtContext<QName, ListStatement, ?> ctx) {
67 return new EmptyListStatement(ctx.getArgument());
71 protected ListEffectiveStatement createEffective(final Current<QName, ListStatement> stmt,
72 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
73 final SchemaPath path = stmt.getSchemaPath();
74 final ListSchemaNode original = (ListSchemaNode) stmt.original();
76 final ImmutableList<QName> keyDefinition;
77 final KeyEffectiveStatement keyStmt = findFirstStatement(substatements, KeyEffectiveStatement.class);
78 if (keyStmt != null) {
79 final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
80 final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
81 for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
82 if (effectiveStatement instanceof LeafSchemaNode) {
83 possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
86 for (final QName keyQName : keyStmt.argument()) {
87 if (!possibleLeafQNamesForKey.contains(keyQName)) {
88 throw new InferenceException(stmt.sourceReference(),
89 "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
90 keyQName.getLocalName(), stmt.argument());
92 keyDefinitionInit.add(keyQName);
95 keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
97 keyDefinition = ImmutableList.of();
100 final boolean configuration = stmt.effectiveConfig();
101 final int flags = new FlagsBuilder()
102 .setHistory(stmt.history())
103 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
104 .setConfiguration(configuration)
105 .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, Ordering.SYSTEM)
106 .equals(Ordering.USER))
108 if (configuration && keyDefinition.isEmpty() && isInstantied(stmt)) {
109 warnConfigList(stmt);
112 final Optional<ElementCountConstraint> elementCountConstraint =
113 EffectiveStmtUtils.createElementCountConstraint(substatements);
114 return original == null && !elementCountConstraint.isPresent()
115 ? new EmptyListEffectiveStatement(stmt, path, flags, substatements, keyDefinition)
116 : new RegularListEffectiveStatement(stmt, path, flags, substatements, keyDefinition,
117 elementCountConstraint.orElse(null), original);
120 private static void warnConfigList(final @NonNull Current<QName, ListStatement> stmt) {
121 final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx = stmt.caerbannog();
122 final StmtContext<QName, ListStatement, ListEffectiveStatement> warnCtx = ctx.getOriginalCtx().orElse(ctx);
123 final Boolean warned = warnCtx.getFromNamespace(ConfigListWarningNamespace.class, Boolean.TRUE);
124 // Hacky check if we have issued a warning for the original statement
125 if (warned == null) {
126 verify(warnCtx instanceof Mutable, "Unexpected context %s", warnCtx);
127 ((Mutable<?, ?, ?>) warnCtx).addToNs(ConfigListWarningNamespace.class, Boolean.TRUE, Boolean.TRUE);
128 LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
129 + "this is fine with OpenDaylight, it can cause interoperability issues with other systems "
130 + "[defined at {}]", ctx.argument(), warnCtx.sourceReference());
134 private static boolean isInstantied(final EffectiveStmtCtx ctx) {
135 Parent parent = ctx.effectiveParent();
136 while (parent != null) {
137 final StatementDefinition parentDef = parent.publicDefinition();
138 if (UNINSTANTIATED_DATATREE_STATEMENTS.contains(parentDef)) {
142 final Parent grandParent = parent.effectiveParent();
143 if (YangStmtMapping.AUGMENT == parentDef && grandParent != null) {
144 // If this is an augment statement and its parent is either a 'module' or 'submodule' statement, we are
145 // dealing with an uninstantiated context.
146 final StatementDefinition grandParentDef = grandParent.publicDefinition();
147 if (YangStmtMapping.MODULE == grandParentDef || YangStmtMapping.SUBMODULE == grandParentDef) {
152 parent = grandParent;