68177d373dd889cc5828503050279d4fbb30c323
[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 documented in the
45  * database and to clients. Specific example here is the registration or deregistration of a netconf device. This
46  * service has an own eventcounter to apply to the ONF 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      * 
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 = new ObjectCreationNotificationXml(ownKeyName, popEvntNumber(),
106                 InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), registrationName);
107         NetworkElementConnectionEntity e =
108                 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      * 
120      * @param mountpointNodeName uuid that is nodeId or mountpointId
121      * @param netconfNode
122      */
123     public void mountpointCreatedIndication(String mountpointNodeName, NetconfNode netconfNode) {
124         LOG.debug("mountpoint create indication for {}", mountpointNodeName);
125         this.registration(mountpointNodeName, netconfNode);
126     }
127
128     /**
129      * After registration
130      * 
131      * @param mountpointNodeName uuid that is nodeId or mountpointId
132      * @param deviceType according to assessement
133      */
134     @Override
135     public void connectIndication(String mountpointNodeName, NetworkElementDeviceType deviceType) {
136
137         // Write first to prevent missing entries
138         LOG.debug("updating networkelement-connection devicetype for {} with {}", mountpointNodeName, deviceType);
139         NetworkElementConnectionEntity e =
140                 NetworkElementConnectionEntitiyUtil.getNetworkConnectionDeviceTpe(deviceType);
141         databaseService.updateNetworkConnectionDeviceType(e, mountpointNodeName);
142
143         AttributeValueChangedNotificationXml notificationXml = new AttributeValueChangedNotificationXml(ownKeyName,
144                 popEvntNumber(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), mountpointNodeName,
145                 "deviceType", deviceType.name());
146         webSocketService.sendViaWebsockets(mountpointNodeName, notificationXml);
147     }
148
149     /**
150      * mountpoint state changed
151      * 
152      * @param mountpointNodeName
153      * @param netconfNode
154      */
155     public void onStateChangeIndication(String mountpointNodeName, NetconfNode netconfNode) {
156         LOG.debug("mountpoint state changed indication for {}", mountpointNodeName);
157         ConnectionStatus csts = netconfNode.getConnectionStatus();
158         this.updateRegistration(mountpointNodeName, ConnectionStatus.class.getSimpleName(),
159                 csts != null ? csts.getName() : "null", netconfNode);
160
161     }
162
163     /**
164      * A deregistration of a mountpoint occured.
165      * 
166      * @param registrationName Name of the event that is used as key in the database.
167      */
168     @Override
169     public void deRegistration(String registrationName) {
170
171         ObjectDeletionNotificationXml dNotificationXml = new ObjectDeletionNotificationXml(ownKeyName, popEvntNumber(),
172                 InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), registrationName);
173
174         // Write first to prevent missing entries
175         databaseService.removeNetworkConnection(registrationName);
176         databaseService.writeConnectionLog(dNotificationXml.getConnectionlogEntity());
177         webSocketService.sendViaWebsockets(registrationName, dNotificationXml);
178
179     }
180
181     /**
182      * Mountpoint state changed .. from connected -> connecting or unable-to-connect or vis-e-versa.
183      * 
184      * @param registrationName Name of the event that is used as key in the database.
185      */
186     @Override
187     public void updateRegistration(String registrationName, String attribute, String attributeNewValue,
188             NetconfNode nNode) {
189         AttributeValueChangedNotificationXml notificationXml = new AttributeValueChangedNotificationXml(ownKeyName,
190                 popEvntNumber(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()), registrationName,
191                 attribute, attributeNewValue);
192         NetworkElementConnectionEntity e =
193                 NetworkElementConnectionEntitiyUtil.getNetworkConnection(registrationName, nNode);
194         LOG.debug("updating networkelement-connection for {} with status {}", registrationName, e.getStatus());
195
196         databaseService.updateNetworkConnection22(e, registrationName);
197         databaseService.writeConnectionLog(notificationXml.getConnectionlogEntity());
198         webSocketService.sendViaWebsockets(registrationName, notificationXml);
199     }
200
201     /**
202      * At a mountpoint a problem situation is indicated
203      *
204      * @param registrationName indicating object within SDN controller, normally the mountpointName
205      * @param problemName that changed
206      * @param problemSeverity of the problem according to NETCONF/YANG
207      */
208
209     public void onProblemNotification(String registrationName, String problemName, InternalSeverity problemSeverity) {
210         LOG.debug("Got event of {} {} {}", registrationName, problemName, problemSeverity);
211         // notification
212
213         ProblemNotificationXml notificationXml =
214                 new ProblemNotificationXml(ownKeyName, registrationName, problemName, problemSeverity,
215                         // popEvntNumberAsString(), InternalDateAndTime.TESTPATTERN );
216                         popEvntNumber(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()));
217
218         databaseService.writeFaultLog(notificationXml.getFaultlog(SourceType.Controller));
219         databaseService.updateFaultCurrent(notificationXml.getFaultcurrent());
220
221         aotsDcaeForwarder.sendProblemNotificationUsingMaintenanceFilter(ownKeyName, notificationXml);
222
223         webSocketService.sendViaWebsockets(registrationName, notificationXml);
224     }
225
226     @Override
227     public void writeEventLog(String objectId, String msg, String value) {
228
229         LOG.debug("Got startComplete");
230         EventlogBuilder eventlogBuilder = new EventlogBuilder();
231         eventlogBuilder.setNodeId(ownKeyName).setTimestamp(new DateAndTime(NETCONFTIME_CONVERTER.getTimeStamp()))
232                 .setObjectId(objectId).setAttributeName(msg).setNewValue(value).setCounter(popEvntNumber())
233                 .setSourceType(SourceType.Controller);
234         databaseService.writeEventLog(eventlogBuilder.build());
235
236     }
237
238     /*---------------------------------------------
239      * Handling of ODL Controller events
240      */
241
242     /**
243      * Called on exit to remove everything for a node from the current list.
244      *
245      * @param nodeName to remove all problems for
246      * @return Number of deleted objects
247      */
248     public int removeAllCurrentProblemsOfNode(String nodeName) {
249         return databaseService.clearFaultsCurrentOfNodeWithObjectId(ownKeyName, nodeName);
250     }
251
252     /*---------------------------------------------------------------
253      * Get/Set
254      */
255
256     /**
257      * @return the ownKeyName
258      */
259     public String getOwnKeyName() {
260         return ownKeyName;
261     }
262
263     /*---------------------------------------------------------------
264      * Private
265      */
266     private Integer popEvntNumber() {
267         return eventNumber++;
268     }
269
270
271
272 }