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