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