31f4253318f146e95c2a0923f3f08d3e14cc4a2d
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / main / java / org / opendaylight / controller / sal / rest / doc / DocProvider.java
1 /*
2  * Copyright (c) 2014 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.rest.doc;
9
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.LinkedList;
13 import java.util.List;
14
15 import org.opendaylight.controller.sal.core.api.Broker;
16 import org.opendaylight.controller.sal.core.api.Provider;
17 import org.opendaylight.controller.sal.core.api.model.SchemaService;
18 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
19 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService.MountProvisionListener;
20 import org.opendaylight.controller.sal.rest.doc.impl.ApiDocGenerator;
21 import org.opendaylight.controller.sal.rest.doc.mountpoints.MountPointSwagger;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.osgi.framework.BundleActivator;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.ServiceReference;
26 import org.osgi.util.tracker.ServiceTracker;
27 import org.osgi.util.tracker.ServiceTrackerCustomizer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class DocProvider implements BundleActivator, ServiceTrackerCustomizer<Broker, Broker>,
32         Provider, AutoCloseable {
33
34     private final Logger _logger = LoggerFactory.getLogger(DocProvider.class);
35
36     private ServiceTracker<Broker, Broker> brokerServiceTracker;
37     private BundleContext bundleContext;
38     private Broker.ProviderSession session;
39
40     private final List<AutoCloseable> toClose = new LinkedList<>();
41
42     @Override
43     public void close() throws Exception {
44         stop(bundleContext);
45     }
46
47     @Override
48     public void onSessionInitiated(Broker.ProviderSession providerSession) {
49         SchemaService schemaService = providerSession.getService(SchemaService.class);
50         ApiDocGenerator.getInstance().setSchemaService(schemaService);
51
52         MountProvisionService mountService = providerSession
53                 .getService(MountProvisionService.class);
54         ListenerRegistration<MountProvisionListener> registration = mountService
55                 .registerProvisionListener(MountPointSwagger.getInstance());
56         MountPointSwagger.getInstance().setGlobalSchema(schemaService);
57         synchronized (toClose) {
58             toClose.add(registration);
59         }
60         MountPointSwagger.getInstance().setMountService(mountService);
61
62         _logger.debug("Restconf API Explorer started");
63     }
64
65     @Override
66     public Collection<ProviderFunctionality> getProviderFunctionality() {
67         return Collections.emptySet();
68     }
69
70     @Override
71     public void start(BundleContext context) throws Exception {
72         bundleContext = context;
73         brokerServiceTracker = new ServiceTracker(context, Broker.class, this);
74         brokerServiceTracker.open();
75     }
76
77     @Override
78     public void stop(BundleContext context) throws Exception {
79         if (brokerServiceTracker != null)
80             brokerServiceTracker.close();
81
82         if (session != null)
83             session.close();
84
85         synchronized (toClose) {
86             for (AutoCloseable close : toClose) {
87                 close.close();
88             }
89         }
90     }
91
92     @Override
93     public Broker addingService(ServiceReference<Broker> reference) {
94         Broker broker = bundleContext.getService(reference);
95         session = broker.registerProvider(this, bundleContext);
96         return broker;
97     }
98
99     @Override
100     public void modifiedService(ServiceReference<Broker> reference, Broker service) {
101         if (session != null)
102             session.close();
103
104         Broker broker = bundleContext.getService(reference);
105         session = broker.registerProvider(this, bundleContext);
106     }
107
108     @Override
109     public void removedService(ServiceReference<Broker> reference, Broker service) {
110         bundleContext.ungetService(reference);
111     }
112 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.