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