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