2 * Copyright (c) 2013 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.controller.sal.schema.service.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.HashSet;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.controller.sal.core.api.model.SchemaService;
18 import org.opendaylight.controller.sal.core.api.model.YangTextSourceProvider;
19 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
20 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
21 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
27 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 public final class GlobalBundleScanningSchemaServiceImpl implements SchemaContextProvider, SchemaService,
34 YangTextSourceProvider, AutoCloseable {
37 private final Set<ListenerRegistration<?>> listeners = new HashSet<>();
38 private final Object lock = new Object();
39 private final DOMSchemaService schemaService;
40 private final DOMYangTextSourceProvider yangProvider;
42 private GlobalBundleScanningSchemaServiceImpl(final DOMSchemaService schemaService) {
43 this.schemaService = Preconditions.checkNotNull(schemaService);
44 this.yangProvider = (DOMYangTextSourceProvider) schemaService.getSupportedExtensions()
45 .get(DOMYangTextSourceProvider.class);
48 public static GlobalBundleScanningSchemaServiceImpl createInstance(final DOMSchemaService schemaService) {
49 return new GlobalBundleScanningSchemaServiceImpl(schemaService);
53 public SchemaContext getSchemaContext() {
54 return schemaService.getGlobalContext();
58 public SchemaContext getGlobalContext() {
59 return schemaService.getGlobalContext();
63 public void addModule(final Module module) {
64 throw new UnsupportedOperationException();
68 public SchemaContext getSessionContext() {
69 throw new UnsupportedOperationException();
73 public void removeModule(final Module module) {
74 throw new UnsupportedOperationException();
78 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
79 final SchemaContextListener listener) {
81 final ListenerRegistration<SchemaContextListener> reg = schemaService.registerSchemaContextListener(
84 final ListenerRegistration<SchemaContextListener> ret =
85 new AbstractListenerRegistration<SchemaContextListener>(listener) {
87 protected void removeRegistration() {
89 listeners.remove(this);
101 public void close() {
102 synchronized (lock) {
103 for (ListenerRegistration<?> l : listeners) {
110 @SuppressWarnings("unchecked")
112 public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(
113 final SourceIdentifier sourceIdentifier) {
114 if (yangProvider == null) {
115 return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
116 "Source provider is not available", sourceIdentifier));
119 return Futures.makeChecked((ListenableFuture<YangTextSchemaSource>) yangProvider.getSource(sourceIdentifier),
120 e -> new SchemaSourceException("Error retrieving source", e));