2 * Copyright (c) 2016 Wipro Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.mwtn.devicemanager.impl.listener;
11 import java.util.List;
13 import javax.annotation.Nullable;
15 import org.opendaylight.mwtn.base.internalTypes.InternalDateAndTime;
16 import org.opendaylight.mwtn.base.internalTypes.InternalSeverity;
17 import org.opendaylight.mwtn.devicemanager.impl.ProviderClient;
18 import org.opendaylight.mwtn.devicemanager.impl.database.service.HtDatabaseEventsService;
19 import org.opendaylight.mwtn.devicemanager.impl.xml.AttributeValueChangedNotificationXml;
20 import org.opendaylight.mwtn.devicemanager.impl.xml.ObjectCreationNotificationXml;
21 import org.opendaylight.mwtn.devicemanager.impl.xml.ObjectDeletionNotificationXml;
22 import org.opendaylight.mwtn.devicemanager.impl.xml.ProblemNotificationXml;
23 import org.opendaylight.mwtn.devicemanager.impl.xml.WebSocketServiceClient;
24 import org.opendaylight.yang.gen.v1.uri.onf.microwavemodel.notifications.rev160809.AttributeValueChangedNotification;
25 import org.opendaylight.yang.gen.v1.uri.onf.microwavemodel.notifications.rev160809.MicrowaveModelNotificationsListener;
26 import org.opendaylight.yang.gen.v1.uri.onf.microwavemodel.notifications.rev160809.ObjectCreationNotification;
27 import org.opendaylight.yang.gen.v1.uri.onf.microwavemodel.notifications.rev160809.ObjectDeletionNotification;
28 import org.opendaylight.yang.gen.v1.uri.onf.microwavemodel.notifications.rev160809.ProblemNotification;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 * Important: Websocket notificatin must be the last action.
36 public class MicrowaveEventListener implements MicrowaveModelNotificationsListener {
38 private static final Logger LOG = LoggerFactory.getLogger(MicrowaveEventListener.class);
40 private final String nodeName;
41 private final WebSocketServiceClient webSocketService;
42 private final HtDatabaseEventsService databaseService;
43 private final ProviderClient dcaeProvider;
44 private final @Nullable ProviderClient aotsmClient;
46 public MicrowaveEventListener(String nodeName, WebSocketServiceClient webSocketService,
47 HtDatabaseEventsService databaseService, ProviderClient dcaeProvider,@Nullable ProviderClient aotsmClient) {
49 this.nodeName = nodeName;
50 this.webSocketService = webSocketService;
51 this.databaseService = databaseService;
52 this.dcaeProvider = dcaeProvider;
53 this.aotsmClient = aotsmClient;
58 public void onAttributeValueChangedNotification(AttributeValueChangedNotification notification) {
59 LOG.debug("Got event of type :: {}", AttributeValueChangedNotification.class.getSimpleName());
61 AttributeValueChangedNotificationXml notificationXml = new AttributeValueChangedNotificationXml(nodeName,
62 String.valueOf(notification.getCounter()), InternalDateAndTime.valueOf(notification.getTimeStamp()),
63 notification.getObjectIdRef().getValue(), notification.getAttributeName(), notification.getNewValue());
66 WebsocketEventInputBuilder builder = new WebsocketEventInputBuilder();
67 builder.setNodeName(nodeName);
68 builder.setEventType(AttributeValueChangedNotification.class.getSimpleName());
69 builder.setXmlEvent(xmlMapper.getXmlString(notificationXml));
70 WebsocketEventInput event= builder.build();
71 websocketmanagerService.websocketEvent(event);
73 databaseService.writeEventLog(notificationXml);
74 // Last notification to client to make shure that database already changed
75 webSocketService.sendViaWebsockets(nodeName, notificationXml);
79 public void onObjectCreationNotification(ObjectCreationNotification notification) {
80 LOG.debug("Got event of type :: {}", ObjectCreationNotification.class.getSimpleName());
82 //ObjectCreationNotificationXml notificationXml = new ObjectCreationNotificationXml(nodeName, notification);
83 ObjectCreationNotificationXml notificationXml = new ObjectCreationNotificationXml( nodeName,
84 notification.getCounter().toString(),
85 InternalDateAndTime.valueOf(notification.getTimeStamp()),
86 notification.getObjectIdRef().getValue());
87 /*public ObjectCreationNotificationXml(String nodeName, org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.ObjectCreationNotification notification) {
88 super(nodeName, notification.getCounter().toString(), InternalDateAndTime.valueOf(notification.getTimeStamp()),
89 notification.getObjectIdRef().getValue());
92 WebsocketEventInputBuilder builder = new WebsocketEventInputBuilder();
93 builder.setNodeName(nodeName);
94 builder.setEventType(ObjectCreationNotification.class.getSimpleName());
95 builder.setXmlEvent(xmlMapper.getXmlString(notificationXml));
96 websocketmanagerService.websocketEvent(builder.build());
98 databaseService.writeEventLog(notificationXml);
100 webSocketService.sendViaWebsockets(nodeName, notificationXml);
105 public void onObjectDeletionNotification(ObjectDeletionNotification notification) {
106 LOG.debug("Got event of type :: {}", ObjectDeletionNotification.class.getSimpleName());
108 //ObjectDeletionNotificationXml notificationXml = new ObjectDeletionNotificationXml(nodeName, notification);
109 ObjectDeletionNotificationXml notificationXml = new ObjectDeletionNotificationXml(nodeName,
110 notification.getCounter().toString(),
111 InternalDateAndTime.valueOf(notification.getTimeStamp()),
112 notification.getObjectIdRef().getValue()
115 WebsocketEventInputBuilder builder = new WebsocketEventInputBuilder();
116 builder.setNodeName(nodeName);
117 builder.setEventType(ObjectDeletionNotification.class.getSimpleName());
118 builder.setXmlEvent(xmlMapper.getXmlString(notificationXml));
119 websocketmanagerService.websocketEvent(builder.build());
121 databaseService.writeEventLog(notificationXml);
123 webSocketService.sendViaWebsockets(nodeName, notificationXml);
127 public void onProblemNotification(ProblemNotification notification) {
128 LOG.debug("Got event of type :: {}", ProblemNotification.class.getSimpleName());
130 ProblemNotificationXml notificationXml = new ProblemNotificationXml(nodeName, notification.getObjectIdRef().getValue(),
131 notification.getProblem(), InternalSeverity.valueOf(notification.getSeverity()),
132 notification.getCounter().toString(), InternalDateAndTime.valueOf(notification.getTimeStamp()));
134 WebsocketEventInputBuilder wsBuilder = new WebsocketEventInputBuilder();
135 wsBuilder.setNodeName(nodeName);
136 wsBuilder.setEventType(ProblemNotification.class.getSimpleName());
137 wsBuilder.setXmlEvent(xmlMapper.getXmlString(notificationXml));
138 websocketmanagerService.websocketEvent(wsBuilder.build());
141 databaseService.writeFaultLog(notificationXml);
142 databaseService.updateFaultCurrent(notificationXml);
144 if (dcaeProvider != null) {
145 dcaeProvider.sendProblemNotification(nodeName, notificationXml);
147 if (aotsmClient != null) {
148 aotsmClient.sendProblemNotification(nodeName, notificationXml);
151 webSocketService.sendViaWebsockets(nodeName, notificationXml);
155 private void initCurrentProblem(ProblemNotificationXml notificationXml) {
156 databaseService.updateFaultCurrent(notificationXml);
157 if (aotsmClient != null) {
158 aotsmClient.sendProblemNotification(this.nodeName, notificationXml);
163 * Called to initialize with the current status and notify the clients
164 * @param notificationXmlList List with problems
166 public void initCurrentProblem(List<ProblemNotificationXml> notificationXmlList) {
168 for (ProblemNotificationXml notificationXml : notificationXmlList) {
169 initCurrentProblem(notificationXml);
175 * Called on exit to remove everything from the current list.
176 * @return Number of deleted objects
178 public int removeAllCurrentProblemsOfNode() {
179 int deleted = databaseService.clearFaultsCurrentOfNode(nodeName);