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