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==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager;
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;
36 public class WebSocketManagerProvider implements WebsocketManagerService, AutoCloseable {
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";
42 private WebSocketManager wsServlet = null;
44 public WebSocketManagerProvider() {
45 LOG.info("Creating provider for {}", APPLICATION_NAME);
50 LOG.info("Init provider for {}", APPLICATION_NAME);
54 public void close() throws Exception {
55 LOG.info("Close provider for {}", APPLICATION_NAME);
58 public void onUnbindService(HttpService httpService) {
59 httpService.unregister(ALIAS);
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");
68 if (wsServlet == null) {
69 wsServlet = new WebSocketManager();
70 httpService.registerServlet(ALIAS, wsServlet, null, null);
71 LOG.info("websocket servlet registered.");
79 public WebSocketManager getWsServlet() {
83 public void setWsServlet(WebSocketManager wsServlet) {
84 this.wsServlet = wsServlet;
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)) {
94 Class<?>[] ifs = cls.getInterfaces();
95 for (Class<?> clsif : ifs) {
96 if (clsif.getSimpleName().equals(clsNameToTest)) {
104 public void sendNotification(Notification notification, NodeId nodeId, QName eventType) {
105 if (!assertNotificationType(notification, eventType)) {
108 this.sendNotification(notification, nodeId, eventType,
109 YangToolsMapperHelper.getTime(notification, Instant.now()));
113 public void sendNotification(Notification notification, NodeId nodeId, QName eventType, DateAndTime eventTime) {
114 WebSocketManagerSocket.broadCast(new NotificationOutput(notification, nodeId.getValue(), eventType, eventTime));
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())));
125 public void sendNotification(DOMNotification notification, NodeId nodeId, QName eventType, DateAndTime eventTime) {
126 WebSocketManagerSocket
127 .broadCast(new DOMNotificationOutput(notification, nodeId.getValue(), eventType, eventTime));