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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2;
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;
35 public class WebSocketManagerProvider extends Blueprint implements WebsocketmanagerService {
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";
41 private WebSocketManager wsServlet = null;
42 private ObjectRegistration<WebSocketManager> websocketService = null;
44 public WebSocketManagerProvider() {
45 LOG.info("Creating provider for {}", APPLICATION_NAME);
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");
58 LOG.debug("wsServlet not yet provided");
61 LOG.error("rpcProviderRegistry not provided");
66 public void close() throws Exception {
67 LOG.info("Close provider for {}", APPLICATION_NAME);
68 if (websocketService != null) {
69 websocketService.close();
73 public void onUnbindService(HttpService httpService) {
74 httpService.unregister(ALIAS);
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");
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) {
90 LOG.info("websocketservice already initialized");
99 public WebSocketManager getWsServlet() {
103 public void setWsServlet(WebSocketManager wsServlet) {
104 this.wsServlet = wsServlet;
108 public ListenableFuture<RpcResult<WebsocketEventOutput>> websocketEvent(WebsocketEventInput input) {
109 if (wsServlet != null) {
110 return wsServlet.websocketEvent(input);
112 RpcResultBuilder<WebsocketEventOutput> result = RpcResultBuilder.failed();
113 return result.withError(ErrorType.APPLICATION, "Not intialized").buildFuture();