05bdeab304933eb34b87d9978d66937a789ff551
[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.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesEffectiveStatement;
14 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesSchemaNode;
15 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesStatement;
16 import org.opendaylight.yangtools.rfc6241.model.api.NetconfStatements;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredStatement.WithoutArgument.WithSubstatements;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseVoidStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.UnknownEffectiveStatementBase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Beta
33 public final class GetFilterElementAttributesStatementSupport extends BaseVoidStatementSupport<
34         GetFilterElementAttributesStatement, GetFilterElementAttributesEffectiveStatement> {
35
36     private static final class Declared extends WithSubstatements implements GetFilterElementAttributesStatement {
37         static final @NonNull Declared EMPTY = new Declared(ImmutableList.of());
38
39         Declared(final ImmutableList<? extends DeclaredStatement<?>> substatements) {
40             super(substatements);
41         }
42     }
43
44     private static final class Effective
45             extends UnknownEffectiveStatementBase<Void, GetFilterElementAttributesStatement>
46             implements GetFilterElementAttributesEffectiveStatement, GetFilterElementAttributesSchemaNode {
47         private final @NonNull SchemaPath path;
48
49         Effective(final GetFilterElementAttributesStatement declared,
50                 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
51                 final StmtContext<Void, GetFilterElementAttributesStatement, ?> ctx) {
52             super(declared.argument(), declared, substatements, ctx);
53             path = ctx.coerceParentContext().getSchemaPath().get().createChild(
54                 ctx.getPublicDefinition().getStatementName());
55         }
56
57         @Override
58         public QName getQName() {
59             return path.getLastComponent();
60         }
61
62         @Override
63         @Deprecated
64         public SchemaPath getPath() {
65             return path;
66         }
67
68         @Override
69         public GetFilterElementAttributesEffectiveStatement asEffectiveStatement() {
70             return this;
71         }
72     }
73
74     private static final Logger LOG = LoggerFactory.getLogger(GetFilterElementAttributesStatementSupport.class);
75     private static final GetFilterElementAttributesStatementSupport INSTANCE =
76             new GetFilterElementAttributesStatementSupport(NetconfStatements.GET_FILTER_ELEMENT_ATTRIBUTES);
77
78     private final SubstatementValidator validator;
79
80     GetFilterElementAttributesStatementSupport(final StatementDefinition definition) {
81         super(definition);
82         this.validator = SubstatementValidator.builder(definition).build();
83     }
84
85     public static GetFilterElementAttributesStatementSupport getInstance() {
86         return INSTANCE;
87     }
88
89     @Override
90     public void onFullDefinitionDeclared(final Mutable<Void, GetFilterElementAttributesStatement,
91             GetFilterElementAttributesEffectiveStatement> stmt) {
92         super.onFullDefinitionDeclared(stmt);
93         stmt.setIsSupportedToBuildEffective(computeSupported(stmt));
94     }
95
96     @Override
97     protected SubstatementValidator getSubstatementValidator() {
98         return validator;
99     }
100
101     @Override
102     protected GetFilterElementAttributesStatement createDeclared(
103             final StmtContext<Void, GetFilterElementAttributesStatement, ?> ctx,
104             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
105         return new Declared(substatements);
106     }
107
108     @Override
109     protected GetFilterElementAttributesStatement createEmptyDeclared(
110             final StmtContext<Void, GetFilterElementAttributesStatement, ?> ctx) {
111         return Declared.EMPTY;
112     }
113
114     @Override
115     protected GetFilterElementAttributesEffectiveStatement createEffective(
116             final StmtContext<Void, GetFilterElementAttributesStatement,
117                 GetFilterElementAttributesEffectiveStatement> ctx,
118             final GetFilterElementAttributesStatement declared,
119             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
120         return new Effective(declared, substatements, ctx);
121     }
122
123     @Override
124     protected GetFilterElementAttributesEffectiveStatement createEmptyEffective(
125             final StmtContext<Void, GetFilterElementAttributesStatement,
126                 GetFilterElementAttributesEffectiveStatement> ctx,
127             final GetFilterElementAttributesStatement declared) {
128         return createEffective(ctx, declared, ImmutableList.of());
129     }
130
131     private static boolean computeSupported(final StmtContext<?, ?, ?> stmt) {
132         final StmtContext<?, ?, ?> parent = stmt.getParentContext();
133         if (parent == null) {
134             LOG.debug("No parent, ignoring get-filter-element-attributes statement");
135             return false;
136         }
137         if (parent.getPublicDefinition() != YangStmtMapping.ANYXML) {
138             LOG.debug("Parent is not an anyxml node, ignoring get-filter-element-attributes statement");
139             return false;
140         }
141         if (!"filter".equals(parent.rawStatementArgument())) {
142             LOG.debug("Parent is not named 'filter', ignoring get-filter-element-attributes statement");
143             return false;
144         }
145
146         final StmtContext<?, ?, ?> grandParent = parent.getParentContext();
147         if (grandParent == null) {
148             LOG.debug("No grandparent, ignoring get-filter-element-attributes statement");
149             return false;
150         }
151         if (grandParent.getPublicDefinition() != YangStmtMapping.INPUT) {
152             LOG.debug("Grandparent is not an input node, ignoring get-filter-element-attributes statement");
153             return false;
154         }
155
156         final StmtContext<?, ?, ?> greatGrandParent = grandParent.getParentContext();
157         if (greatGrandParent == null) {
158             LOG.debug("No grandparent, ignoring get-filter-element-attributes statement");
159             return false;
160         }
161         if (greatGrandParent.getPublicDefinition() != YangStmtMapping.RPC) {
162             LOG.debug("Grandparent is not an RPC node, ignoring get-filter-element-attributes statement");
163             return false;
164         }
165
166         switch (greatGrandParent.rawStatementArgument()) {
167             case "get":
168             case "get-config":
169                 return true;
170             default:
171                 LOG.debug("Great-grandparent is not named 'get' nor 'get-config, ignoring get-filter-element-attributes"
172                     + " statement");
173                 return false;
174         }
175     }
176 }