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