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