2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.repo;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.kohsuke.MetaInfServices;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
21 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.spi.AbstractSchemaRepository;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
29 * A {@link SchemaRepository} which allows sharing of {@link SchemaContext} as long as their specification is the same.
32 * Note: for current implementation, "same" means the same filter and the same set of {@link SourceIdentifier}s.
35 @MetaInfServices(value = SchemaRepository.class)
36 public final class SharedSchemaRepository extends AbstractSchemaRepository implements Identifiable<String> {
37 private final LoadingCache<SchemaContextFactoryConfiguration, EffectiveModelContextFactory> cacheByConfig =
38 CacheBuilder.newBuilder().softValues()
39 .build(new CacheLoader<SchemaContextFactoryConfiguration, EffectiveModelContextFactory>() {
41 public EffectiveModelContextFactory load(final SchemaContextFactoryConfiguration key) {
42 return new SharedEffectiveModelContextFactory(SharedSchemaRepository.this, key);
46 private final @NonNull String id;
47 private final @NonNull YangParserFactory factory;
49 public SharedSchemaRepository() {
53 public SharedSchemaRepository(final String id) {
54 this(id, new YangParserFactoryImpl());
57 public SharedSchemaRepository(final String id, final YangParserFactory factory) {
58 this.id = requireNonNull(id);
59 this.factory = requireNonNull(factory);
63 public @NonNull String getIdentifier() {
68 public @NonNull EffectiveModelContextFactory createEffectiveModelContextFactory(
69 final @NonNull SchemaContextFactoryConfiguration config) {
70 return cacheByConfig.getUnchecked(config);
73 @NonNull YangParserFactory factory() {
78 public String toString() {
79 return "SchemaRepository: " + id;