Bug 4506: Honor if-feature during SchemaContext assembly
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveStatementBase.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.yang.parser.stmt.rfc6020.effective;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
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.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
29
30 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
31
32     private static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
33             new Predicate<StmtContext<?,?,?>>() {
34         @Override
35         public boolean apply(final StmtContext<?, ?, ?> input) {
36             return input.isSupportedToBuildEffective();
37         }
38     };
39
40     private static final Predicate<StmtContext<?, ?,?>> IS_UNKNOWN_STATEMENT_CONTEXT =
41             new Predicate<StmtContext<?,?,?>>() {
42         @Override
43         public boolean apply(final StmtContext<?, ?, ?> input) {
44             return StmtContextUtils.isUnknownStatement(input);
45         }
46     };
47
48     private static final Predicate<StmtContext<?, ?, ?>> ARE_FEATURES_SUPPORTED =
49             new Predicate<StmtContext<?, ?, ?>>() {
50
51                 @Override
52                 public boolean apply(StmtContext<?, ?, ?> input) {
53                     return StmtContextUtils.areFeaturesSupported(input);
54                 }
55             };
56
57     private final List<? extends EffectiveStatement<?, ?>> substatements;
58     private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
59
60     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
61         this(ctx, true);
62     }
63
64     /**
65      * Constructor.
66      *
67      * @param ctx
68      *            context of statement.
69      * @param buildUnknownSubstatements
70      *            if it is false, the unknown substatements are omitted from
71      *            build of effective substatements till the call of either
72      *            effectiveSubstatements or getOmittedUnknownSubstatements
73      *            method. The main purpose of this is to allow the build of
74      *            recursive extension definitions.
75      */
76     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, boolean buildUnknownSubstatements) {
77
78         final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
79         final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
80
81         for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
82             if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
83                 substatementsInit.add(declaredSubstatement);
84                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
85                 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
86                         .getEffectOfStatement());
87             } else {
88                 substatementsInit.add(declaredSubstatement);
89             }
90         }
91         substatementsInit.addAll(effectiveSubstatements);
92
93         Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
94                 IS_SUPPORTED_TO_BUILD_EFFECTIVE);
95         if (!buildUnknownSubstatements) {
96             this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
97                     IS_UNKNOWN_STATEMENT_CONTEXT));
98             substatementsToBuild = Collections2.filter(substatementsToBuild,
99                     Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT));
100         } else {
101             this.unknownSubstatementsToBuild = ImmutableList.of();
102         }
103
104         substatementsToBuild = Collections2.filter(substatementsToBuild, ARE_FEATURES_SUPPORTED);
105
106         Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
107         this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, buildEffective));
108     }
109
110     Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
111         Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
112         return Collections2.transform(unknownSubstatementsToBuild, buildEffective);
113     }
114
115     @Override
116     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
117         throw new UnsupportedOperationException("Not implemented yet.");
118     }
119
120     @Override
121     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
122         throw new UnsupportedOperationException("Not implemented yet.");
123     }
124
125     @Override
126     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
127         if (unknownSubstatementsToBuild.isEmpty()) {
128             return substatements;
129         } else {
130             return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
131         }
132     }
133
134     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
135         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
136                 Predicates.instanceOf(type));
137         return possible.isPresent() ? type.cast(possible.get()) : null;
138     }
139
140     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
141         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
142                 Predicates.instanceOf(type));
143         return possible.isPresent() ? type.cast(possible.get()) : null;
144     }
145
146     @SuppressWarnings("unchecked")
147     protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
148         return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
149     }
150
151     protected final <T> T firstSubstatementOfType(final Class<T> type) {
152         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
153                 Predicates.instanceOf(type));
154         return possible.isPresent() ? type.cast(possible.get()) : null;
155     }
156
157     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
158         Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
159                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
160         return possible.isPresent() ? returnType.cast(possible.get()) : null;
161     }
162
163     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
164         return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();
165     }
166 }