Merge "Fix for bug #236 and bug #240 Have made changes in opendaylight-table-types...
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / 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.dom.broker;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.net.URL;
13 import java.util.Enumeration;
14
15 import org.opendaylight.controller.sal.core.api.model.SchemaService;
16 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
23 import org.opendaylight.yangtools.yang.parser.impl.util.URLSchemaContextResolver;
24 import org.osgi.framework.Bundle;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.BundleEvent;
27 import org.osgi.framework.ServiceReference;
28 import org.osgi.util.tracker.BundleTracker;
29 import org.osgi.util.tracker.BundleTrackerCustomizer;
30 import org.osgi.util.tracker.ServiceTracker;
31 import org.osgi.util.tracker.ServiceTrackerCustomizer;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Optional;
36 import com.google.common.collect.ImmutableSet;
37 import com.google.common.collect.ImmutableSet.Builder;
38
39 public class GlobalBundleScanningSchemaServiceImpl implements //
40         SchemaContextProvider, //
41         SchemaService, //
42         ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
43         AutoCloseable {
44     private static final Logger logger = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class);
45
46     private ListenerRegistry<SchemaServiceListener> listeners;
47
48     private BundleContext context;
49     private BundleScanner scanner = new BundleScanner();
50
51     private BundleTracker<ImmutableSet<Registration<URL>>> bundleTracker;
52
53     private final URLSchemaContextResolver contextResolver = new URLSchemaContextResolver();
54
55     private ServiceTracker<SchemaServiceListener, SchemaServiceListener> listenerTracker;
56
57     private boolean starting = true;
58
59     public ListenerRegistry<SchemaServiceListener> getListeners() {
60         return listeners;
61     }
62
63     public void setListeners(ListenerRegistry<SchemaServiceListener> listeners) {
64         this.listeners = listeners;
65     }
66
67     public BundleContext getContext() {
68         return context;
69     }
70
71     public void setContext(BundleContext context) {
72         this.context = context;
73     }
74
75     public void start() {
76         checkState(context != null);
77         if (listeners == null) {
78             listeners = new ListenerRegistry<>();
79         }
80
81         listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, GlobalBundleScanningSchemaServiceImpl.this);
82         bundleTracker = new BundleTracker<ImmutableSet<Registration<URL>>>(context, BundleEvent.RESOLVED
83                         | BundleEvent.UNRESOLVED, scanner);
84         bundleTracker.open();
85         listenerTracker.open();
86         starting = false;
87         tryToUpdateSchemaContext();
88     }
89
90     @Override
91     public SchemaContext getSchemaContext() {
92         return getGlobalContext();
93     }
94
95     public SchemaContext getGlobalContext() {
96         return contextResolver.getSchemaContext().orNull();
97     }
98
99     @Override
100     public void addModule(Module module) {
101         throw new UnsupportedOperationException();
102     }
103
104     @Override
105     public SchemaContext getSessionContext() {
106         throw new UnsupportedOperationException();
107     }
108
109     @Override
110     public void removeModule(Module module) {
111         throw new UnsupportedOperationException();
112     }
113
114     @Override
115     public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener listener) {
116         return listeners.register(listener);
117     }
118
119     @Override
120     public void close() throws Exception {
121         if (bundleTracker != null) {
122             bundleTracker.close();
123         }
124         if (listenerTracker != null) {
125             listenerTracker.close();
126         }
127         // FIXME: Add listeners.close();
128     }
129
130
131     private void updateContext(SchemaContext snapshot) {
132         Object[] services = listenerTracker.getServices();
133         if (services != null) {
134             for (Object rawListener : services) {
135                 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
136                 try {
137                     listener.onGlobalContextUpdated(snapshot);
138                 } catch (Exception e) {
139                     logger.error("Exception occured during invoking listener", e);
140                 }
141             }
142         }
143         for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
144             try {
145                 listener.getInstance().onGlobalContextUpdated(snapshot);
146             } catch (Exception e) {
147                 logger.error("Exception occured during invoking listener", e);
148             }
149         }
150     }
151
152     private class BundleScanner implements BundleTrackerCustomizer<ImmutableSet<Registration<URL>>> {
153         @Override
154         public ImmutableSet<Registration<URL>> addingBundle(Bundle bundle, BundleEvent event) {
155
156             if (bundle.getBundleId() == 0) {
157                 return ImmutableSet.of();
158             }
159
160             Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
161             Builder<Registration<URL>> builder = ImmutableSet.<Registration<URL>> builder();
162             while (enumeration != null && enumeration.hasMoreElements()) {
163                 Registration<URL> reg = contextResolver.registerSource(enumeration.nextElement());
164                 builder.add(reg);
165             }
166             ImmutableSet<Registration<URL>> urls = builder.build();
167             if(urls.isEmpty()) {
168                 return urls;
169             }
170             tryToUpdateSchemaContext();
171             return urls;
172         }
173
174         @Override
175         public void modifiedBundle(Bundle bundle, BundleEvent event, ImmutableSet<Registration<URL>> object) {
176             logger.debug("Modified bundle {} {} {}", bundle, event, object);
177         }
178
179         /**
180          * If removing YANG files makes yang store inconsistent, method
181          * {@link #getYangStoreSnapshot()} will throw exception. There is no
182          * rollback.
183          */
184
185         @Override
186         public synchronized void removedBundle(Bundle bundle, BundleEvent event, ImmutableSet<Registration<URL>> urls) {
187             for (Registration<URL> url : urls) {
188                 try {
189                     url.close();
190                 } catch (Exception e) {
191                     e.printStackTrace();
192                 }
193             }
194             tryToUpdateSchemaContext();
195         }
196     }
197
198     @Override
199     public SchemaServiceListener addingService(ServiceReference<SchemaServiceListener> reference) {
200
201         SchemaServiceListener listener = context.getService(reference);
202         SchemaContext _ctxContext = getGlobalContext();
203         if (getContext() != null && _ctxContext != null) {
204             listener.onGlobalContextUpdated(_ctxContext);
205         }
206         return listener;
207     }
208
209     public synchronized void tryToUpdateSchemaContext() {
210         if(starting ) {
211             return;
212         }
213         Optional<SchemaContext> schema = contextResolver.tryToUpdateSchemaContext();
214         if(schema.isPresent()) {
215             updateContext(schema.get());
216         }
217     }
218
219     @Override
220     public void modifiedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
221         // NOOP
222     }
223
224     @Override
225     public void removedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
226         context.ungetService(reference);
227     }
228 }