SDNR WT configure alarm severity
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / listener / ODLEventListener.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 javax.annotation.Nullable;
21 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalDateAndTime;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalSeverity;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.NetconfTimeStamp;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.ProviderClient;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database.service.HtDatabaseEventsService;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.AttributeValueChangedNotificationXml;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectCreationNotificationXml;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectDeletionNotificationXml;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClient;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.MaintenanceService;
32 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.ProblemNotification;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Responsible class for documenting changes in the ODL itself. The occurence of such an event is
38  * documented in the database and to clients. Specific example here is the registration or
39  * deregistration of a netconf device. This service has an own eventcounter to apply to the ONF
40  * Coremodel netconf behaviour.
41  *
42  * Important: Websocket notification must be the last action.
43  *
44  * @author herbert
45  */
46
47 public class ODLEventListener {
48
49     private static final Logger LOG = LoggerFactory.getLogger(ODLEventListener.class);
50
51     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter();
52
53     private final String ownKeyName;
54     private final WebSocketServiceClient webSocketService;
55     private final HtDatabaseEventsService databaseService;
56     private final ProviderClient dcaeProvider;
57     private final ProviderClient aotsMProvider;
58     private final MaintenanceService maintenanceService;
59
60     private int eventNumber;
61
62     /*---------------------------------------------------------------
63      * Construct
64      */
65
66     /**
67      * Create a Service to document events to clients and within a database
68      *
69      * @param ownKeyName The name of this service, that is used in the database as identification key.
70      * @param webSocketService service to direct messages to clients
71      * @param databaseService service to write to the database
72      * @param dcaeProvider to deliver problems to
73      * @param maintenanceService2
74      */
75     @SuppressWarnings("javadoc")
76     public ODLEventListener(String ownKeyName, WebSocketServiceClient webSocketService,
77             HtDatabaseEventsService databaseService, ProviderClient dcaeProvider,
78             @Nullable ProviderClient aotsMProvider, MaintenanceService maintenanceService) {
79         super();
80
81         this.ownKeyName = ownKeyName;
82         this.webSocketService = webSocketService;
83
84         this.databaseService = databaseService;
85         this.dcaeProvider = dcaeProvider;
86         this.aotsMProvider = aotsMProvider;
87
88         this.eventNumber = 0;
89         this.maintenanceService = maintenanceService;
90
91     }
92
93     /*---------------------------------------------------------------
94      * Handling of ODL Controller events
95      */
96
97     /**
98      * A registration of a mountpoint occured, that is in connect state
99      *
100      * @param registrationName Name of the event that is used as key in the database.
101      */
102
103     public void registration(String registrationName) {
104
105         ObjectCreationNotificationXml cNotificationXml =
106                 new ObjectCreationNotificationXml(ownKeyName, getEventNumberAsString(),
107                         InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), registrationName);
108
109         // Write first to prevent missing entries
110         databaseService.writeEventLog(cNotificationXml);
111         webSocketService.sendViaWebsockets(registrationName, cNotificationXml);
112     }
113
114
115     /**
116      * A deregistration of a mountpoint occured.
117      *
118      * @param registrationName Name of the event that is used as key in the database.
119      */
120
121     public void deRegistration(String registrationName) {
122
123         ObjectDeletionNotificationXml dNotificationXml =
124                 new ObjectDeletionNotificationXml(ownKeyName, getEventNumberAsString(),
125                         InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), registrationName);
126
127         // Write first to prevent missing entries
128         databaseService.writeEventLog(dNotificationXml);
129         webSocketService.sendViaWebsockets(registrationName, dNotificationXml);
130
131     }
132
133     /**
134      * Mountpoint state changed .. from connected -> connecting or unable-to-connect or vis-e-versa.
135      *
136      * @param registrationName Name of the event that is used as key in the database.
137      */
138     public void updateRegistration(String registrationName, String attribute, String attributeNewValue) {
139         AttributeValueChangedNotificationXml notificationXml = new AttributeValueChangedNotificationXml(ownKeyName,
140                 getEventNumberAsString(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()),
141                 registrationName, attribute, attributeNewValue);
142         databaseService.writeEventLog(notificationXml);
143         webSocketService.sendViaWebsockets(registrationName, notificationXml);
144
145     }
146
147     /**
148      * At a mountpoint a problem situation is indicated
149      *
150      * @param registrationName indicating object within SDN controller, normally the mountpointName
151      * @param problemName that changed
152      * @param problemSeverity of the problem according to NETCONF/YANG
153      */
154
155     public void onProblemNotification(String registrationName, String problemName, InternalSeverity problemSeverity) {
156         LOG.debug("Got event of type :: {} or {} or {}", ProblemNotification.class.getSimpleName(),
157                 org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev180907.ProblemNotification.class
158                         .getSimpleName(),
159                 org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev181010.ProblemNotification.class
160                         .getSimpleName());
161         // notification
162
163         ProblemNotificationXml notificationXml =
164                 new ProblemNotificationXml(ownKeyName, registrationName, problemName, problemSeverity,
165                         // popEvntNumberAsString(), InternalDateAndTime.TESTPATTERN );
166                         getEventNumberAsString(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()));
167
168         databaseService.writeFaultLog(notificationXml);
169         databaseService.updateFaultCurrent(notificationXml);
170
171         if (!maintenanceService.isONFObjectInMaintenance(registrationName, notificationXml.getObjectId(),
172                 notificationXml.getProblem())) {
173             dcaeProvider.sendProblemNotification(ownKeyName, notificationXml);
174             if (aotsMProvider != null) {
175                 aotsMProvider.sendProblemNotification(ownKeyName, notificationXml, false);// not a nealarm, its a
176             }
177         } // sdncontroller alarm
178         else {
179             LOG.debug("Notification will not be sent to external services. Device " + registrationName
180                     + " is in maintenance mode");
181         }
182
183         webSocketService.sendViaWebsockets(registrationName, notificationXml);
184     }
185
186
187     /*---------------------------------------------
188      * Handling of ODL Controller events
189      */
190
191     /**
192      * Called on exit to remove everything for a node from the current list.
193      *
194      * @param nodeName to remove all problems for
195      * @return Number of deleted objects
196      */
197     public int removeAllCurrentProblemsOfNode(String nodeName) {
198         return databaseService.clearFaultsCurrentOfNodeWithObjectId(ownKeyName, nodeName);
199     }
200
201     /*---------------------------------------------------------------
202      * Get/Set
203      */
204
205     /**
206      * @return the ownKeyName
207      */
208     public String getOwnKeyName() {
209         return ownKeyName;
210     }
211
212     /*---------------------------------------------------------------
213      * Private
214      */
215
216     private String getEventNumberAsString() {
217         return String.valueOf(popEvntNumber());
218     }
219
220     private int popEvntNumber() {
221         return eventNumber++;
222     }
223 }