BUG 1082 Migrate sal-rest-connector to Async Data Broker API
[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 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
13 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
14 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
15 import org.opendaylight.controller.sal.core.api.Provider;
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.opendaylight.controller.sal.rest.api.RestConnector;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
24
25 public class RestconfProviderImpl implements Provider, AutoCloseable, RestConnector {
26
27     public final static String NOT_INITALIZED_MSG = "Restconf is not initialized yet. Please try again later";
28
29     private ListenerRegistration<SchemaServiceListener> listenerRegistration;
30     private PortNumber port;
31     public void setWebsocketPort(PortNumber port) {
32         this.port = port;
33     }
34
35     private Thread webSocketServerThread;
36
37     @Override
38     public void onSessionInitiated(ProviderSession session) {
39         final DOMDataBroker domDataBroker = session.getService(DOMDataBroker.class);
40
41         BrokerFacade.getInstance().setContext(session);
42         BrokerFacade.getInstance().setDomDataBroker( domDataBroker);
43
44         SchemaService schemaService = session.getService(SchemaService.class);
45         listenerRegistration = schemaService.registerSchemaServiceListener(ControllerContext.getInstance());
46         ControllerContext.getInstance().setSchemas(schemaService.getGlobalContext());
47         ControllerContext.getInstance().setMountService(session.getService(DOMMountPointService.class));
48
49         webSocketServerThread = new Thread(WebSocketServer.createInstance(port.getValue().intValue()));
50         webSocketServerThread.setName("Web socket server on port " + port);
51         webSocketServerThread.start();
52     }
53
54     @Override
55     public Collection<ProviderFunctionality> getProviderFunctionality() {
56         return Collections.emptySet();
57     }
58
59     @Override
60     public void close() {
61         if (listenerRegistration != null) {
62             listenerRegistration.close();
63         }
64         webSocketServerThread.interrupt();
65     }
66 }

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