migrate sdnr features to phosphorus
[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.DOMNotificationOutput;
24 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.data.NotificationOutput;
25 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.osgi.service.http.HttpService;
32 import org.osgi.service.http.NamespaceException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class WebSocketManagerProvider implements WebsocketManagerService, AutoCloseable {
37
38     private static final Logger LOG = LoggerFactory.getLogger(WebSocketManagerProvider.class);
39     private static final String APPLICATION_NAME = WebSocketManagerProvider.class.getName();
40     private static final String ALIAS = "/websocket";
41
42     private WebSocketManager wsServlet = null;
43
44     public WebSocketManagerProvider() {
45         LOG.info("Creating provider for {}", APPLICATION_NAME);
46     }
47
48
49     public void init() {
50         LOG.info("Init provider for {}", APPLICATION_NAME);
51     }
52
53     @Override
54     public void close() throws Exception {
55         LOG.info("Close provider for {}", APPLICATION_NAME);
56     }
57
58     public void onUnbindService(HttpService httpService) {
59         httpService.unregister(ALIAS);
60         wsServlet = null;
61     }
62
63     public void onBindService(HttpService httpService) throws ServletException, NamespaceException {
64         if (httpService == null) {
65             LOG.warn("Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService");
66         } else {
67
68             if (wsServlet == null) {
69                 wsServlet = new WebSocketManager();
70                 httpService.registerServlet(ALIAS, wsServlet, null, null);
71                 LOG.info("websocket servlet registered.");
72             } else {
73                 LOG.warn("Servelt ");
74             }
75         }
76
77     }
78
79     public WebSocketManager getWsServlet() {
80         return wsServlet;
81     }
82
83     public void setWsServlet(WebSocketManager wsServlet) {
84         this.wsServlet = wsServlet;
85     }
86
87     public static boolean assertNotificationType(Notification notification, QName eventType) {
88         final String yangTypeName = eventType.getLocalName();
89         final Class<?> cls = notification.getClass();
90         final String clsNameToTest = YangToolsMapperHelper.toCamelCaseClassName(yangTypeName);
91         if (cls.getSimpleName().equals(clsNameToTest)) {
92             return true;
93         }
94         Class<?>[] ifs = cls.getInterfaces();
95         for (Class<?> clsif : ifs) {
96             if (clsif.getSimpleName().equals(clsNameToTest)) {
97                 return true;
98             }
99         }
100         return false;
101     }
102
103     @Override
104     public void sendNotification(Notification notification, NodeId nodeId, QName eventType) {
105         if (!assertNotificationType(notification, eventType)) {
106             return;
107         }
108         this.sendNotification(notification, nodeId, eventType,
109                 YangToolsMapperHelper.getTime(notification, Instant.now()));
110     }
111
112     @Override
113     public void sendNotification(Notification notification, NodeId nodeId, QName eventType, DateAndTime eventTime) {
114         WebSocketManagerSocket.broadCast(new NotificationOutput(notification, nodeId.getValue(), eventType, eventTime));
115
116     }
117
118     @Override
119     public void sendNotification(DOMNotification notification, NodeId nodeId, QName eventType) {
120         WebSocketManagerSocket.broadCast(new DOMNotificationOutput(notification, nodeId.getValue(), eventType,
121                 YangToolsMapperHelper.getTime(notification, Instant.now())));
122     }
123
124     @Override
125     public void sendNotification(DOMNotification notification, NodeId nodeId, QName eventType, DateAndTime eventTime) {
126         WebSocketManagerSocket
127                 .broadCast(new DOMNotificationOutput(notification, nodeId.getValue(), eventType, eventTime));
128     }
129
130 }