Migrate QName-based declared statements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / list / AbstractListStatementSupport.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.list;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.Status;
22 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByStatement.Ordering;
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;
43
44 abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<ListStatement, ListEffectiveStatement> {
45     private static final Logger LOG = LoggerFactory.getLogger(AbstractListStatementSupport.class);
46
47     AbstractListStatementSupport() {
48         super(YangStmtMapping.LIST);
49     }
50
51     @Override
52     public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
53         return StmtContextUtils.parseIdentifier(ctx, value);
54     }
55
56     @Override
57     public final void onStatementAdded(final Mutable<QName, ListStatement, ListEffectiveStatement> stmt) {
58         stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
59     }
60
61     @Override
62     protected ListStatement createDeclared(final StmtContext<QName, ListStatement, ?> ctx,
63             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
64         return new RegularListStatement(ctx.coerceStatementArgument(), substatements);
65     }
66
67     @Override
68     protected ListStatement createEmptyDeclared(final StmtContext<QName, ListStatement, ?> ctx) {
69         return new EmptyListStatement(ctx.coerceStatementArgument());
70     }
71
72     @Override
73     protected final ListEffectiveStatement createEffective(
74             final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx,
75             final ListStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
76         final StatementSourceReference ref = ctx.getStatementSourceReference();
77         final SchemaPath path = ctx.getSchemaPath().get();
78         final ListSchemaNode original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
79                 .orElse(null);
80
81         final ImmutableList<QName> keyDefinition;
82         final KeyEffectiveStatement keyStmt = findFirstStatement(substatements, KeyEffectiveStatement.class);
83         if (keyStmt != null) {
84             final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
85             final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
86             for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
87                 if (effectiveStatement instanceof LeafSchemaNode) {
88                     possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
89                 }
90             }
91             for (final SchemaNodeIdentifier key : keyStmt.argument()) {
92                 final QName keyQName = key.getLastComponent();
93
94                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
95                     throw new InferenceException(ref, "Key '%s' misses node '%s' in list '%s'",
96                         keyStmt.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
97                 }
98                 keyDefinitionInit.add(keyQName);
99             }
100
101             keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
102         } else {
103             keyDefinition = ImmutableList.of();
104         }
105
106         final boolean configuration = ctx.isConfiguration();
107         final int flags = new FlagsBuilder()
108                 .setHistory(ctx.getCopyHistory())
109                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
110                 .setConfiguration(configuration)
111                 .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, Ordering.SYSTEM)
112                     .equals(Ordering.USER))
113                 .toFlags();
114         if (configuration && keyDefinition.isEmpty() && !inGrouping(ctx)) {
115             LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
116                     + " this is fine with OpenDaylight, it can cause interoperability issues with other systems "
117                     + "[at {}]", ctx.getStatementArgument(), ref);
118         }
119
120         final Optional<ElementCountConstraint> elementCountConstraint =
121                 EffectiveStmtUtils.createElementCountConstraint(substatements);
122         return original == null && !elementCountConstraint.isPresent()
123                 ? new EmptyListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition)
124                         : new RegularListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition,
125                             elementCountConstraint.orElse(null), original);
126     }
127
128     private static boolean inGrouping(final StmtContext<?, ?, ?> ctx) {
129         StmtContext<?, ?, ?> parent = ctx.getParentContext();
130         while (parent != null) {
131             if (parent.getPublicDefinition() == YangStmtMapping.GROUPING) {
132                 return true;
133             }
134             parent = parent.getParentContext();
135         }
136         return false;
137     }
138
139     @Override
140     protected final ListEffectiveStatement createEmptyEffective(
141             final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx, final ListStatement declared) {
142         return createEffective(ctx, declared, ImmutableList.of());
143     }
144 }