Update devicemanager components
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / eventdatahandler / ODLEventListenerHandler.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.eventdatahandler;
19
20 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
21 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
22 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeForwarderInternal;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalDateAndTime;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.NetworkElementConnectionEntitiyUtil;
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.service.EventHandlingService;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EventlogBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementConnectionEntity;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementDeviceType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.SourceType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Responsible class for documenting changes in the ODL itself. The occurence of such an event is
44  * documented in the database and to clients. Specific example here is the registration or
45  * deregistration of a netconf device. This service has an own eventcounter to apply to the ONF
46  * Coremodel netconf behaviour.
47  *
48  * Important: Websocket notification must be the last action.
49  *
50  * @author herbert
51  */
52
53 public class ODLEventListenerHandler implements EventHandlingService {
54
55     private static final Logger LOG = LoggerFactory.getLogger(ODLEventListenerHandler.class);
56
57     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter();
58
59     private final String ownKeyName;
60     private final WebSocketServiceClientInternal webSocketService;
61     private final DataProvider databaseService;
62     private final DcaeForwarderInternal aotsDcaeForwarder;
63
64     private int eventNumber;
65
66     /*---------------------------------------------------------------
67      * Construct
68      */
69
70     /**
71      * Create a Service to document events to clients and within a database
72      *
73      * @param ownKeyName The name of this service, that is used in the database as identification key.
74      * @param webSocketService service to direct messages to clients
75      * @param databaseService service to write to the database
76      * @param dcaeForwarder to deliver problems to external service
77      */
78     public ODLEventListenerHandler(String ownKeyName, WebSocketServiceClientInternal webSocketService,
79             DataProvider databaseService, DcaeForwarderInternal dcaeForwarder) {
80         super();
81
82         this.ownKeyName = ownKeyName;
83         this.webSocketService = webSocketService;
84
85         this.databaseService = databaseService;
86         this.aotsDcaeForwarder = dcaeForwarder;
87
88         this.eventNumber = 0;
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      * @param registrationName of device (mountpoint name)
99      * @param nNode with mountpoint data
100      */
101     @Override
102     public void registration(String registrationName, NetconfNode nNode) {
103
104         ObjectCreationNotificationXml cNotificationXml =
105                 new ObjectCreationNotificationXml(ownKeyName, popEvntNumber(),
106                         InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), registrationName);
107         NetworkElementConnectionEntity e = NetworkElementConnectionEntitiyUtil.getNetworkConnection(registrationName, nNode);
108         LOG.debug("registration networkelement-connection for {} with status {}", registrationName, e.getStatus());
109
110         // Write first to prevent missing entries
111         databaseService.updateNetworkConnection22(e, registrationName);
112         databaseService.writeConnectionLog(cNotificationXml.getConnectionlogEntity());
113         webSocketService.sendViaWebsockets(registrationName, cNotificationXml);
114     }
115
116     /**
117      * After registration
118      * @param mountpointNodeName uuid that is nodeId or mountpointId
119      * @param deviceType according to assessement
120      */
121     @Override
122     public void connectIndication(String mountpointNodeName, NetworkElementDeviceType deviceType) {
123
124         // Write first to prevent missing entries
125         LOG.debug("updating networkelement-connection devicetype for {} with {}",mountpointNodeName, deviceType);
126         NetworkElementConnectionEntity e = NetworkElementConnectionEntitiyUtil.getNetworkConnectionDeviceTpe(deviceType);
127         databaseService.updateNetworkConnectionDeviceType(e, mountpointNodeName);
128
129         AttributeValueChangedNotificationXml notificationXml = new AttributeValueChangedNotificationXml(ownKeyName,
130                 popEvntNumber(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()),
131                 mountpointNodeName, "deviceType", deviceType.name());
132         webSocketService.sendViaWebsockets(mountpointNodeName, notificationXml);
133     }
134
135
136     /**
137      * A deregistration of a mountpoint occured.
138      * @param registrationName Name of the event that is used as key in the database.
139      */
140     @Override
141     public void deRegistration(String registrationName) {
142
143         ObjectDeletionNotificationXml dNotificationXml =
144                 new ObjectDeletionNotificationXml(ownKeyName, popEvntNumber(),
145                         InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), registrationName);
146
147         // Write first to prevent missing entries
148         databaseService.removeNetworkConnection(registrationName);
149         databaseService.writeConnectionLog(dNotificationXml.getConnectionlogEntity());
150         webSocketService.sendViaWebsockets(registrationName, dNotificationXml);
151
152     }
153
154     /**
155      * Mountpoint state changed .. from connected -> connecting or unable-to-connect or vis-e-versa.
156      * @param registrationName Name of the event that is used as key in the database.
157      */
158
159     @Override
160     public void updateRegistration(String registrationName, String attribute, String attributeNewValue, NetconfNode nNode) {
161         AttributeValueChangedNotificationXml notificationXml = new AttributeValueChangedNotificationXml(ownKeyName,
162                 popEvntNumber(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()),
163                 registrationName, attribute, attributeNewValue);
164         NetworkElementConnectionEntity e = NetworkElementConnectionEntitiyUtil.getNetworkConnection(registrationName, nNode);
165         LOG.debug("updating networkelement-connection for {} with status {}", registrationName, e.getStatus());
166
167         databaseService.updateNetworkConnection22(e, registrationName);
168         databaseService.writeConnectionLog(notificationXml.getConnectionlogEntity());
169         webSocketService.sendViaWebsockets(registrationName, notificationXml);
170     }
171
172     /**
173      * At a mountpoint a problem situation is indicated
174      *
175      * @param registrationName indicating object within SDN controller, normally the mountpointName
176      * @param problemName that changed
177      * @param problemSeverity of the problem according to NETCONF/YANG
178      */
179
180     public void onProblemNotification(String registrationName, String problemName, InternalSeverity problemSeverity) {
181         LOG.debug("Got event of {} {} {}", registrationName, problemName, problemSeverity);
182         // notification
183
184         ProblemNotificationXml notificationXml =
185                 new ProblemNotificationXml(ownKeyName, registrationName, problemName, problemSeverity,
186                         // popEvntNumberAsString(), InternalDateAndTime.TESTPATTERN );
187                         popEvntNumber(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()));
188
189         databaseService.writeFaultLog(notificationXml.getFaultlog(SourceType.Controller));
190         databaseService.updateFaultCurrent(notificationXml.getFaultcurrent());
191
192         aotsDcaeForwarder.sendProblemNotificationUsingMaintenanceFilter(ownKeyName, notificationXml);
193
194         webSocketService.sendViaWebsockets(registrationName, notificationXml);
195     }
196
197     @Override
198     public void writeEventLog(String objectId, String msg, String value) {
199
200         LOG.debug("Got startComplete");
201         EventlogBuilder eventlogBuilder = new EventlogBuilder();
202         eventlogBuilder.setNodeId(ownKeyName).setTimestamp(new DateAndTime(NETCONFTIME_CONVERTER.getTimeStamp()))
203         .setObjectId(objectId).setAttributeName(msg).setNewValue(value).setCounter(popEvntNumber())
204         .setSourceType(SourceType.Controller);
205         databaseService.writeEventLog(eventlogBuilder.build());
206
207     }
208
209     /*---------------------------------------------
210      * Handling of ODL Controller events
211      */
212
213     /**
214      * Called on exit to remove everything for a node from the current list.
215      *
216      * @param nodeName to remove all problems for
217      * @return Number of deleted objects
218      */
219     public int removeAllCurrentProblemsOfNode(String nodeName) {
220         return databaseService.clearFaultsCurrentOfNodeWithObjectId(ownKeyName, nodeName);
221     }
222
223     /*---------------------------------------------------------------
224      * Get/Set
225      */
226
227     /**
228      * @return the ownKeyName
229      */
230     public String getOwnKeyName() {
231         return ownKeyName;
232     }
233
234     /*---------------------------------------------------------------
235      * Private
236      */
237     private Integer popEvntNumber() {
238         return eventNumber++;
239     }
240
241 }