2c54d2a78a15a6309a803657ddb400d0b3f23681
[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 import org.opendaylight.mdsal.binding.api.RpcProviderService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketmanagerService;
23 import org.opendaylight.yangtools.concepts.ObjectRegistration;
24 import org.osgi.service.http.HttpService;
25 import org.osgi.service.http.NamespaceException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class WebSocketManagerProvider extends Blueprint {
30
31     private static final Logger LOG = LoggerFactory.getLogger(WebSocketManagerProvider.class);
32     private static final String APPLICATION_NAME = WebSocketManagerProvider.class.getName();
33     private static final String ALIAS = "/websocket";
34
35     private WebSocketManager wsServlet = null;
36     private ObjectRegistration<WebSocketManager> websocketService = null;
37
38     public WebSocketManagerProvider() {
39         LOG.info("Creating provider for {}", APPLICATION_NAME);
40     }
41
42     @Override
43     public void init() {
44         LOG.info("Init provider for {}", APPLICATION_NAME);
45         RpcProviderService rpcProviderRegistry = this.getRpcProviderRegistry();
46         if (rpcProviderRegistry != null) {
47             if (wsServlet != null) {
48                 this.websocketService =
49                         rpcProviderRegistry.registerRpcImplementation(WebsocketmanagerService.class, wsServlet);
50                 LOG.info("websocketservice initialized");
51             } else {
52                 LOG.debug("wsServlet not yet 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             if (wsServlet == null) {
78                 wsServlet = new WebSocketManager();
79                 httpService.registerServlet(ALIAS, wsServlet, null, null);
80                 LOG.info("websocket servlet registered.");
81                 if (this.websocketService == null) {
82                     this.init();
83                 } else {
84                     LOG.info("websocketservice already initialized");
85                 }
86             } else {
87                 LOG.warn("Servelt ");
88             }
89         }
90
91     }
92
93     public WebSocketManager getWsServlet() {
94         return wsServlet;
95     }
96
97     public void setWsServlet(WebSocketManager wsServlet) {
98         this.wsServlet = wsServlet;
99     }
100 }