6d2eacbaf5ff50df232ce3a8edba13674b4d82b9
[controller.git] / opendaylight / md-sal / sal-schema-service / src / main / java / org / opendaylight / controller / sal / schema / service / impl / GlobalBundleScanningSchemaServiceImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.schema.service.impl;
9
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;
15 import java.util.Set;
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;
31
32 @Deprecated
33 public final class GlobalBundleScanningSchemaServiceImpl implements SchemaContextProvider, SchemaService,
34         YangTextSourceProvider, AutoCloseable {
35
36     @GuardedBy("lock")
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;
41
42     private GlobalBundleScanningSchemaServiceImpl(final DOMSchemaService schemaService) {
43         this.schemaService = Preconditions.checkNotNull(schemaService);
44         this.yangProvider = (DOMYangTextSourceProvider) schemaService.getSupportedExtensions()
45                 .get(DOMYangTextSourceProvider.class);
46     }
47
48     public static GlobalBundleScanningSchemaServiceImpl createInstance(final DOMSchemaService schemaService) {
49         return new GlobalBundleScanningSchemaServiceImpl(schemaService);
50     }
51
52     @Override
53     public SchemaContext getSchemaContext() {
54         return schemaService.getGlobalContext();
55     }
56
57     @Override
58     public SchemaContext getGlobalContext() {
59         return schemaService.getGlobalContext();
60     }
61
62     @Override
63     public void addModule(final Module module) {
64         throw new UnsupportedOperationException();
65     }
66
67     @Override
68     public SchemaContext getSessionContext() {
69         throw new UnsupportedOperationException();
70     }
71
72     @Override
73     public void removeModule(final Module module) {
74         throw new UnsupportedOperationException();
75     }
76
77     @Override
78     public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
79             final SchemaContextListener listener) {
80         synchronized (lock) {
81             final ListenerRegistration<SchemaContextListener> reg = schemaService.registerSchemaContextListener(
82                 listener);
83
84             final ListenerRegistration<SchemaContextListener> ret =
85                     new AbstractListenerRegistration<SchemaContextListener>(listener) {
86                 @Override
87                 protected void removeRegistration() {
88                     synchronized (lock) {
89                         listeners.remove(this);
90                     }
91                     reg.close();
92                 }
93             };
94
95             listeners.add(ret);
96             return ret;
97         }
98     }
99
100     @Override
101     public void close() {
102         synchronized (lock) {
103             for (ListenerRegistration<?> l : listeners) {
104                 l.close();
105             }
106             listeners.clear();
107         }
108     }
109
110     @SuppressWarnings("unchecked")
111     @Override
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));
117         }
118
119         return Futures.makeChecked((ListenableFuture<YangTextSchemaSource>) yangProvider.getSource(sourceIdentifier),
120                 e -> new SchemaSourceException("Error retrieving source", e));
121     }
122 }