Merge "fix oauth code"
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / listener / ODLEventListener.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.impl.listener;
19
20 import javax.annotation.Nullable;
21 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalDateAndTime;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalSeverity;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.NetconfTimeStamp;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.ProviderClient;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database.service.HtDatabaseEventsService;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectCreationNotificationXml;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ObjectDeletionNotificationXml;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClient;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.MaintenanceService;
31 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.ProblemNotification;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Responsible class for documenting changes in the ODL itself. The occurence of
37  * such an event is documented in the database and to clients. Specific example
38  * here is the registration or deregistration of a netconf device. This service
39  * has an own eventcounter to apply to the ONF Coremodel netconf behaviour.
40  *
41  * Important: Websocket notification must be the last action.
42  *
43  * @author herbert
44  */
45
46 public class ODLEventListener {
47
48     private static final Logger LOG = LoggerFactory.getLogger(ODLEventListener.class);
49     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter();
50
51
52     private final String ownKeyName;
53
54     private final WebSocketServiceClient webSocketService;
55     private final HtDatabaseEventsService databaseService;
56     private final ProviderClient dcaeProvider;
57     private final ProviderClient aotsMProvider;
58     private int eventNumber;
59     private final MaintenanceService maintenanceService;
60     /*---------------------------------------------------------------
61      * Construct
62      */
63
64     /**
65      * Create a Service to document events to clients and within a database
66      *
67      * @param ownKeyName          The name of this service, that is used in the
68      *                            database as identification key.
69      * @param webSocketService    service to direct messages to clients
70      * @param databaseService     service to write to the database
71      * @param dcaeProvider        to deliver problems to
72      * @param maintenanceService2
73      */
74     @SuppressWarnings("javadoc")
75     public ODLEventListener(String ownKeyName, WebSocketServiceClient webSocketService,
76             HtDatabaseEventsService databaseService, ProviderClient dcaeProvider,
77             @Nullable ProviderClient aotsMProvider, MaintenanceService maintenanceService) {
78         super();
79
80         this.ownKeyName = ownKeyName;
81         this.webSocketService = webSocketService;
82
83         this.databaseService = databaseService;
84         this.dcaeProvider = dcaeProvider;
85         this.aotsMProvider = aotsMProvider;
86
87         this.eventNumber = 0;
88         this.maintenanceService = maintenanceService;
89
90     }
91
92     /*---------------------------------------------------------------
93      * Functions
94      */
95
96     /**
97      * A registration of a mountpoint occured.
98      *
99      * @param registrationName Name of the event that is used as key in the
100      *                         database.
101      */
102
103     public void registration(String registrationName) {
104
105         ObjectCreationNotificationXml cNotificationXml = new ObjectCreationNotificationXml(ownKeyName,
106                 popEvntNumberAsString(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()),
107                 registrationName);
108
109         // Write first to prevent missing entries
110         databaseService.writeEventLog(cNotificationXml);
111
112         webSocketService.sendViaWebsockets(registrationName, cNotificationXml);
113
114     }
115
116     /**
117      * A deregistration of a mountpoint occured.
118      *
119      * @param registrationName Name of the event that is used as key in the
120      *                         database.
121      */
122
123     public void deRegistration(String registrationName) {
124
125         ObjectDeletionNotificationXml dNotificationXml = new ObjectDeletionNotificationXml(ownKeyName,
126                 popEvntNumberAsString(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()),
127                 registrationName);
128
129         // Write first to prevent missing entries
130         databaseService.writeEventLog(dNotificationXml);
131
132         webSocketService.sendViaWebsockets(registrationName, dNotificationXml);
133
134     }
135
136     /**
137      * At a mountpoint a problem situation is indicated
138      *
139      * @param registrationName indicating object within SDN controller, normally the
140      *                         mountpointName
141      * @param problemName      that changed
142      * @param problemSeverity  of the problem according to NETCONF/YANG
143      */
144
145     public void onProblemNotification(String registrationName, String problemName, InternalSeverity problemSeverity) {
146         LOG.debug("Got event of type :: {} or {} or {}", ProblemNotification.class.getSimpleName(),
147                 org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev180907.ProblemNotification.class
148                         .getSimpleName(),
149                 org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev181010.ProblemNotification.class
150                         .getSimpleName());
151         // notification
152
153         ProblemNotificationXml notificationXml = new ProblemNotificationXml(ownKeyName, registrationName, problemName,
154                 problemSeverity,
155                 // popEvntNumberAsString(), InternalDateAndTime.TESTPATTERN );
156                 popEvntNumberAsString(), InternalDateAndTime.valueOf(NETCONFTIME_CONVERTER.getTimeStamp()));
157
158         databaseService.writeFaultLog(notificationXml);
159         databaseService.updateFaultCurrent(notificationXml);
160
161         if (!maintenanceService.isONFObjectInMaintenance(registrationName, notificationXml.getObjectId(),
162                 notificationXml.getProblem())) {
163             dcaeProvider.sendProblemNotification(ownKeyName, notificationXml);
164             if (aotsMProvider != null) {
165                 aotsMProvider.sendProblemNotification(ownKeyName, notificationXml, false);// not a nealarm, its a
166             }
167         } // sdncontroller alarm
168         else {
169             LOG.debug("Notification will not be sent to external services. Device " + registrationName
170                     + " is in maintenance mode");
171         }
172
173         webSocketService.sendViaWebsockets(registrationName, notificationXml);
174     }
175
176     /**
177      * Called on exit to remove everything for a node from the current list.
178      *
179      * @param nodeName to remove all problems for
180      * @return Number of deleted objects
181      */
182     public int removeAllCurrentProblemsOfNode(String nodeName) {
183         return databaseService.clearFaultsCurrentOfNodeWithObjectId(ownKeyName, nodeName);
184     }
185
186     /*---------------------------------------------------------------
187      * Get/Set
188      */
189
190     /**
191      * @return the ownKeyName
192      */
193     public String getOwnKeyName() {
194         return ownKeyName;
195     }
196
197     /*---------------------------------------------------------------
198      * Private
199      */
200
201     private String popEvntNumberAsString() {
202         return String.valueOf(popEvntNumber());
203     }
204
205     private int popEvntNumber() {
206         return eventNumber++;
207     }
208 }