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