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.model.repo.util;
10 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ArrayListMultimap;
14 import com.google.common.collect.ListMultimap;
15 import com.google.common.collect.Lists;
16 import com.google.common.collect.Multimap;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
30 import javax.annotation.concurrent.GuardedBy;
31 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
34 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
36 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
37 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
38 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
39 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Abstract base class for {@link SchemaRepository} implementations. It handles registration and lookup of schema
46 * sources, subclasses need only to provide their own {@link #createSchemaContextFactory()} implementation.
49 public abstract class AbstractSchemaRepository implements SchemaRepository, SchemaSourceRegistry {
50 private static final Logger LOG = LoggerFactory.getLogger(AbstractSchemaRepository.class);
53 * Source identifier -> representation -> provider map. We usually are looking for
54 * a specific representation of a source.
57 private final Map<SourceIdentifier, ListMultimap<Class<? extends SchemaSourceRepresentation>,
58 AbstractSchemaSourceRegistration<?>>> sources = new HashMap<>();
61 * Schema source listeners.
64 private final List<SchemaListenerRegistration> listeners = new ArrayList<>();
66 @SuppressWarnings("unchecked")
67 private static <T extends SchemaSourceRepresentation> ListenableFuture<T> fetchSource(
68 final SourceIdentifier id, final Iterator<AbstractSchemaSourceRegistration<?>> it) {
69 final AbstractSchemaSourceRegistration<?> reg = it.next();
71 return Futures.catchingAsync(((SchemaSourceProvider<T>)reg.getProvider()).getSource(id), Throwable.class,
73 LOG.debug("Failed to acquire source from {}", reg, input);
76 return fetchSource(id, it);
79 throw new MissingSchemaSourceException("All available providers exhausted", id, input);
80 }, MoreExecutors.directExecutor());
84 public <T extends SchemaSourceRepresentation> ListenableFuture<T> getSchemaSource(final SourceIdentifier id,
85 final Class<T> representation) {
86 final ArrayList<AbstractSchemaSourceRegistration<?>> sortedSchemaSourceRegistrations;
89 final ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> srcs =
92 return immediateFailedFluentFuture(new MissingSchemaSourceException(
93 "No providers registered for source" + id, id));
96 sortedSchemaSourceRegistrations = Lists.newArrayList(srcs.get(representation));
99 // TODO, remove and make sources keep sorted multimap (e.g. ArrayListMultimap with SortedLists)
100 Collections.sort(sortedSchemaSourceRegistrations, SchemaProviderCostComparator.INSTANCE);
102 final Iterator<AbstractSchemaSourceRegistration<?>> regs = sortedSchemaSourceRegistrations.iterator();
103 if (!regs.hasNext()) {
104 return immediateFailedFluentFuture(new MissingSchemaSourceException(
105 "No providers for source " + id + " representation " + representation + " available", id));
108 final ListenableFuture<T> fetchSourceFuture = fetchSource(id, regs);
109 // Add callback to notify cache listeners about encountered schema
110 Futures.addCallback(fetchSourceFuture, new FutureCallback<T>() {
112 public void onSuccess(final T result) {
113 for (final SchemaListenerRegistration listener : listeners) {
114 listener.getInstance().schemaSourceEncountered(result);
119 @SuppressWarnings("checkstyle:parameterName")
120 public void onFailure(final Throwable t) {
121 LOG.trace("Skipping notification for encountered source {}, fetching source failed", id, t);
123 }, MoreExecutors.directExecutor());
125 return fetchSourceFuture;
128 private synchronized <T extends SchemaSourceRepresentation> void addSource(final PotentialSchemaSource<T> source,
129 final AbstractSchemaSourceRegistration<T> reg) {
130 ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> map =
131 sources.get(source.getSourceIdentifier());
133 map = ArrayListMultimap.create();
134 sources.put(source.getSourceIdentifier(), map);
137 map.put(source.getRepresentation(), reg);
139 final Collection<PotentialSchemaSource<?>> reps = Collections.singleton(source);
140 for (SchemaListenerRegistration l : listeners) {
141 l.getInstance().schemaSourceRegistered(reps);
145 private synchronized <T extends SchemaSourceRepresentation> void removeSource(final PotentialSchemaSource<?> source,
146 final SchemaSourceRegistration<?> reg) {
147 final Multimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> m =
148 sources.get(source.getSourceIdentifier());
150 m.remove(source.getRepresentation(), reg);
152 for (SchemaListenerRegistration l : listeners) {
153 l.getInstance().schemaSourceUnregistered(source);
157 sources.remove(source.getSourceIdentifier());
163 public <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(
164 final SchemaSourceProvider<? super T> provider, final PotentialSchemaSource<T> source) {
165 final PotentialSchemaSource<T> src = source.cachedReference();
167 final AbstractSchemaSourceRegistration<T> ret = new AbstractSchemaSourceRegistration<T>(provider, src) {
169 protected void removeRegistration() {
170 removeSource(src, this);
179 public SchemaListenerRegistration registerSchemaSourceListener(final SchemaSourceListener listener) {
180 final SchemaListenerRegistration ret = new AbstractSchemaListenerRegistration(listener) {
182 protected void removeRegistration() {
183 listeners.remove(this);
187 synchronized (this) {
188 final Collection<PotentialSchemaSource<?>> col = new ArrayList<>();
189 for (Multimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> m
190 : sources.values()) {
191 for (AbstractSchemaSourceRegistration<?> r : m.values()) {
192 col.add(r.getInstance());
196 // Notify first, so translator-type listeners, who react by registering a source
197 // do not cause infinite loop.
198 listener.schemaSourceRegistered(col);
204 private static class SchemaProviderCostComparator implements Comparator<AbstractSchemaSourceRegistration<?>>,
206 static final SchemaProviderCostComparator INSTANCE = new SchemaProviderCostComparator();
207 private static final long serialVersionUID = 1L;
210 public int compare(final AbstractSchemaSourceRegistration<?> o1, final AbstractSchemaSourceRegistration<?> o2) {
211 return o1.getInstance().getCost() - o2.getInstance().getCost();
214 private Object readResolve() {