3d570e59a89b5722ce5aa8e32da74f7239eb962d
[ccsdk/features.git] / sdnr / wt / devicemanager-core / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / eventdatahandler / DeviceManagerDatabaseNotificationService.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.devicemanager.eventdatahandler;
19
20 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
24 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeForwarderInternal;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClientInternal;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl.MaintenanceServiceImpl;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.EquipmentService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.DevicemanagerNotificationDelayService;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.NotificationDelayFilter;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.NotificationDelayedListener;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.EquipmentData;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.FaultData;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Faultcurrent;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultcurrentBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.AttributeValueChangedNotification;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.AttributeValueChangedNotificationBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ObjectCreationNotification;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ObjectCreationNotificationBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ObjectDeletionNotification;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ObjectDeletionNotificationBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ProblemNotification;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ProblemNotificationBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class DeviceManagerDatabaseNotificationService implements NotificationService, EquipmentService, FaultService,
57         NotificationDelayedListener<ProblemNotificationXml> {
58
59     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerDatabaseNotificationService.class);
60
61     private final @NonNull DataProvider databaseService;
62     private final @NonNull WebSocketServiceClientInternal webSocketService;
63     private @NonNull final DevicemanagerNotificationDelayService notificationDelayService;
64     private @NonNull final DcaeForwarderInternal aotsDcaeForwarder;
65
66     private final @NonNull MaintenanceServiceImpl maintenanceService;
67
68     /**
69      * @param databaseService to access database
70      * @param maintenanceService
71      * @param webSocketService to send notifications
72      * @param notificationDelayService filter to prevent toggle alarms
73      * @param aotsDcaeForwarder
74      */
75     public DeviceManagerDatabaseNotificationService(@NonNull DataProvider databaseService,
76             @NonNull MaintenanceServiceImpl maintenanceService,
77             @NonNull WebSocketServiceClientInternal webSocketService,
78             @NonNull DevicemanagerNotificationDelayService notificationDelayService,
79             @NonNull DcaeForwarderInternal aotsDcaeForwarder) {
80         super();
81         HtAssert.nonnull(databaseService, maintenanceService, webSocketService, notificationDelayService);
82
83         this.databaseService = databaseService;
84         this.maintenanceService = maintenanceService;
85         this.webSocketService = webSocketService;
86         this.notificationDelayService = notificationDelayService;
87         this.aotsDcaeForwarder = aotsDcaeForwarder;
88     }
89
90     @Override
91     public void eventNotification(@NonNull EventlogEntity eventNotification) {
92         String nodeId = eventNotification.getNodeId();
93         if (nodeId == null) {
94             nodeId = "EmptyNodeId";
95         }
96         databaseService.writeEventLog(eventNotification);
97         AttributeValueChangedNotification notification = new AttributeValueChangedNotificationBuilder()
98                 .setAttributeName(eventNotification.getAttributeName()).setCounter(eventNotification.getCounter())
99                 .setNewValue(eventNotification.getNewValue()).setObjectIdRef(eventNotification.getObjectId())
100                 .setTimeStamp(eventNotification.getTimestamp()).build();
101         this.webSocketService.sendViaWebsockets(new NodeId(nodeId), notification, AttributeValueChangedNotification.QNAME,
102                 eventNotification.getTimestamp());
103     }
104
105     @Override
106     public void changeNotification(NodeId nodeId, @Nullable Integer counter, @Nullable DateAndTime timeStamp,
107             @Nullable String objectId, @Nullable String attributeName, @Nullable String newValue) {
108         EventlogEntity eventlogEntity =
109                 new EventlogNotificationBuilder(nodeId, counter, timeStamp, objectId, attributeName, newValue).build();
110         this.eventNotification(eventlogEntity);
111     }
112
113     @Override
114     public void creationNotification(NodeId nodeId, @Nullable Integer counter, @Nullable DateAndTime timeStamp,
115             @Nullable String objectId) {
116         EventlogEntity eventlogEntity =
117                 new EventlogNotificationBuilder(nodeId, counter, timeStamp, objectId, "creation", null).build();
118         databaseService.writeEventLog(eventlogEntity);
119         ObjectCreationNotification notification = new ObjectCreationNotificationBuilder().setCounter(counter)
120                 .setObjectIdRef(objectId).setTimeStamp(eventlogEntity.getTimestamp()).build();
121         this.webSocketService.sendViaWebsockets(nodeId, notification, ObjectCreationNotification.QNAME,
122                 eventlogEntity.getTimestamp());
123     }
124
125
126     @Override
127     public void deletionNotification(NodeId nodeId, @Nullable Integer counter, @Nullable DateAndTime timeStamp,
128             @Nullable String objectId) {
129         EventlogEntity eventlogEntity =
130                 new EventlogNotificationBuilder(nodeId, counter, timeStamp, objectId, "deletion", null).build();
131         databaseService.writeEventLog(eventlogEntity);
132         ObjectDeletionNotification notification = new ObjectDeletionNotificationBuilder().setCounter(counter)
133                 .setObjectIdRef(objectId).setTimeStamp(eventlogEntity.getTimestamp()).build();
134         this.webSocketService.sendViaWebsockets(nodeId, notification, ObjectDeletionNotification.QNAME,
135                 eventlogEntity.getTimestamp());
136     }
137
138     @Override
139     public void writeEquipment(NodeId nodeId, @NonNull EquipmentData equipment) {
140         HtAssert.nonnull(equipment);
141         List<Inventory> list = equipment.getList();
142         HtAssert.nonnull(list);
143         databaseService.writeInventory(nodeId.getValue(), list);
144
145     }
146
147     @Override
148     public void faultNotification(@NonNull FaultlogEntity faultNotification) {
149         databaseService.writeFaultLog(faultNotification);
150         databaseService.updateFaultCurrent(getFaultCurrent(faultNotification));
151
152         ProblemNotificationXml notificationXml = new ProblemNotificationXml(faultNotification);
153         String nodeName = faultNotification.getNodeId();
154         if (NotificationDelayFilter.isEnabled()) {
155             if (notificationXml.getSeverity() == InternalSeverity.NonAlarmed) {
156                 this.notificationDelayService.getInstance(nodeName, this).clearAlarmNotification(notificationXml);
157             } else {
158                 this.notificationDelayService.getInstance(nodeName, this).pushAlarmNotification(notificationXml);
159             }
160         } else {
161             this.pushAlarmIfNotInMaintenance(nodeName, notificationXml);
162         }
163         // Send
164         ProblemNotification notification = new ProblemNotificationBuilder().setCounter(faultNotification.getCounter())
165                 .setObjectIdRef(faultNotification.getObjectId()).setTimeStamp(faultNotification.getTimestamp())
166                 .setProblem(faultNotification.getProblem())
167                 .setSeverity(InternalSeverity.toYang(faultNotification.getSeverity())).build();
168         this.webSocketService.sendViaWebsockets(new NodeId(faultNotification.getNodeId()), notification,
169                 ProblemNotification.QNAME, faultNotification.getTimestamp());
170     }
171
172     private void pushAlarmIfNotInMaintenance(String nodeName, ProblemNotificationXml notificationXml) {
173         if (!this.maintenanceService.isONFObjectInMaintenance(new NodeId(nodeName), notificationXml.getObjectId(),
174                 notificationXml.getProblem())) {
175             this.aotsDcaeForwarder.sendProblemNotification(new NodeId(nodeName), notificationXml);
176         } else {
177             LOG.debug("Notification will not be sent to external services. Device " + nodeName
178                     + " is in maintenance mode");
179         }
180     }
181
182     @Override
183     public int removeAllCurrentProblemsOfNode(@NonNull NodeId nodeId) {
184         int deleted = databaseService.clearFaultsCurrentOfNode(nodeId.getValue());
185         return deleted;
186     }
187
188     @Override
189     public void initCurrentProblemStatus(@NonNull NodeId nodeId, FaultData resultList) {
190         resultList.getProblemList().forEach(problem -> {
191             FaultcurrentBuilder bFaultcurrent = new FaultcurrentBuilder();
192             bFaultcurrent.fieldsFrom(problem);
193             databaseService.updateFaultCurrent(bFaultcurrent.build());
194         });
195     }
196
197     @Override
198     public int removeObjectsCurrentProblemsOfNode(@NonNull NodeId nodeId, String objectId) {
199         int deleted = databaseService.clearFaultsCurrentOfNodeWithObjectId(nodeId.getValue(), objectId);
200         return deleted;
201     }
202
203     private Faultcurrent getFaultCurrent(@NonNull FaultlogEntity problem) {
204         FaultcurrentBuilder bFaultcurrent = new FaultcurrentBuilder();
205         bFaultcurrent.fieldsFrom(problem);
206         return bFaultcurrent.build();
207
208     }
209
210     @Override
211     public void onNotificationDelay(String nodeName, ProblemNotificationXml notification) {
212         LOG.debug("Got delayed event of type :: {}", ProblemNotificationXml.class.getSimpleName());
213         this.pushAlarmIfNotInMaintenance(nodeName, notification);
214     }
215
216 }