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