0b6e9b453db15482ca8f12b9680bb931b7774fcc
[ccsdk/features.git] / sdnr / wt / websocketmanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / websocketmanager / 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.websocketmanager;
19
20 import java.time.Instant;
21 import javax.servlet.ServletException;
22 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.WebsocketManagerService;
23 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.data.NotificationOutput;
24 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
25 import org.opendaylight.mdsal.dom.api.DOMNotification;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
27 import org.opendaylight.yangtools.yang.binding.Notification;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.osgi.service.http.HttpService;
30 import org.osgi.service.http.NamespaceException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class WebSocketManagerProvider implements WebsocketManagerService, AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(WebSocketManagerProvider.class);
37     private static final String APPLICATION_NAME = WebSocketManagerProvider.class.getName();
38     private static final String ALIAS = "/websocket";
39
40     private WebSocketManager wsServlet = null;
41
42     public WebSocketManagerProvider() {
43         LOG.info("Creating provider for {}", APPLICATION_NAME);
44     }
45
46
47     public void init() {
48         LOG.info("Init provider for {}", APPLICATION_NAME);
49     }
50
51     @Override
52     public void close() throws Exception {
53         LOG.info("Close provider for {}", APPLICATION_NAME);
54     }
55
56     public void onUnbindService(HttpService httpService) {
57         httpService.unregister(ALIAS);
58         wsServlet = null;
59     }
60
61     public void onBindService(HttpService httpService) throws ServletException, NamespaceException {
62         if (httpService == null) {
63             LOG.warn("Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService");
64         } else {
65
66             if (wsServlet == null) {
67                 wsServlet = new WebSocketManager();
68                 httpService.registerServlet(ALIAS, wsServlet, null, null);
69                 LOG.info("websocket servlet registered.");
70             } else {
71                 LOG.warn("Servelt ");
72             }
73         }
74
75     }
76
77     public WebSocketManager getWsServlet() {
78         return wsServlet;
79     }
80
81     public void setWsServlet(WebSocketManager wsServlet) {
82         this.wsServlet = wsServlet;
83     }
84
85
86     @Override
87     public void sendNotification(Notification notification, String nodeId, QName eventType) {
88         this.sendNotification(notification, nodeId, eventType, YangToolsMapperHelper.getTime(notification,Instant.now()));
89     }
90
91     @Override
92     public void sendNotification(Notification notification, String nodeId, QName eventType, DateAndTime eventTime) {
93         WebSocketManagerSocket.broadCast(new NotificationOutput(notification, nodeId, eventType, eventTime));
94
95     }
96
97     @Override
98     public void sendNotification(DOMNotification notification, String nodeId, QName eventType) {
99         LOG.warn("not yet implemented");
100
101     }
102
103     @Override
104     public void sendNotification(DOMNotification notification, String nodeId, QName eventType, DateAndTime eventTime) {
105         LOG.warn("not yet implemented");
106
107     }
108
109 }