update old version
[ccsdk/features.git] / sdnr / wt / websocketmanager2 / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / websocketmanager2 / WebSocketManagerProvider.java
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 com.google.common.util.concurrent.ListenableFuture;
21 import javax.servlet.ServletException;
22 import org.opendaylight.mdsal.binding.api.RpcProviderService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketmanagerService;
26 import org.opendaylight.yangtools.concepts.ObjectRegistration;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
29 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
30 import org.osgi.service.http.HttpService;
31 import org.osgi.service.http.NamespaceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class WebSocketManagerProvider extends Blueprint implements WebsocketmanagerService {
36
37     private static final Logger LOG = LoggerFactory.getLogger(WebSocketManagerProvider.class);
38     private static final String APPLICATION_NAME = WebSocketManagerProvider.class.getName();
39     private static final String ALIAS = "/websocket";
40
41     private WebSocketManager wsServlet = null;
42     private ObjectRegistration<WebSocketManager> websocketService = null;
43
44     public WebSocketManagerProvider() {
45         LOG.info("Creating provider for {}", APPLICATION_NAME);
46     }
47
48     @Override
49     public void init() {
50         LOG.info("Init provider for {}", APPLICATION_NAME);
51         RpcProviderService rpcProviderRegistry = this.getRpcProviderRegistry();
52         if (rpcProviderRegistry != null) {
53             if (wsServlet != null) {
54                 this.websocketService =
55                         rpcProviderRegistry.registerRpcImplementation(WebsocketmanagerService.class, wsServlet);
56                 LOG.info("websocketservice initialized");
57             } else {
58                 LOG.debug("wsServlet not yet provided");
59             }
60         } else {
61             LOG.error("rpcProviderRegistry not provided");
62         }
63     }
64
65     @Override
66     public void close() throws Exception {
67         LOG.info("Close provider for {}", APPLICATION_NAME);
68         if (websocketService != null) {
69             websocketService.close();
70         }
71     }
72
73     public void onUnbindService(HttpService httpService) {
74         httpService.unregister(ALIAS);
75         wsServlet = null;
76     }
77
78     public void onBindService(HttpService httpService) throws ServletException, NamespaceException {
79         if (httpService == null) {
80             LOG.warn("Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService");
81         } else {
82
83             if (wsServlet == null) {
84                 wsServlet = new WebSocketManager();
85                 httpService.registerServlet(ALIAS, wsServlet, null, null);
86                 LOG.info("websocket servlet registered.");
87                 if (this.websocketService == null) {
88                     this.init();
89                 } else {
90                     LOG.info("websocketservice already initialized");
91                 }
92             } else {
93                 LOG.warn("Servelt ");
94             }
95         }
96
97     }
98
99     public WebSocketManager getWsServlet() {
100         return wsServlet;
101     }
102
103     public void setWsServlet(WebSocketManager wsServlet) {
104         this.wsServlet = wsServlet;
105     }
106
107     @Override
108     public ListenableFuture<RpcResult<WebsocketEventOutput>> websocketEvent(WebsocketEventInput input) {
109         if (wsServlet != null) {
110                         return wsServlet.websocketEvent(input);
111                 } else {
112             RpcResultBuilder<WebsocketEventOutput> result = RpcResultBuilder.failed();
113             return result.withError(ErrorType.APPLICATION, "Not intialized").buildFuture();
114         }
115     }
116 }