/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/eplv10.html */ package org.opendaylight.yangtools.yang.model.util.repo; import com.google.common.annotations.Beta; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import org.opendaylight.yangtools.concepts.Delegator; /** * * Abstract caching schema provider with support of multiple context * per backing {@link SchemaSourceProvider}. * * @param Input Schema Source Representation * @param Output Schema Source Representation * * @deprecated Replaced with {@link org.opendaylight.yangtools.yang.model.repo.util.AbstractSchemaSourceCache} */ @Deprecated public abstract class AbstractCachingSchemaSourceProvider implements AdvancedSchemaSourceProvider, Delegator> { private final AdvancedSchemaSourceProvider defaultDelegate; /** * Construct caching schema source provider with supplied delegate. * * Default delegate is is used to retrieve schema source when cache does not * contain requested sources. * * @param delegate SchemaSourceProvided used to look up and retrieve schema source * when cache does not contain requested sources. */ protected AbstractCachingSchemaSourceProvider(final AdvancedSchemaSourceProvider delegate) { this.defaultDelegate = delegate; } @Override public Optional getSchemaSource(final String moduleName, final Optional revision) { Preconditions.checkNotNull(moduleName, "Module name should not be null."); Preconditions.checkNotNull(revision, "Revision should not be null"); return getSchemaSource(SourceIdentifier.create(moduleName, revision)); } @Override public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) { return getSchemaSourceImpl(sourceIdentifier, defaultDelegate); } /** * Actual implementation of schema source retrieval. * *
    *
  • look up cached schema source via {@link #getCachedSchemaSource(SourceIdentifier)} *
  • If source was found in cache, returns source to client code. *
  • If source was not found in cache, Look up schema source in supplied delegate *
  • Updates cache with schema from delegate by {@link #cacheSchemaSource(SourceIdentifier, Optional)} *
  • Result is returned to client code. *
* * @param identifier Source identifier * @param delegate Delegate to lookup if there is a miss. * @return Optional of schema source, present if source was found. Absent otherwise. */ protected final Optional getSchemaSourceImpl(final SourceIdentifier identifier, final AdvancedSchemaSourceProvider delegate) { Preconditions.checkNotNull(identifier, "Source identifier name should not be null."); Optional cached = getCachedSchemaSource(identifier); if (cached.isPresent()) { return cached; } Optional live = delegate.getSchemaSource(identifier); return cacheSchemaSource(identifier, live); } /** * Caches supplied result and returns cached result which should be returned to client. * *

* Implementations of cache are required to cache schema source if possible. * They are not required to cache {@link Optional#absent()}. * * Implementations are required to transform source representation if O and I * are different. * * This method SHOULD NOT fail and should recover from Runtime exceptions * by not caching source and only transforming it. * * @param identifier Source Identifier for which schema SHOULD be cached * @param input Optional of schema source, representing one returned from delegate. * @return Optional of schema source, representing result returned from this cache. */ abstract protected Optional cacheSchemaSource(SourceIdentifier identifier, Optional input); /** * Returns cached schema source of {@link Optional#absent()} if source is not present in cache. * *

* Implementations of cache MUST return cached schema source, if it is present in cache, * otherwise source will be requested from deleate and then cache will be updated * via {@link #cacheSchemaSource(SourceIdentifier, Optional)}. * * @param identifier Source Identifier for which schema should be retrieved. * @return Cached schema source. */ abstract protected Optional getCachedSchemaSource(SourceIdentifier identifier); @Override public AdvancedSchemaSourceProvider getDelegate() { return defaultDelegate; } /** * Creates an lightweight instance of source provider, which uses this cache for caching * and supplied additional delegate for lookup of not cached sources. *

* * @param delegate Backing {@link SchemaSourceProvider} which should be used for lookup * for sources not present in schema. * @return new instance of {@link SchemaSourceProvider} which first lookup in cache * and then in delegate. * */ @Beta public SchemaSourceProvider createInstanceFor(final SchemaSourceProvider delegate) { return new SchemaSourceProviderInstance(SchemaSourceProviders.toAdvancedSchemaSourceProvider(delegate)); } /** * * Lightweight instance of source provider, which is associated with parent * {@link AbstractCachingSchemaSourceProvider}, but uses * different delegate for retrieving not cached sources. * */ @Beta private class SchemaSourceProviderInstance implements // AdvancedSchemaSourceProvider, Delegator> { private final AdvancedSchemaSourceProvider delegate; protected SchemaSourceProviderInstance(final AdvancedSchemaSourceProvider delegate) { super(); this.delegate = Preconditions.checkNotNull(delegate, "Delegate should not be null");; } @Override public Optional getSchemaSource(final String moduleName, final Optional revision) { return getSchemaSource(SourceIdentifier.create(moduleName, revision)); } @Override public AdvancedSchemaSourceProvider getDelegate() { return delegate; } @Override public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) { return getSchemaSourceImpl(sourceIdentifier, getDelegate()); } } }