1f1d0eb83147533d1171a4e81a36a73d7f72acd0
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / RestconfProvider.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.impl;
9
10 import java.util.Collection;
11 import java.util.Collections;
12 import org.opendaylight.controller.sal.core.api.Broker;
13 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
14 import org.opendaylight.controller.sal.core.api.Provider;
15 import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.opendaylight.controller.sal.core.api.mount.MountService;
18 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
19 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
20 import org.opendaylight.controller.sal.streams.websockets.WebSocketServer;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
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
29 public class RestconfProvider implements BundleActivator, Provider, ServiceTrackerCustomizer<Broker, Broker> {
30
31     public final static String NOT_INITALIZED_MSG = "Restconf is not initialized yet. Please try again later";
32
33     private ListenerRegistration<SchemaServiceListener> listenerRegistration;
34     private ServiceTracker<Broker, Broker> brokerServiceTrancker;
35     private BundleContext bundleContext;
36     private Thread webSocketServerThread;
37
38     @Override
39     public void onSessionInitiated(ProviderSession session) {
40         DataBrokerService dataService = session.getService(DataBrokerService.class);
41
42         BrokerFacade.getInstance().setContext(session);
43         BrokerFacade.getInstance().setDataService(dataService);
44
45         SchemaService schemaService = session.getService(SchemaService.class);
46         listenerRegistration = schemaService.registerSchemaServiceListener(ControllerContext.getInstance());
47         ControllerContext.getInstance().setSchemas(schemaService.getGlobalContext());
48         ControllerContext.getInstance().setMountService(session.getService(MountService.class));
49     }
50
51     @Override
52     public void start(BundleContext context) throws Exception {
53         String websocketPortStr = context.getProperty(WebSocketServer.WEBSOCKET_SERVER_CONFIG_PROPERTY);
54         int websocketPort = (websocketPortStr != null && !"".equals(websocketPortStr)) ? Integer
55                 .parseInt(websocketPortStr) : WebSocketServer.DEFAULT_PORT;
56         bundleContext = context;
57         webSocketServerThread = new Thread(WebSocketServer.createInstance(websocketPort));
58         webSocketServerThread.setName("Web socket server");
59         webSocketServerThread.start();
60         brokerServiceTrancker = new ServiceTracker<>(context, Broker.class, this);
61         brokerServiceTrancker.open();
62     }
63
64     @Override
65     public void stop(BundleContext context) {
66         if (listenerRegistration != null) {
67             try {
68                 listenerRegistration.close();
69             } catch (Exception e) {
70                 e.printStackTrace();
71             }
72         }
73         webSocketServerThread.interrupt();
74         brokerServiceTrancker.close();
75     }
76
77     @Override
78     public Collection<ProviderFunctionality> getProviderFunctionality() {
79         return Collections.emptySet();
80     }
81
82     @Override
83     public Broker addingService(ServiceReference<Broker> reference) {
84         Broker broker = bundleContext.getService(reference);
85         broker.registerProvider(this, bundleContext);
86         return broker;
87     }
88
89     @Override
90     public void modifiedService(ServiceReference<Broker> reference, Broker service) {
91         // NOOP
92     }
93
94     @Override
95     public void removedService(ServiceReference<Broker> reference, Broker service) {
96         bundleContext.ungetService(reference);
97         BrokerFacade.getInstance().setContext(null);
98         BrokerFacade.getInstance().setDataService(null);
99         ControllerContext.getInstance().setSchemas(null);
100     }
101 }

©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.