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/eplv10.html
8 package org.opendaylight.yangtools.yang.model.util.repo;
10 import org.opendaylight.yangtools.concepts.Delegator;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
19 * Abstract caching schema provider with support of multiple context
20 * per backing {@link SchemaSourceProvider}.
22 * @param <I> Input Schema Source Representation
23 * @param <O> Output Schema Source Representation
25 public abstract class AbstractCachingSchemaSourceProvider<I, O> implements AdvancedSchemaSourceProvider<O>,
26 Delegator<AdvancedSchemaSourceProvider<I>> {
28 private final AdvancedSchemaSourceProvider<I> defaultDelegate;
31 * Construct caching schema source provider with supplied delegate.
33 * Default delegate is is used to retrieve schema source when cache does not
34 * contain requested sources.
36 * @param delegate SchemaSourceProvided used to look up and retrieve schema source
37 * when cache does not contain requested sources.
39 protected AbstractCachingSchemaSourceProvider(final AdvancedSchemaSourceProvider<I> delegate) {
40 this.defaultDelegate = delegate;
44 public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
45 Preconditions.checkNotNull(moduleName, "Module name should not be null.");
46 Preconditions.checkNotNull(revision, "Revision should not be null");
47 return getSchemaSource(SourceIdentifier.create(moduleName, revision));
51 public Optional<O> getSchemaSource(final SourceIdentifier sourceIdentifier) {
52 return getSchemaSourceImpl(sourceIdentifier, defaultDelegate);
56 * Actual implementation of schema source retrieval.
59 * <li>look up cached schema source via {@link #getCachedSchemaSource(SourceIdentifier)}
60 * <li>If source was found in cache, returns source to client code.
61 * <li>If source was not found in cache, Look up schema source in supplied <code>delegate</code>
62 * <li>Updates cache with schema from delegate by {@link #cacheSchemaSource(SourceIdentifier, Optional)}
63 * <li>Result is returned to client code.
66 * @param identifier Source identifier
67 * @param delegate Delegate to lookup if there is a miss.
68 * @return Optional of schema source, present if source was found. Absent otherwise.
70 protected final Optional<O> getSchemaSourceImpl(final SourceIdentifier identifier,
71 final AdvancedSchemaSourceProvider<I> delegate) {
72 Preconditions.checkNotNull(identifier, "Source identifier name should not be null.");
74 Optional<O> cached = getCachedSchemaSource(identifier);
75 if (cached.isPresent()) {
78 Optional<I> live = delegate.getSchemaSource(identifier);
79 return cacheSchemaSource(identifier, live);
83 * Caches supplied result and returns cached result which should be returned to client.
86 * Implementations of cache are required to cache schema source if possible.
87 * They are not required to cache {@link Optional#absent()}.
89 * Implementations are required to transform source representation if <code>O</code> and <code>I</code>
92 * This method SHOULD NOT fail and should recover from Runtime exceptions
93 * by not caching source and only transforming it.
95 * @param identifier Source Identifier for which schema SHOULD be cached
96 * @param input Optional of schema source, representing one returned from delegate.
97 * @return Optional of schema source, representing result returned from this cache.
99 abstract protected Optional<O> cacheSchemaSource(SourceIdentifier identifier, Optional<I> input);
102 * Returns cached schema source of {@link Optional#absent()} if source is not present in cache.
105 * Implementations of cache MUST return cached schema source, if it is present in cache,
106 * otherwise source will be requested from deleate and then cache will be updated
107 * via {@link #cacheSchemaSource(SourceIdentifier, Optional)}.
109 * @param identifier Source Identifier for which schema should be retrieved.
110 * @return Cached schema source.
112 abstract protected Optional<O> getCachedSchemaSource(SourceIdentifier identifier);
115 public AdvancedSchemaSourceProvider<I> getDelegate() {
116 return defaultDelegate;
120 * Creates an lightweight instance of source provider, which uses this cache for caching
121 * and supplied additional delegate for lookup of not cached sources.
124 * @param delegate Backing {@link SchemaSourceProvider} which should be used for lookup
125 * for sources not present in schema.
126 * @return new instance of {@link SchemaSourceProvider} which first lookup in cache
127 * and then in delegate.
131 public SchemaSourceProvider<O> createInstanceFor(final SchemaSourceProvider<I> delegate) {
132 return new SchemaSourceProviderInstance(SchemaSourceProviders.toAdvancedSchemaSourceProvider(delegate));
138 * Lightweight instance of source provider, which is associated with parent
139 * {@link AbstractCachingSchemaSourceProvider}, but uses
140 * different delegate for retrieving not cached sources.
144 private class SchemaSourceProviderInstance implements //
145 AdvancedSchemaSourceProvider<O>,
146 Delegator<AdvancedSchemaSourceProvider<I>> {
148 private final AdvancedSchemaSourceProvider<I> delegate;
150 protected SchemaSourceProviderInstance(final AdvancedSchemaSourceProvider<I> delegate) {
152 this.delegate = Preconditions.checkNotNull(delegate, "Delegate should not be null");;
156 public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
157 return getSchemaSource(SourceIdentifier.create(moduleName, revision));
161 public AdvancedSchemaSourceProvider<I> getDelegate() {
166 public Optional<O> getSchemaSource(final SourceIdentifier sourceIdentifier) {
167 return getSchemaSourceImpl(sourceIdentifier, getDelegate());