1db1e2ca5ad1aaf47fefe59c24b243106596e5e6
[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.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.AttributeValueChangedNotificationXml;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectCreationNotificationXml;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectDeletionNotificationXml;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClientInternal;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl.MaintenanceServiceImpl;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.EquipmentService;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.DevicemanagerNotificationDelayService;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.NotificationDelayFilter;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter.NotificationDelayedListener;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.EquipmentData;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.EventlogNotificationBuilder;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.FaultData;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.FaultNotificationBuilder2;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EventlogEntity;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Faultcurrent;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.FaultcurrentBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.FaultlogEntity;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Inventory;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.SeverityType;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.SourceType;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class DeviceManagerDatabaseNotificationService implements NotificationService, EquipmentService, FaultService,NotificationDelayedListener<ProblemNotificationXml> {
56
57         private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerDatabaseNotificationService.class);
58
59         private final @NonNull DataProvider databaseService;
60     private final @NonNull WebSocketServiceClientInternal webSocketService;
61     private @NonNull
62     final DevicemanagerNotificationDelayService notificationDelayService;
63     private @NonNull
64     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, @NonNull WebSocketServiceClientInternal webSocketService,
77             @NonNull DevicemanagerNotificationDelayService notificationDelayService,
78             @NonNull DcaeForwarderInternal aotsDcaeForwarder) {
79         super();
80         HtAssert.nonnull(databaseService, maintenanceService,webSocketService, notificationDelayService);
81
82         this.databaseService = databaseService;
83         this.maintenanceService = maintenanceService;
84         this.webSocketService = webSocketService;
85         this.notificationDelayService = notificationDelayService;
86         this.aotsDcaeForwarder = aotsDcaeForwarder;
87     }
88
89     @Override
90     public void eventNotification(@NonNull EventlogEntity eventNotification) {
91         String nodeId = eventNotification.getNodeId();
92         if (nodeId == null) {
93             nodeId = "EmptyNodeId";
94         }
95         databaseService.writeEventLog(eventNotification);
96         webSocketService.sendViaWebsockets(nodeId, new AttributeValueChangedNotificationXml(eventNotification));
97     }
98
99     @Override
100     public void changeNotification(NodeId nodeId, @Nullable Integer counter, @Nullable DateAndTime timeStamp,
101             @Nullable String objectId, @Nullable String attributeName, @Nullable String newValue) {
102         EventlogEntity eventlogEntity = new EventlogNotificationBuilder(nodeId, counter, timeStamp, objectId, attributeName, newValue).build();
103         databaseService.writeEventLog(eventlogEntity);
104         webSocketService.sendViaWebsockets(nodeId.getValue(), new AttributeValueChangedNotificationXml(eventlogEntity));
105     }
106
107     @Override
108     public void creationNotification(NodeId nodeId, @Nullable Integer counter, @Nullable DateAndTime timeStamp,
109             @Nullable String objectId) {
110         EventlogEntity eventlogEntity = new EventlogNotificationBuilder(nodeId, counter, timeStamp, objectId, "creation", null).build();
111         databaseService.writeEventLog(eventlogEntity);
112         webSocketService.sendViaWebsockets(nodeId.getValue(), new ObjectCreationNotificationXml(eventlogEntity));
113     }
114
115
116     @Override
117     public void deletionNotification(NodeId nodeId, @Nullable Integer counter, @Nullable DateAndTime timeStamp,
118             @Nullable String objectId) {
119         EventlogEntity eventlogEntity = new EventlogNotificationBuilder(nodeId, counter, timeStamp, objectId, "deletion", null).build();
120         databaseService.writeEventLog(eventlogEntity);
121         webSocketService.sendViaWebsockets(nodeId.getValue(), new ObjectDeletionNotificationXml(eventlogEntity));
122     }
123
124     @Override
125     public void writeEquipment(@NonNull EquipmentData equipment) {
126         //equipment.getList().forEach(card -> databaseService.writeInventory(card));
127         HtAssert.nonnull(equipment);
128         List<Inventory> list = equipment.getList();
129         HtAssert.nonnull(list);
130         for (Inventory card : list) {
131             databaseService.writeInventory(card);
132         }
133     }
134
135     @Override
136     public void faultNotification(@NonNull FaultlogEntity faultNotification) {
137         databaseService.writeFaultLog(faultNotification);
138         databaseService.updateFaultCurrent(getFaultCurrent(faultNotification));
139
140         ProblemNotificationXml notificationXml = new ProblemNotificationXml(faultNotification);
141         String nodeName = faultNotification.getNodeId();
142         if(NotificationDelayFilter.isEnabled())
143         {
144             if(notificationXml.getSeverity() == InternalSeverity.NonAlarmed) {
145                 this.notificationDelayService.getInstance(nodeName, this).clearAlarmNotification(notificationXml);
146             } else {
147                 this.notificationDelayService.getInstance(nodeName, this).pushAlarmNotification(notificationXml);
148             }
149         }
150         else
151         {
152              this.pushAlarmIfNotInMaintenance(nodeName,notificationXml);
153         }
154         // ToggleAlarmFilter functionality
155 //        if (notificationDelayService.processNotification(notificationXml.getSeverity() == InternalSeverity.NonAlarmed,
156 //                notificationXml.getProblem(), notificationXml)) {
157 //              if (notificationDelayService.processNotification(notificationXml)) {
158 //                  aotsDcaeForwarder.sendProblemNotificationUsingMaintenanceFilter(nodeName, notificationXml);
159 //              }
160 //        }
161         // end of ToggleAlarmFilter
162
163         this.webSocketService.sendViaWebsockets(nodeName, notificationXml);
164     }
165     private void pushAlarmIfNotInMaintenance(String nodeName,ProblemNotificationXml notificationXml)
166     {
167          if(!this.maintenanceService.isONFObjectInMaintenance(nodeName, notificationXml.getObjectId(), notificationXml.getProblem()))
168          {
169              this.aotsDcaeForwarder.sendProblemNotification(nodeName, notificationXml);
170          }
171          else
172          {
173              LOG.debug("Notification will not be sent to external services. Device "+nodeName+" is in maintenance mode");
174          }
175     }
176     @Override
177     public void faultNotification(@NonNull NodeId nodeId, @Nullable Integer counter, @Nullable DateAndTime timeStamp,
178             @Nullable String objectId, @Nullable String problem, @Nullable SeverityType severity) {
179         FaultNotificationBuilder2 bFaultlog = new FaultNotificationBuilder2(nodeId, counter, timeStamp, objectId, problem, severity, SourceType.Netconf);
180         faultNotification(bFaultlog.build());
181
182     }
183
184     @Override
185     public int removeAllCurrentProblemsOfNode(@NonNull NodeId nodeId) {
186         int deleted = databaseService.clearFaultsCurrentOfNode(nodeId.getValue());
187         return deleted;
188     }
189
190     @Override
191     public void initCurrentProblemStatus(@NonNull NodeId nodeId, FaultData resultList) {
192         resultList.getProblemList().forEach(problem -> {
193             FaultcurrentBuilder bFaultcurrent = new FaultcurrentBuilder();
194             bFaultcurrent.fieldsFrom(problem);
195             databaseService.updateFaultCurrent(bFaultcurrent.build());
196             });
197     }
198
199     @Override
200     public int removeObjectsCurrentProblemsOfNode(@NonNull NodeId nodeId, String objectId) {
201         int deleted = databaseService.clearFaultsCurrentOfNodeWithObjectId(nodeId.getValue(), objectId);
202         return deleted;
203     }
204
205     private Faultcurrent getFaultCurrent(@NonNull FaultlogEntity problem) {
206         FaultcurrentBuilder bFaultcurrent = new FaultcurrentBuilder();
207         bFaultcurrent.fieldsFrom(problem);
208         return bFaultcurrent.build();
209
210     }
211
212         @Override
213         public void onNotificationDelay(String nodeName,ProblemNotificationXml notification) {
214                 LOG.debug("Got delayed event of type :: {}", ProblemNotificationXml.class.getSimpleName());
215         this.pushAlarmIfNotInMaintenance(nodeName,notification);
216         }
217 }