Populate ietf-restconf operations container
[yangtools.git] / parser / rfc8040-parser-support / src / main / java / org / opendaylight / yangtools / rfc8040 / parser / YangDataStatementSupport.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.rfc8040.parser;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import org.opendaylight.yangtools.rfc8040.model.api.YangDataEffectiveStatement;
15 import org.opendaylight.yangtools.rfc8040.model.api.YangDataStatement;
16 import org.opendaylight.yangtools.rfc8040.model.api.YangDataStatements;
17 import org.opendaylight.yangtools.yang.common.Empty;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
25 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35
36 @Beta
37 public final class YangDataStatementSupport
38         extends AbstractStringStatementSupport<YangDataStatement, YangDataEffectiveStatement> {
39     private static final SubstatementValidator VALIDATOR = SubstatementValidator.builder(YangDataStatements.YANG_DATA)
40         .addMandatory(YangStmtMapping.CONTAINER)
41         .addOptional(YangStmtMapping.USES)
42         .build();
43
44     public YangDataStatementSupport(final YangParserConfiguration config) {
45         super(YangDataStatements.YANG_DATA, StatementPolicy.reject(), config, VALIDATOR);
46     }
47
48     @Override
49     public void onStatementAdded(final Mutable<String, YangDataStatement, YangDataEffectiveStatement> ctx) {
50         // as per https://tools.ietf.org/html/rfc8040#section-8,
51         // yang-data is ignored unless it appears as a top-level statement
52         if (ctx.coerceParentContext().getParentContext() != null) {
53             ctx.setUnsupported();
54         }
55     }
56
57     @Override
58     public void onFullDefinitionDeclared(final Mutable<String, YangDataStatement, YangDataEffectiveStatement> ctx) {
59         // Parse and populate our argument to be picked up when we build the effective statement
60         final String argument = SourceException.throwIfNull(ctx.argument(), ctx, "yang-data requires an argument");
61         final QName qname = StmtContextUtils.parseIdentifier(ctx, argument);
62         ctx.addToNs(YangDataArgumentNamespace.class, Empty.value(), qname);
63
64         // Support for 'operations' container semantics. For this we need to recognize when the model at hand matches
65         // RFC8040 ietf-restconf module. In ordered to do that we hook onto this particular definition:
66         //
67         //   rc:yang-data yang-api {
68         //     uses restconf;
69         //   }
70         //
71         // If we find it, we hook an inference action which performs the next step when the module is fully declared.
72         if (ctx.isSupportedToBuildEffective() && "yang-api".equals(ctx.argument())) {
73             final var stmts = ctx.declaredSubstatements();
74             if (stmts.size() == 1) {
75                 final var stmt = stmts.iterator().next();
76                 if (stmt.producesEffective(UsesEffectiveStatement.class) && "restconf".equals(stmt.rawArgument())) {
77                     // The rc:yang-data shape matches, but we are not sure about the module identity, that needs to be
78                     // done later multiple stages, the first one being initiated through this call.
79                     OperationsValidateModuleAction.applyTo(ctx.coerceParentContext());
80                 }
81             }
82         }
83     }
84
85     @Override
86     public boolean isIgnoringIfFeatures() {
87         return true;
88     }
89
90     @Override
91     public boolean isIgnoringConfig() {
92         return true;
93     }
94
95     @Override
96     protected YangDataStatement createDeclared(final BoundStmtCtx<String> ctx,
97             final ImmutableList<DeclaredStatement<?>> substatements) {
98         return new YangDataStatementImpl(ctx.getRawArgument(), substatements);
99     }
100
101     @Override
102     protected YangDataStatement attachDeclarationReference(final YangDataStatement stmt,
103             final DeclarationReference reference) {
104         return new RefYangDataStatement(stmt, reference);
105     }
106
107     @Override
108     protected YangDataEffectiveStatement createEffective(final Current<String, YangDataStatement> stmt,
109             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
110         // So now we need to deal with effective validation. The requirement is that:
111         //        It MUST contain data definition statements
112         //        that result in exactly one container data node definition.
113         final long dataDefs = substatements.stream().filter(DataTreeEffectiveStatement.class::isInstance).count();
114         if (dataDefs == 0) {
115             throw new MissingSubstatementException("yang-data requires exactly one container", stmt.sourceReference());
116         }
117         if (dataDefs > 1) {
118             throw new InvalidSubstatementException(stmt,
119                 "yang-data requires exactly one data definition node, found %s", dataDefs);
120         }
121
122         return new YangDataEffectiveStatementImpl(stmt, substatements,
123             verifyNotNull(stmt.namespaceItem(YangDataArgumentNamespace.class, Empty.value())));
124     }
125 }