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.repo.util;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ArrayListMultimap;
12 import com.google.common.collect.ListMultimap;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Multimap;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.FutureFallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.Iterator;
27 import javax.annotation.concurrent.GuardedBy;
28 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
29 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
30 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
34 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
35 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
38 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
39 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Abstract base class for {@link SchemaRepository} implementations. It handles registration
47 * and lookup of schema sources, subclasses need only to provide their own
48 * {@link #createSchemaContextFactory(SchemaSourceFilter)} implementation.
51 public abstract class AbstractSchemaRepository implements SchemaRepository, SchemaSourceRegistry {
52 private static final Logger LOG = LoggerFactory.getLogger(AbstractSchemaRepository.class);
53 private static final ExceptionMapper<SchemaSourceException> FETCH_MAPPER = ReflectiveExceptionMapper.create("Schema source fetch", SchemaSourceException.class);
56 * Source identifier -> representation -> provider map. We usually are looking for
57 * a specific representation of a source.
60 private final Map<SourceIdentifier, ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>>> sources = new HashMap<>();
63 * Schema source listeners.
66 private final Collection<SchemaListenerRegistration> listeners = new ArrayList<>();
68 private static final <T extends SchemaSourceRepresentation> CheckedFuture<T, SchemaSourceException> fetchSource(final SourceIdentifier id, final Iterator<AbstractSchemaSourceRegistration<?>> it) {
69 final AbstractSchemaSourceRegistration<?> reg = it.next();
71 @SuppressWarnings("unchecked")
72 final CheckedFuture<? extends T, SchemaSourceException> f = ((SchemaSourceProvider<T>)reg.getProvider()).getSource(id);
74 return Futures.makeChecked(Futures.withFallback(f, new FutureFallback<T>() {
76 public ListenableFuture<T> create(final Throwable t) throws SchemaSourceException {
77 LOG.debug("Failed to acquire source from {}", reg, t);
80 return fetchSource(id, it);
83 throw new MissingSchemaSourceException("All available providers exhausted", id, t);
89 public <T extends SchemaSourceRepresentation> CheckedFuture<T, SchemaSourceException> getSchemaSource(final SourceIdentifier id, final Class<T> representation) {
90 final ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> srcs = sources.get(id);
92 return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("No providers registered for source" + id, id));
95 // TODO, remove and make sources keep sorted multimap (e.g. ArrayListMultimap with SortedLists)
96 final ArrayList<AbstractSchemaSourceRegistration<?>> sortedSchemaSourceRegistrations = Lists.newArrayList(srcs.get(representation));
97 Collections.sort(sortedSchemaSourceRegistrations, SchemaProviderCostComparator.INSTANCE);
99 final Iterator<AbstractSchemaSourceRegistration<?>> regs = sortedSchemaSourceRegistrations.iterator();
100 if (!regs.hasNext()) {
101 return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(
102 new MissingSchemaSourceException("No providers for source " + id + " representation " + representation + " available", id));
105 CheckedFuture<T, SchemaSourceException> fetchSourceFuture = fetchSource(id, regs);
106 // Add callback to notify cache listeners about encountered schema
107 Futures.addCallback(fetchSourceFuture, new FutureCallback<T>() {
109 public void onSuccess(final T result) {
110 for (final SchemaListenerRegistration listener : listeners) {
111 listener.getInstance().schemaSourceEncountered(result);
116 public void onFailure(final Throwable t) {
117 LOG.trace("Skipping notification for encountered source {}, fetching source failed", id, t);
121 return fetchSourceFuture;
124 private synchronized <T extends SchemaSourceRepresentation> void addSource(final PotentialSchemaSource<T> source, final AbstractSchemaSourceRegistration<T> reg) {
125 ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> m = sources.get(source.getSourceIdentifier());
127 m = ArrayListMultimap.create();
128 sources.put(source.getSourceIdentifier(), m);
131 m.put(source.getRepresentation(), reg);
133 final Collection<PotentialSchemaSource<?>> reps = Collections.<PotentialSchemaSource<?>>singleton(source);
134 for (SchemaListenerRegistration l : listeners) {
135 l.getInstance().schemaSourceRegistered(reps);
139 private synchronized <T extends SchemaSourceRepresentation> void removeSource(final PotentialSchemaSource<?> source, final SchemaSourceRegistration<?> reg) {
140 final Multimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> m = sources.get(source.getSourceIdentifier());
142 m.remove(source.getRepresentation(), reg);
144 for (SchemaListenerRegistration l : listeners) {
145 l.getInstance().schemaSourceUnregistered(source);
149 sources.remove(source.getSourceIdentifier());
155 public <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(final SchemaSourceProvider<? super T> provider, final PotentialSchemaSource<T> source) {
156 final PotentialSchemaSource<T> src = source.cachedReference();
158 final AbstractSchemaSourceRegistration<T> ret = new AbstractSchemaSourceRegistration<T>(provider, src) {
160 protected void removeRegistration() {
161 removeSource(src, this);
170 public SchemaListenerRegistration registerSchemaSourceListener(final SchemaSourceListener listener) {
171 final SchemaListenerRegistration ret = new AbstractSchemaListenerRegistration(listener) {
173 protected void removeRegistration() {
174 listeners.remove(this);
178 synchronized (this) {
179 final Collection<PotentialSchemaSource<?>> col = new ArrayList<>();
180 for (Multimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> m : sources.values()) {
181 for (AbstractSchemaSourceRegistration<?> r : m.values()) {
182 col.add(r.getInstance());
186 // Notify first, so translator-type listeners, who react by registering a source
187 // do not cause infinite loop.
188 listener.schemaSourceRegistered(col);
194 private static class SchemaProviderCostComparator implements Comparator<AbstractSchemaSourceRegistration<?>> {
195 public static final SchemaProviderCostComparator INSTANCE = new SchemaProviderCostComparator();
198 public int compare(final AbstractSchemaSourceRegistration<?> o1, final AbstractSchemaSourceRegistration<?> o2) {
199 return o1.getInstance().getCost() - o2.getInstance().getCost();