610001775d69b177365f3aeaaa7f42f6ea07d522
[ccsdk/features.git] /
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yangtools.yang.binding.Notification;
29 import org.opendaylight.yangtools.yang.common.QName;
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 implements WebsocketManagerService, AutoCloseable {
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
43     public WebSocketManagerProvider() {
44         LOG.info("Creating provider for {}", APPLICATION_NAME);
45     }
46
47
48     public void init() {
49         LOG.info("Init provider for {}", APPLICATION_NAME);
50     }
51
52     @Override
53     public void close() throws Exception {
54         LOG.info("Close provider for {}", APPLICATION_NAME);
55     }
56
57     public void onUnbindService(HttpService httpService) {
58         httpService.unregister(ALIAS);
59         wsServlet = null;
60     }
61
62     public void onBindService(HttpService httpService) throws ServletException, NamespaceException {
63         if (httpService == null) {
64             LOG.warn("Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService");
65         } else {
66
67             if (wsServlet == null) {
68                 wsServlet = new WebSocketManager();
69                 httpService.registerServlet(ALIAS, wsServlet, null, null);
70                 LOG.info("websocket servlet registered.");
71             } else {
72                 LOG.warn("Servelt ");
73             }
74         }
75
76     }
77
78     public WebSocketManager getWsServlet() {
79         return wsServlet;
80     }
81
82     public void setWsServlet(WebSocketManager wsServlet) {
83         this.wsServlet = wsServlet;
84     }
85
86
87     @Override
88     public void sendNotification(Notification notification, NodeId nodeId, QName eventType) {
89         if(!assertNotificationType(notification, eventType)){
90             return;
91         }
92         this.sendNotification(notification, nodeId, eventType, YangToolsMapperHelper.getTime(notification,Instant.now()));
93     }
94
95     public static boolean assertNotificationType(Notification notification, QName eventType) {
96         final String yangTypeName = eventType.getLocalName();
97         final Class<?> cls = notification.getClass();
98         final String clsNameToTest = YangToolsMapperHelper.toCamelCaseClassName(yangTypeName);
99         if(cls.getSimpleName().equals(clsNameToTest)) {
100             return true;
101         }
102         Class<?>[] ifs = cls.getInterfaces();
103         for(Class<?> clsif:ifs) {
104             if(clsif.getSimpleName().equals(clsNameToTest)) {
105                 return true;
106             }
107         }
108         return false;
109     }
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         LOG.warn("not yet implemented");
121
122     }
123
124     @Override
125     public void sendNotification(DOMNotification notification, NodeId nodeId, QName eventType, DateAndTime eventTime) {
126         LOG.warn("not yet implemented");
127
128     }
129
130 }