Merge "Snapshot changes"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / RestconfProviderImpl.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
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.rest.api.RestConnector;
19 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
20 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
21 import org.opendaylight.controller.sal.streams.websockets.WebSocketServer;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
25
26 public class RestconfProviderImpl implements Provider, AutoCloseable, RestConnector {
27
28     public final static String NOT_INITALIZED_MSG = "Restconf is not initialized yet. Please try again later";
29
30     private ListenerRegistration<SchemaContextListener> listenerRegistration;
31     private PortNumber port;
32     public void setWebsocketPort(PortNumber port) {
33         this.port = port;
34     }
35
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.registerSchemaContextListener(ControllerContext.getInstance());
47         ControllerContext.getInstance().setSchemas(schemaService.getGlobalContext());
48         ControllerContext.getInstance().setMountService(session.getService(MountService.class));
49
50         webSocketServerThread = new Thread(WebSocketServer.createInstance(port.getValue().intValue()));
51         webSocketServerThread.setName("Web socket server on port " + port);
52         webSocketServerThread.start();
53     }
54
55     @Override
56     public Collection<ProviderFunctionality> getProviderFunctionality() {
57         return Collections.emptySet();
58     }
59
60     @Override
61     public void close() {
62         if (listenerRegistration != null) {
63             listenerRegistration.close();
64         }
65         webSocketServerThread.interrupt();
66     }
67 }

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