737fe5463dc7bf2020b8b5a06befc6f563cab99e
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2;
19
20 import javax.servlet.ServletException;
21
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketmanagerService;
25 import org.osgi.service.http.HttpService;
26 import org.osgi.service.http.NamespaceException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class WebSocketManagerProvider extends Blueprint {
31
32         private static final Logger LOG = LoggerFactory.getLogger(WebSocketManagerProvider.class);
33         private static final String APPLICATION_NAME = WebSocketManagerProvider.class.getName();
34         private static final String ALIAS = "/websocket";
35
36         private WebSocketManager wsServlet = null;
37         private RpcRegistration<WebsocketmanagerService> websocketService = null;
38         
39         public WebSocketManagerProvider() {
40                 LOG.info("Creating provider for {}", APPLICATION_NAME);
41         }
42
43         @Override
44         public void init() {
45                 LOG.info("Init provider for {}", APPLICATION_NAME);
46                 RpcProviderRegistry rpcProviderRegistry = this.getRpcProviderRegistry();
47                 if (rpcProviderRegistry != null) {
48                         if (wsServlet != null) {
49                                 this.websocketService = rpcProviderRegistry.addRpcImplementation(WebsocketmanagerService.class,
50                                                 wsServlet);
51                         } else {
52                                 LOG.error("wsServlet not provided");
53                         }
54                 } else {
55                         LOG.error("rpcProviderRegistry not provided");
56                 }
57         }
58
59         @Override
60         public void close() throws Exception {
61                 LOG.info("Close provider for {}", APPLICATION_NAME);
62                 if (websocketService != null) {
63                         websocketService.close();
64                 }
65         }
66
67         public void onUnbindService(HttpService httpService) {
68                 httpService.unregister(ALIAS);
69                 wsServlet = null;
70         }
71
72         public void onBindService(HttpService httpService) throws ServletException, NamespaceException {
73                 if (httpService == null) {
74                         LOG.warn("Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService");
75                 } else {
76                     
77                         wsServlet = new WebSocketManager();
78                         httpService.registerServlet(ALIAS, wsServlet, null, null);
79                         LOG.info("websocket servlet registered.");
80                         if(this.websocketService==null)
81                                 this.init();
82                         else
83                                 LOG.info("websocketservice already initialized");
84                 }
85
86         }
87
88         public WebSocketManager getWsServlet() {
89                 return wsServlet;
90         }
91
92         public void setWsServlet(WebSocketManager wsServlet) {
93                 this.wsServlet = wsServlet;
94         }
95 }