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