Populate xpath/ hierarchy
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / EffectiveSchemaContext.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.reactor;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.function.Function;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
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.stmt.ModuleEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
25 import org.opendaylight.yangtools.yang.model.spi.SimpleSchemaContext;
26
27 @VisibleForTesting
28 public final class EffectiveSchemaContext extends SimpleSchemaContext implements EffectiveModelContext {
29     private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
30     private final ImmutableMap<QNameModule, ModuleEffectiveStatement> rootEffectiveStatements;
31
32     private EffectiveSchemaContext(final Set<Module> modules, final List<DeclaredStatement<?>> rootDeclaredStatements,
33             final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
34         super(modules);
35         this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
36         this.rootEffectiveStatements = rootEffectiveStatements.stream()
37                 .filter(ModuleEffectiveStatement.class::isInstance).map(ModuleEffectiveStatement.class::cast)
38                 .collect(ImmutableMap.toImmutableMap(ModuleEffectiveStatement::localQNameModule, Function.identity()));
39     }
40
41     static EffectiveSchemaContext create(final List<DeclaredStatement<?>> rootDeclaredStatements,
42             final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
43         final Set<Module> modules = new HashSet<>();
44         for (EffectiveStatement<?, ?> stmt : rootEffectiveStatements) {
45             if (stmt.getDeclared() instanceof ModuleStatement) {
46                 Verify.verify(stmt instanceof Module);
47                 modules.add((Module) stmt);
48             }
49         }
50
51         return new EffectiveSchemaContext(modules, rootDeclaredStatements, rootEffectiveStatements);
52     }
53
54     @VisibleForTesting
55     public List<DeclaredStatement<?>> getRootDeclaredStatements() {
56         return rootDeclaredStatements;
57     }
58
59     @Override
60     public ImmutableMap<QNameModule, ModuleEffectiveStatement> getModuleStatements() {
61         return rootEffectiveStatements;
62     }
63 }