BUG-8327: deprecate sal.core.api.model.SchemaService
[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 java.util.HashSet;
14 import java.util.Set;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.opendaylight.controller.sal.core.api.model.YangTextSourceProvider;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
20 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
26 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30
31 @Deprecated
32 public final class GlobalBundleScanningSchemaServiceImpl implements SchemaContextProvider, SchemaService,
33         YangTextSourceProvider, AutoCloseable {
34
35     @GuardedBy("lock")
36     private final Set<ListenerRegistration<?>> listeners = new HashSet<>();
37     private final Object lock = new Object();
38     private final DOMSchemaService schemaService;
39     private final DOMYangTextSourceProvider yangProvider;
40
41     private GlobalBundleScanningSchemaServiceImpl(final DOMSchemaService schemaService) {
42         this.schemaService = Preconditions.checkNotNull(schemaService);
43         this.yangProvider = (DOMYangTextSourceProvider) schemaService.getSupportedExtensions()
44                 .get(DOMYangTextSourceProvider.class);
45     }
46
47     public static GlobalBundleScanningSchemaServiceImpl createInstance(final DOMSchemaService schemaService) {
48         return new GlobalBundleScanningSchemaServiceImpl(schemaService);
49     }
50
51     @Override
52     public SchemaContext getSchemaContext() {
53         return schemaService.getGlobalContext();
54     }
55
56     @Override
57     public SchemaContext getGlobalContext() {
58         return schemaService.getGlobalContext();
59     }
60
61     @Override
62     public void addModule(final Module module) {
63         throw new UnsupportedOperationException();
64     }
65
66     @Override
67     public SchemaContext getSessionContext() {
68         throw new UnsupportedOperationException();
69     }
70
71     @Override
72     public void removeModule(final Module module) {
73         throw new UnsupportedOperationException();
74     }
75
76     @Override
77     public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
78             final SchemaContextListener listener) {
79         synchronized (lock) {
80             final ListenerRegistration<SchemaContextListener> reg = schemaService.registerSchemaContextListener(
81                 listener);
82
83             final ListenerRegistration<SchemaContextListener> ret =
84                     new AbstractListenerRegistration<SchemaContextListener>(listener) {
85                 @Override
86                 protected void removeRegistration() {
87                     synchronized (lock) {
88                         listeners.remove(this);
89                     }
90                     reg.close();
91                 }
92             };
93
94             listeners.add(ret);
95             return ret;
96         }
97     }
98
99     @Override
100     public void close() {
101         synchronized (lock) {
102             for (ListenerRegistration<?> l : listeners) {
103                 l.close();
104             }
105             listeners.clear();
106         }
107     }
108
109     @SuppressWarnings("unchecked")
110     @Override
111     public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(
112             final SourceIdentifier sourceIdentifier) {
113         if (yangProvider == null) {
114             return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
115                 "Source provider is not available", sourceIdentifier));
116         }
117
118         return (CheckedFuture<YangTextSchemaSource, SchemaSourceException>) yangProvider.getSource(sourceIdentifier);
119     }
120 }