Move implementation of OnStatementAdded to single class
[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 static com.google.common.base.Verify.verify;
11
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;
18 import java.util.Set;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
30 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByStatement.Ordering;
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.InferenceException;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 abstract class AbstractListStatementSupport extends
47         BaseSchemaTreeStatementSupport<ListStatement, ListEffectiveStatement> {
48
49     private static final Logger LOG = LoggerFactory.getLogger(AbstractListStatementSupport.class);
50     private static final ImmutableSet<YangStmtMapping> UNINSTANTIATED_DATATREE_STATEMENTS = ImmutableSet.of(
51         YangStmtMapping.GROUPING, YangStmtMapping.NOTIFICATION, YangStmtMapping.INPUT, YangStmtMapping.OUTPUT);
52
53     AbstractListStatementSupport() {
54         super(YangStmtMapping.LIST);
55     }
56
57     @Override
58     public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
59         return StmtContextUtils.parseIdentifier(ctx, value);
60     }
61
62     @Override
63     protected final ListStatement createDeclared(final StmtContext<QName, ListStatement, ?> ctx,
64             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
65         return new RegularListStatement(ctx.coerceStatementArgument(), substatements);
66     }
67
68     @Override
69     protected final ListStatement createEmptyDeclared(final StmtContext<QName, ListStatement, ?> ctx) {
70         return new EmptyListStatement(ctx.coerceStatementArgument());
71     }
72
73     @Override
74     protected final ListEffectiveStatement createEffective(
75             final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx,
76             final ListStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
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 QName keyQName : keyStmt.argument()) {
92                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
93                     throw new InferenceException(ctx.getStatementSourceReference(),
94                         "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
95                         keyQName.getLocalName(), ctx.getStatementArgument());
96                 }
97                 keyDefinitionInit.add(keyQName);
98             }
99
100             keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
101         } else {
102             keyDefinition = ImmutableList.of();
103         }
104
105         final boolean configuration = ctx.isConfiguration();
106         final int flags = new FlagsBuilder()
107                 .setHistory(ctx.getCopyHistory())
108                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
109                 .setConfiguration(configuration)
110                 .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, Ordering.SYSTEM)
111                     .equals(Ordering.USER))
112                 .toFlags();
113         if (configuration && keyDefinition.isEmpty() && isInstantied(ctx)) {
114             warnConfigList(ctx);
115         }
116
117         final Optional<ElementCountConstraint> elementCountConstraint =
118                 EffectiveStmtUtils.createElementCountConstraint(substatements);
119         return original == null && !elementCountConstraint.isPresent()
120                 ? new EmptyListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition)
121                         : new RegularListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition,
122                             elementCountConstraint.orElse(null), original);
123     }
124
125     private static void warnConfigList(final @NonNull StmtContext<QName, ListStatement, ListEffectiveStatement> ctx) {
126         final StmtContext<QName, ListStatement, ListEffectiveStatement> warnCtx = ctx.getOriginalCtx().orElse(ctx);
127         final Boolean warned = warnCtx.getFromNamespace(ConfigListWarningNamespace.class, Boolean.TRUE);
128         // Hacky check if we have issued a warning for the original statement
129         if (warned == null) {
130             verify(warnCtx instanceof Mutable, "Unexpected context %s", warnCtx);
131             ((Mutable<?, ?, ?>) warnCtx).addToNs(ConfigListWarningNamespace.class, Boolean.TRUE, Boolean.TRUE);
132             LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
133                     + "this is fine with OpenDaylight, it can cause interoperability issues with other systems "
134                     + "[defined at {}]", ctx.getStatementArgument(), warnCtx.getStatementSourceReference());
135         }
136     }
137
138     private static boolean isInstantied(final StmtContext<?, ?, ?> ctx) {
139         StmtContext<?, ?, ?> parent = ctx.getParentContext();
140         while (parent != null) {
141             final StatementDefinition parentDef = parent.getPublicDefinition();
142             if (UNINSTANTIATED_DATATREE_STATEMENTS.contains(parentDef)) {
143                 return false;
144             }
145
146             final StmtContext<?, ?, ?> grandParent = parent.getParentContext();
147             if (YangStmtMapping.AUGMENT == parentDef && grandParent != null) {
148                 // If this is an augment statement and its parent is either a 'module' or 'submodule' statement, we are
149                 // dealing with an uninstantiated context.
150                 final StatementDefinition grandParentDef = grandParent.getPublicDefinition();
151                 if (YangStmtMapping.MODULE == grandParentDef || YangStmtMapping.SUBMODULE == grandParentDef) {
152                     return false;
153                 }
154             }
155
156             parent = grandParent;
157         }
158         return true;
159     }
160
161     @Override
162     protected final ListEffectiveStatement createEmptyEffective(
163             final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx, final ListStatement declared) {
164         return createEffective(ctx, declared, ImmutableList.of());
165     }
166 }