Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / devicemanager / impl / listener / ODLEventListener.java
1 /*
2 * Copyright (c) 2017 highstreet technologies GmbH and others. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 */
8
9 package org.opendaylight.mwtn.devicemanager.impl.listener;
10
11 import javax.annotation.Nullable;
12
13 import org.opendaylight.mwtn.base.internalTypes.InternalDateAndTime;
14 import org.opendaylight.mwtn.base.internalTypes.InternalSeverity;
15 import org.opendaylight.mwtn.base.netconf.NetconfTimeStamp;
16 import org.opendaylight.mwtn.devicemanager.impl.ProviderClient;
17 import org.opendaylight.mwtn.devicemanager.impl.database.service.HtDatabaseEventsService;
18 import org.opendaylight.mwtn.devicemanager.impl.xml.ObjectCreationNotificationXml;
19 import org.opendaylight.mwtn.devicemanager.impl.xml.ObjectDeletionNotificationXml;
20 import org.opendaylight.mwtn.devicemanager.impl.xml.ProblemNotificationXml;
21 import org.opendaylight.mwtn.devicemanager.impl.xml.WebSocketServiceClient;
22 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.ProblemNotification;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Responsible class for documenting changes in the ODL itself. The occurence of
28  * such an event is documented in the database and to clients. Specific example
29  * here is the registration or deregistration of a netconf device. This service
30  * has an own eventcounter to apply to the ONF Coremodel netconf behaviour.
31  *
32  * Important: Websocket notification must be the last action.
33  *
34  * @author herbert
35  */
36
37 public class ODLEventListener {
38
39         private static final Logger LOG = LoggerFactory.getLogger(ODLEventListener.class);
40
41         private final String ownKeyName;
42
43         private final WebSocketServiceClient webSocketService;
44         private final HtDatabaseEventsService databaseService;
45         private final ProviderClient dcaeProvider;
46         private final ProviderClient aotsMProvider;
47         private int eventNumber;
48
49         /*---------------------------------------------------------------
50          * Construct
51          */
52
53         /**
54          * Create a Service to document events to clients and within a database
55          *
56          * @param ownKeyName
57          *            The name of this service, that is used in the database as
58          *            identification key.
59          * @param webSocketService
60          *            service to direct messages to clients
61          * @param databaseService
62          *            service to write to the database
63          * @param dcaeProvider
64          *            to deliver problems to
65          */
66         public ODLEventListener(String ownKeyName, WebSocketServiceClient webSocketService,
67                         HtDatabaseEventsService databaseService, ProviderClient dcaeProvider,
68                         @Nullable ProviderClient aotsMProvider) {
69                 super();
70
71                 this.ownKeyName = ownKeyName;
72                 this.webSocketService = webSocketService;
73
74                 this.databaseService = databaseService;
75                 this.dcaeProvider = dcaeProvider;
76                 this.aotsMProvider = aotsMProvider;
77
78                 this.eventNumber = 0;
79
80         }
81
82         /*---------------------------------------------------------------
83          * Functions
84          */
85
86         /**
87          * A registration of a mountpoint occured.
88          *
89          * @param registrationName
90          *            Name of the event that is used as key in the database.
91          */
92
93         public void registration(String registrationName) {
94
95                 ObjectCreationNotificationXml cNotificationXml = new ObjectCreationNotificationXml(ownKeyName,
96                                 popEvntNumberAsString(), InternalDateAndTime.valueOf(NetconfTimeStamp.getTimeStamp()),
97                                 registrationName);
98
99                 // Write first to prevent missing entries
100                 databaseService.writeEventLog(cNotificationXml);
101
102                 webSocketService.sendViaWebsockets(registrationName, cNotificationXml);
103
104         }
105
106         /**
107          * A deregistration of a mountpoint occured.
108          *
109          * @param registrationName
110          *            Name of the event that is used as key in the database.
111          */
112
113         public void deRegistration(String registrationName) {
114
115                 ObjectDeletionNotificationXml dNotificationXml = new ObjectDeletionNotificationXml(ownKeyName,
116                                 popEvntNumberAsString(), InternalDateAndTime.valueOf(NetconfTimeStamp.getTimeStamp()),
117                                 registrationName);
118
119                 // Write first to prevent missing entries
120                 databaseService.writeEventLog(dNotificationXml);
121
122                 webSocketService.sendViaWebsockets(registrationName, dNotificationXml);
123
124         }
125
126         /**
127          * At a mountpoint a problem situation is indicated
128          *
129          * @param registrationName
130          *            indicating object within SDN controller, normally the
131          *            mountpointName
132          * @param problemName
133          *            that changed
134          * @param problemSeverity
135          *            of the problem according to NETCONF/YANG
136          */
137
138         public void onProblemNotification(String registrationName, String problemName, InternalSeverity problemSeverity) {
139                 LOG.debug("Got event of type :: {}", ProblemNotification.class.getSimpleName());
140                 // notification
141
142                 ProblemNotificationXml notificationXml = new ProblemNotificationXml(ownKeyName, registrationName, problemName,
143                                 problemSeverity,
144                                 // popEvntNumberAsString(), InternalDateAndTime.TESTPATTERN );
145                                 popEvntNumberAsString(), InternalDateAndTime.valueOf(NetconfTimeStamp.getTimeStamp()));
146
147                 databaseService.writeFaultLog(notificationXml);
148                 databaseService.updateFaultCurrent(notificationXml);
149
150                 dcaeProvider.sendProblemNotification(ownKeyName, notificationXml);
151                 if (aotsMProvider != null)
152                         aotsMProvider.sendProblemNotification(ownKeyName, notificationXml, false);// not a nealarm, its a
153                                                                                                                                                                                 // sdncontroller alarm
154
155                 webSocketService.sendViaWebsockets(registrationName, notificationXml);
156         }
157
158         /**
159          * Called on exit to remove everything for a node from the current list.
160          *
161          * @param nodeName
162          *            to remove all problems for
163          * @return Number of deleted objects
164          */
165         public int removeAllCurrentProblemsOfNode(String nodeName) {
166                 return databaseService.clearFaultsCurrentOfNodeWithObjectId(ownKeyName, nodeName);
167         }
168
169         /*---------------------------------------------------------------
170          * Private
171          */
172
173         private String popEvntNumberAsString() {
174                 return String.valueOf(popEvntNumber());
175         }
176
177         private int popEvntNumber() {
178                 return eventNumber++;
179         }
180 }