Do not expose StmtContext to StatementFactory
[yangtools.git] / parser / rfc6241-parser-support / src / main / java / org / opendaylight / yangtools / rfc6241 / parser / GetFilterElementAttributesStatementSupport.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, 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.rfc6241.parser;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableList;
12 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesEffectiveStatement;
13 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesStatement;
14 import org.opendaylight.yangtools.rfc6241.model.api.NetconfStatements;
15 import org.opendaylight.yangtools.yang.common.Empty;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractEmptyStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Beta
31 public final class GetFilterElementAttributesStatementSupport extends AbstractEmptyStatementSupport<
32         GetFilterElementAttributesStatement, GetFilterElementAttributesEffectiveStatement> {
33     private static final Logger LOG = LoggerFactory.getLogger(GetFilterElementAttributesStatementSupport.class);
34     private static final SubstatementValidator VALIDATOR =
35         SubstatementValidator.builder(NetconfStatements.GET_FILTER_ELEMENT_ATTRIBUTES).build();
36
37     public GetFilterElementAttributesStatementSupport(final YangParserConfiguration config) {
38         super(NetconfStatements.GET_FILTER_ELEMENT_ATTRIBUTES, StatementPolicy.reject(), config, VALIDATOR);
39     }
40
41     @Override
42     public void onFullDefinitionDeclared(final Mutable<Empty, GetFilterElementAttributesStatement,
43             GetFilterElementAttributesEffectiveStatement> stmt) {
44         super.onFullDefinitionDeclared(stmt);
45         if (!computeSupported(stmt)) {
46             stmt.setUnsupported();
47         }
48     }
49
50     @Override
51     protected GetFilterElementAttributesStatement createDeclared(final BoundStmtCtx<Empty> ctx,
52             final ImmutableList<DeclaredStatement<?>> substatements) {
53         return substatements.isEmpty() ? GetFilterElementAttributesStatementImpl.EMPTY
54             : new GetFilterElementAttributesStatementImpl(substatements);
55     }
56
57     @Override
58     protected GetFilterElementAttributesStatement attachDeclarationReference(
59             final GetFilterElementAttributesStatement stmt, final DeclarationReference reference) {
60         return new RefGetFilterElementAttributesStatement(stmt, reference);
61     }
62
63     @Override
64     protected GetFilterElementAttributesEffectiveStatement createEffective(
65             final Current<Empty, GetFilterElementAttributesStatement> stmt,
66             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
67         return new GetFilterElementAttributesEffectiveStatementImpl(stmt, substatements);
68     }
69
70     private static boolean computeSupported(final StmtContext<?, ?, ?> stmt) {
71         final StmtContext<?, ?, ?> parent = stmt.getParentContext();
72         if (parent == null) {
73             LOG.debug("No parent, ignoring get-filter-element-attributes statement");
74             return false;
75         }
76         if (parent.publicDefinition() != YangStmtMapping.ANYXML) {
77             LOG.debug("Parent is not an anyxml node, ignoring get-filter-element-attributes statement");
78             return false;
79         }
80         if (!"filter".equals(parent.rawArgument())) {
81             LOG.debug("Parent is not named 'filter', ignoring get-filter-element-attributes statement");
82             return false;
83         }
84
85         final StmtContext<?, ?, ?> grandParent = parent.getParentContext();
86         if (grandParent == null) {
87             LOG.debug("No grandparent, ignoring get-filter-element-attributes statement");
88             return false;
89         }
90         if (grandParent.publicDefinition() != YangStmtMapping.INPUT) {
91             LOG.debug("Grandparent is not an input node, ignoring get-filter-element-attributes statement");
92             return false;
93         }
94
95         final StmtContext<?, ?, ?> greatGrandParent = grandParent.getParentContext();
96         if (greatGrandParent == null) {
97             LOG.debug("No grandparent, ignoring get-filter-element-attributes statement");
98             return false;
99         }
100         if (greatGrandParent.publicDefinition() != YangStmtMapping.RPC) {
101             LOG.debug("Grandparent is not an RPC node, ignoring get-filter-element-attributes statement");
102             return false;
103         }
104
105         switch (greatGrandParent.getRawArgument()) {
106             case "get":
107             case "get-config":
108                 return true;
109             default:
110                 LOG.debug("Great-grandparent is not named 'get' nor 'get-config, ignoring get-filter-element-attributes"
111                     + " statement");
112                 return false;
113         }
114     }
115 }