Fix malformed time stamp
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / listener / NetconfEventListener12.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.impl.listener;
19
20 import java.util.List;
21
22 import javax.annotation.Nullable;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalSeverity;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.ONFCoreNetworkElement12Equipment;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.ONFCoreNetworkElementCallback;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.wrapperc.OnfMicrowaveModelNotification;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.toggleAlarmFilter.NotificationDelayFilter;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.toggleAlarmFilter.NotificationDelayService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.toggleAlarmFilter.NotificationDelayedListener;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.ProviderClient;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database.service.HtDatabaseEventsService;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.AttributeValueChangedNotificationXml;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectCreationNotificationXml;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectDeletionNotificationXml;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClient;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.MaintenanceService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Important: Websocket notification must be the last action.
43  * At the beginning intended to handle notifications of type <code>OnfMicrowaveModelNotification</code>.
44  * Today an abstract class for processing notifications independent of model.
45  *
46  * @author herbert
47  */
48 public class NetconfEventListener12 implements OnfMicrowaveModelNotification, NotificationDelayedListener<ProblemNotificationXml> {
49
50     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventListener12.class);
51
52     private final String nodeName;
53     private final WebSocketServiceClient webSocketService;
54     //private final WebsocketmanagerService websocketmanagerService;
55     //private final XmlMapper xmlMapper;
56     private final HtDatabaseEventsService databaseService;
57     private final ProviderClient dcaeProvider;
58     private final @Nullable ProviderClient aotsmClient;
59
60     private final MaintenanceService maintenanceService;
61
62     private final NotificationDelayFilter<ProblemNotificationXml> delayFilter;
63     private final ONFCoreNetworkElementCallback ne;
64
65     public NetconfEventListener12(String nodeName, WebSocketServiceClient webSocketService,
66             HtDatabaseEventsService databaseService, ProviderClient dcaeProvider,@Nullable ProviderClient aotsmClient,
67             MaintenanceService maintenanceService2,NotificationDelayService<ProblemNotificationXml> notificationDelayService,
68             ONFCoreNetworkElementCallback ne) {
69         super();
70         this.nodeName = nodeName;
71         //this.websocketmanagerService = websocketmanagerService;
72         //this.xmlMapper = xmlMapper;
73         this.webSocketService = webSocketService;
74         this.databaseService = databaseService;
75         this.dcaeProvider = dcaeProvider;
76         this.aotsmClient = aotsmClient;
77         this.maintenanceService=maintenanceService2;
78         this.delayFilter=notificationDelayService.getInstance(nodeName, this);//12(nodeName,this);
79         this.ne = ne;
80     }
81
82
83     @Override
84     public void onAttributeValueChangedNotification(AttributeValueChangedNotificationXml notificationXml) {
85
86         ne.notificationFromNeListener(notificationXml);
87
88         databaseService.writeEventLog(notificationXml);
89
90         webSocketService.sendViaWebsockets(nodeName, notificationXml);
91     }
92
93
94     @Override
95     public void onObjectCreationNotification(ObjectCreationNotificationXml notificationXml) {
96
97         databaseService.writeEventLog(notificationXml);
98
99         webSocketService.sendViaWebsockets(nodeName, notificationXml);
100
101     }
102
103     @Override
104     public void onObjectDeletionNotification(ObjectDeletionNotificationXml notificationXml) {
105
106         databaseService.writeEventLog(notificationXml);
107
108         webSocketService.sendViaWebsockets(nodeName, notificationXml);
109     }
110
111     @Override
112     public void onProblemNotification(ProblemNotificationXml notificationXml) {
113
114         databaseService.writeFaultLog(notificationXml);
115         databaseService.updateFaultCurrent(notificationXml);
116
117         //ToggleAlarmFilter functionality
118         if(NotificationDelayFilter.isEnabled())
119         {
120             if(notificationXml.getSeverity() == InternalSeverity.NonAlarmed) {
121                 delayFilter.clearAlarmNotification(notificationXml.getProblem(), notificationXml);
122             } else {
123                 delayFilter.pushAlarmNotification(notificationXml.getProblem(), notificationXml);
124             }
125         }
126         else
127         {
128              this.pushAlarmIfNotInMaintenance(notificationXml);
129         }
130         //end of ToggleAlarmFilter
131
132         this.webSocketService.sendViaWebsockets(nodeName, notificationXml);
133
134     }
135
136     @Override
137     public void onNotificationDelay(ProblemNotificationXml notificationXml) {
138
139         LOG.debug("Got delayed event of type :: {}", ProblemNotificationXml.class.getSimpleName());
140         this.pushAlarmIfNotInMaintenance(notificationXml);
141
142     }
143     private void pushAlarmIfNotInMaintenance(ProblemNotificationXml notificationXml)
144     {
145          if(!this.maintenanceService.isONFObjectInMaintenance(nodeName, notificationXml.getObjectId(), notificationXml.getProblem()))
146          {
147              this.dcaeProvider.sendProblemNotification(nodeName, notificationXml);
148              if(this.aotsmClient!=null) {
149                 this.aotsmClient.sendProblemNotification(nodeName, notificationXml);
150             }
151          }
152          else
153          {
154              LOG.debug("Notification will not be sent to external services. Device "+this.nodeName+" is in maintenance mode");
155          }
156     }
157     private void initCurrentProblem(ProblemNotificationXml notificationXml) {
158         databaseService.updateFaultCurrent(notificationXml);
159         //to prevent push alarms on reconnect
160         //=> only pushed alarms are forwared to dcae
161         //dcaeProvider.sendProblemNotification(nodeName, notificationXml);
162         if(aotsmClient!=null) {
163             aotsmClient.sendProblemNotification(this.nodeName, notificationXml);
164         }
165     }
166
167     /**
168      * Called to initialize with the current status and notify the clients
169      * @param notificationXmlList List with problems
170      */
171     public void initCurrentProblemStatus(List<ProblemNotificationXml> notificationXmlList) {
172
173         for (ProblemNotificationXml notificationXml : notificationXmlList) {
174             initCurrentProblem(notificationXml);
175         }
176
177     }
178
179     /**
180      * Called on exit to remove everything from the current list.
181      * @return Number of deleted objects
182      */
183     public int removeAllCurrentProblemsOfNode() {
184         int deleted = databaseService.clearFaultsCurrentOfNode(nodeName);
185         return deleted;
186     }
187
188     /**
189      * Called on exit to remove for one Object-Pac from the current list.
190      * @param objectId uuid of the interface-pac or equipment-pac
191      * @return Number of deleted objects
192      */
193     public int removeObjectsCurrentProblemsOfNode(String objectId) {
194         int deleted = databaseService.clearFaultsCurrentOfNodeWithObjectId(nodeName, objectId);
195         return deleted;
196     }
197
198     /**
199      * Write equipment data to database
200      * @param equipment to write
201      */
202     public void writeEquipment(ONFCoreNetworkElement12Equipment equipment) {
203         databaseService.writeInventory(equipment);
204     }
205
206  }