4efbf6e28e93d34b8f326bb9ba14286c62e90651
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.data;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Date;
27 import java.util.List;
28
29 import javax.annotation.Nonnull;
30
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
34 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.BoolQueryBuilder;
35 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
36 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
37 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.EsDataObjectReaderWriter2;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.ArchiveCleanProvider;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.ConnectionLogStatus;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.ConnectionlogBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.ConnectionlogEntity;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EventlogBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.EventlogEntity;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.FaultcurrentBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.FaultcurrentEntity;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.FaultlogBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.FaultlogEntity;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.GranularityPeriodType;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Inventory;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.InventoryBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.InventoryEntity;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementConnectionBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementConnectionEntity;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.PmdataEntity;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.PmdataEntityBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.UpdateNetworkElementConnectionInputBuilder;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66 /**
67  * Event service, writing all events into the database into the appropriate index.
68  *
69  * @author herbert
70  */
71 public class HtDatabaseEventsService implements ArchiveCleanProvider, DataProvider {
72     private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseEventsService.class);
73
74     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter();
75
76     private HtDatabaseClient client;
77     private EsDataObjectReaderWriter2<EventlogEntity> eventRWEventLogDevicemanager;
78     private EsDataObjectReaderWriter2<InventoryEntity> eventRWEquipment;
79     private EsDataObjectReaderWriter2<FaultcurrentEntity> eventRWFaultCurrentDB;
80     private EsDataObjectReaderWriter2<FaultlogEntity> eventRWFaultLogDB;
81     private EsDataObjectReaderWriter2<ConnectionlogEntity> eventRWConnectionLogDB;
82     private final EsDataObjectReaderWriter2<NetworkElementConnectionEntity> networkelementConnectionDB;
83     private final EsDataObjectReaderWriter2<PmdataEntity> pmData15mDB;
84     private final EsDataObjectReaderWriter2<PmdataEntity> pmData24hDB;
85
86     @SuppressWarnings("unused")
87     private final ElasticSearchDataProvider dataProvider;
88     // --- Construct and initialize
89
90
91     public HtDatabaseEventsService(HtDatabaseClient client, ElasticSearchDataProvider elasticSearchDataProvider)
92             throws Exception {
93
94         LOG.info("Create {} start", HtDatabaseEventsService.class);
95         this.dataProvider = elasticSearchDataProvider;
96
97         try {
98             // Create control structure
99             this.client = client;
100
101             eventRWEventLogDevicemanager = new EsDataObjectReaderWriter2<>(client, Entity.Eventlog,
102                     EventlogEntity.class, EventlogBuilder.class);
103
104             eventRWEquipment = new EsDataObjectReaderWriter2<>(client, Entity.Inventoryequipment, InventoryEntity.class,
105                     InventoryBuilder.class);
106
107             eventRWFaultCurrentDB = new EsDataObjectReaderWriter2<>(client, Entity.Faultcurrent,
108                     FaultcurrentEntity.class, FaultcurrentBuilder.class);
109
110             eventRWFaultLogDB = new EsDataObjectReaderWriter2<>(client, Entity.Faultlog, FaultlogEntity.class,
111                     FaultlogBuilder.class);
112
113             eventRWConnectionLogDB = new EsDataObjectReaderWriter2<>(client, Entity.Connectionlog,
114                     ConnectionlogEntity.class, ConnectionlogBuilder.class);
115
116             networkelementConnectionDB = new EsDataObjectReaderWriter2<>(client, Entity.NetworkelementConnection,
117                     NetworkElementConnectionEntity.class, NetworkElementConnectionBuilder.class, true)
118                             .setEsIdAttributeName("_id");
119
120             pmData15mDB = new EsDataObjectReaderWriter2<>(client, Entity.Historicalperformance15min, PmdataEntity.class,
121                     PmdataEntityBuilder.class);
122
123             pmData24hDB = new EsDataObjectReaderWriter2<>(client, Entity.Historicalperformance24h, PmdataEntity.class,
124                     PmdataEntityBuilder.class);
125
126         } catch (Exception e) {
127             LOG.error("Can not start database client. Exception: {}", e);
128             throw new Exception("Can not start database client. Exception: {}", e);
129         }
130         LOG.info("Create {} finished. DB Service {} started.", HtDatabaseEventsService.class,
131                 client != null ? "sucessfully" : "not");
132     }
133
134     // --- Function
135
136     // -- Connection log
137     @Override
138     public void writeConnectionLog(ConnectionlogEntity event) {
139         if (assertIfClientNull(event)) {
140             return;
141         }
142         LOG.debug("Write event: {}", event);
143         eventRWConnectionLogDB.write(event, null);
144
145     }
146
147     // -- Event log
148     @Override
149     public void writeEventLog(EventlogEntity event) {
150         if (assertIfClientNull("No client to write {}", event)) {
151             return;
152         }
153
154         LOG.debug("Write event: {}", event.toString());
155         eventRWEventLogDevicemanager.write(event, null);
156     }
157
158
159     // -- Fault log
160
161     @Override
162     public void writeFaultLog(FaultlogEntity fault) {
163         if (assertIfClientNull(fault)) {
164             return;
165         }
166
167         LOG.debug("Write fault to faultlog: {}", fault.toString());
168         eventRWFaultLogDB.write(fault, null);
169     }
170
171     // -- Fault current
172
173     @Override
174     public void updateFaultCurrent(FaultcurrentEntity fault) {
175         if (assertIfClientNull(fault)) {
176             return;
177         }
178
179         if (FaultEntityManager.isManagedAsCurrentProblem(fault)) {
180             if (FaultEntityManager.isNoAlarmIndication(fault)) {
181                 LOG.debug("Remove from currentFaults: {}", fault.toString());
182                 eventRWFaultCurrentDB.remove(FaultEntityManager.genSpecificEsId(fault));
183             } else {
184                 LOG.debug("Write to currentFaults: {}", fault.toString());
185                 eventRWFaultCurrentDB.write(fault, FaultEntityManager.genSpecificEsId(fault));
186             }
187         } else {
188             LOG.debug("Ingnore for currentFaults: {}", fault.toString());
189         }
190     }
191
192     /**
193      * Remove all entries for one node
194      *
195      * @param nodeName contains the mountpointname
196      * @return number of deleted entries
197      */
198     @Override
199     public int clearFaultsCurrentOfNode(String nodeName) {
200         if (assertIfClientNullForNodeName(nodeName)) {
201             return -1;
202         }
203
204         LOG.debug("Remove from currentFaults all faults for node: {}", nodeName);
205         return eventRWFaultCurrentDB.remove(EsFaultCurrent.getQueryForOneNode(nodeName));
206     }
207
208     /**
209      * Remove all entries for one node
210      *
211      * @param nodeName contains the mountpointname
212      * @param objectId of element to be deleted
213      * @return number of deleted entries
214      */
215     @Override
216     public int clearFaultsCurrentOfNodeWithObjectId(String nodeName, String objectId) {
217         if (assertIfClientNullForNodeName(nodeName)) {
218             return -1;
219         }
220
221         LOG.debug("Remove from currentFaults all faults for node/objectId: {}/{}", nodeName, objectId);
222         return eventRWFaultCurrentDB.remove(EsFaultCurrent.getQueryForOneNodeAndObjectId(nodeName, objectId));
223     }
224
225     /**
226      * Deliver list with all mountpoint/node-names in the database.
227      *
228      * @return List of all mountpoint/node-names the had active alarms.
229      */
230     @Override
231     public @Nonnull List<String> getAllNodesWithCurrentAlarms() {
232         if (assertIfClientNull("No DB, can not delete for all nodes", null)) {
233             return new ArrayList<>();
234         }
235
236         LOG.debug("Remove from currentFaults faults for all node");
237         List<String> nodeNames = new ArrayList<>();
238
239         for (FaultcurrentEntity fault : eventRWFaultCurrentDB.doReadAll().getHits()) {
240             String nodeName = fault.getNodeId();
241             if (!nodeNames.contains(nodeName)) {
242                 // this.clearFaultsCurrentOfNode(nodeName); -> Function shifted
243                 nodeNames.add(nodeName);
244             }
245         }
246         return nodeNames;
247     }
248
249     // -- Inventory and equipment current
250
251     /**
252      * write internal equipment to database
253      * 
254      * @param internalEquipment with mandatory fields.
255      */
256     @Override
257     public void writeInventory(Inventory internalEquipment) {
258
259         if (assertIfClientNullForNodeName(internalEquipment.getNodeId())) {
260             return;
261         }
262         if (internalEquipment.getManufacturerIdentifier() == null) {
263             internalEquipment = new InventoryBuilder(internalEquipment).setManufacturerIdentifier("").build();
264         }
265         if (internalEquipment.getDate() == null) {
266             internalEquipment = new InventoryBuilder(internalEquipment).setDate("").build();
267         }
268
269         eventRWEquipment.write(internalEquipment, internalEquipment.getNodeId() + "/" + internalEquipment.getUuid());
270     }
271
272
273     // -- Networkelement
274
275
276     /**
277      * join base with parameters of toJoin (only non null values)
278      * 
279      * @param base base object
280      * @param toJoin object with new property values
281      * @return new joined object
282      */
283     @SuppressWarnings("unused")
284     private NetworkElementConnectionEntity joinNe(NetworkElementConnectionEntity base,
285             NetworkElementConnectionEntity toJoin) {
286         if (base == null) {
287             return toJoin;
288         }
289         NetworkElementConnectionBuilder builder = new NetworkElementConnectionBuilder(base);
290         if (toJoin != null) {
291             if (toJoin.isIsRequired() != null) {
292                 builder.setIsRequired(toJoin.isIsRequired());
293             }
294             if (toJoin.getCoreModelCapability() != null) {
295                 builder.setCoreModelCapability(toJoin.getCoreModelCapability());
296             }
297             if (toJoin.getDeviceType() != null) {
298                 builder.setDeviceType(toJoin.getDeviceType());
299             }
300             if (toJoin.getHost() != null) {
301                 builder.setHost(toJoin.getHost());
302             }
303             if (toJoin.getNodeDetails() != null) {
304                 builder.setNodeDetails(toJoin.getNodeDetails());
305             }
306             if (toJoin.getPassword() != null) {
307                 builder.setPassword(toJoin.getPassword());
308             }
309             if (toJoin.getPort() != null) {
310                 builder.setPort(toJoin.getPort());
311             }
312             if (toJoin.getStatus() != null) {
313                 builder.setStatus(toJoin.getStatus());
314             }
315             if (toJoin.getUsername() != null) {
316                 builder.setUsername(toJoin.getUsername());
317             }
318         }
319         return builder.build();
320     }
321
322     /**
323      *
324      * @param networkElementConnectionEntitiy to wirte to DB
325      * @param nodeId Id for this DB element
326      */
327     @Override
328     public boolean updateNetworkConnectionDeviceType(NetworkElementConnectionEntity networkElementConnectionEntitiy,
329             String nodeId) {
330         return this.networkelementConnectionDB.update(networkElementConnectionEntitiy, nodeId) != null;
331         //              NetworkElementConnectionEntity e = this.networkelementConnectionDB.read(nodeId);
332         //              this.networkelementConnectionDB.write(this.joinNe(e, networkElementConnectionEntitiy), nodeId);
333     }
334
335     /**
336      * Update after new mountpoint registration
337      * 
338      * @param networkElementConnectionEntitiy data
339      * @param nodeId of device (mountpoint name)
340      */
341     @Override
342     public boolean updateNetworkConnection22(NetworkElementConnectionEntity networkElementConnectionEntitiy,
343             String nodeId) {
344         LOG.info("update networkelement-connection for {} with data {}", nodeId, networkElementConnectionEntitiy);
345         return this.networkelementConnectionDB.updateOrCreate(networkElementConnectionEntitiy, nodeId,
346                 Arrays.asList("is-required", "username", "password")) != null;
347         //              NetworkElementConnectionEntity e = this.networkelementConnectionDB.read(nodeId);
348         //              this.networkelementConnectionDB.write(this.joinNe(e, networkElementConnectionEntitiy), nodeId);
349
350     }
351
352     /* please do not remove */
353     //    public void cleanNetworkElementConnections() {
354     //        this.networkelementConnectionDB.remove(QueryBuilders.matchQuery("is-required", false));
355     //        CreateNetworkElementConnectionInput x = new CreateNetworkElementConnectionInputBuilder().setStatus(ConnectionLogStatus.Disconnected).build();
356     //        this.networkelementConnectionDB.update(x,QueryBuilders.matchAllQuery());
357     //    }
358
359     @Override
360     public void removeNetworkConnection(String nodeId) {
361         Boolean isRequired;
362         NetworkElementConnectionEntity e = this.networkelementConnectionDB.read(nodeId);
363         if (e != null && (isRequired = e.isIsRequired()) != null) {
364             if (isRequired) {
365                 LOG.debug("updating connection status for {} of required ne to disconnected", nodeId);
366                 this.networkelementConnectionDB.update(new UpdateNetworkElementConnectionInputBuilder()
367                         .setStatus(ConnectionLogStatus.Disconnected).build(), nodeId);
368             } else {
369                 LOG.debug("remove networkelement-connection for {} entry because of non-required", nodeId);
370                 this.networkelementConnectionDB.remove(nodeId);
371             }
372         } else {
373             LOG.warn("Unable to update connection-status. dbentry for {} not found in networkelement-connection",
374                     nodeId);
375         }
376     }
377
378     // -- Multiple areas
379
380     @Override
381     public int doIndexClean(Date olderAreOutdated) {
382
383         String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
384         int removed = 0;
385
386         QueryBuilder queryEventBase = EsEventBase.getQueryForTimeStamp(netconfTimeStamp);
387         removed += eventRWEventLogDevicemanager.remove(queryEventBase);
388
389         QueryBuilder queryFaultLog = EsFaultLogDevicemanager.getQueryForTimeStamp(netconfTimeStamp);
390         removed += eventRWFaultLogDB.remove(queryFaultLog);
391         return removed;
392     }
393
394     @Override
395     public int getNumberOfOldObjects(Date olderAreOutdated) {
396
397         String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
398         int numberOfElements = 0;
399
400         QueryBuilder queryEventBase = EsEventBase.getQueryForTimeStamp(netconfTimeStamp);
401         numberOfElements += eventRWEventLogDevicemanager.doReadAll(queryEventBase).getTotal();
402
403         QueryBuilder queryFaultLog = EsFaultLogDevicemanager.getQueryForTimeStamp(netconfTimeStamp);
404         numberOfElements += eventRWFaultLogDB.doReadAll(queryFaultLog).getTotal();
405
406         return numberOfElements;
407     }
408
409     // -- Helper
410
411     /**
412      * Verify status of client
413      * 
414      * @param event that is printed with message
415      * @return true if client is null
416      */
417     private boolean assertIfClientNull(Object event) {
418         return assertIfClientNull("No DB, can not write: {}", event);
419     }
420
421     private boolean assertIfClientNullForNodeName(Object object) {
422         return assertIfClientNull("No DB, can not handle node: {}", object);
423     }
424
425     /**
426      * Verify status of client
427      * 
428      * @param message to print including {} for object printout.
429      * @return true if client is null
430      */
431     private boolean assertIfClientNull(String message, Object object) {
432         if (client == null) {
433             LOG.debug(message, object);
434             return true;
435         }
436         return false;
437     }
438
439     // ### sub classes
440
441
442     private static class EsEventBase {
443         /**
444          * Query to get older Elements
445          * 
446          * @param netconfTimeStamp to identify older Elements
447          * @return QueryBuilder for older elements related to timestamp
448          */
449         private static QueryBuilder getQueryForTimeStamp(String netconfTimeStamp) {
450             return new RangeQueryBuilder("timestamp").lte(netconfTimeStamp);
451         }
452     }
453     private static class EsFaultLogDevicemanager {
454         /**
455          * Get older Elements
456          * 
457          * @param netconfTimeStamp to identify query elements older than this timestamp.
458          * @return QueryBuilder for related elements
459          */
460         public static QueryBuilder getQueryForTimeStamp(String netconfTimeStamp) {
461             return new RangeQueryBuilder("timestamp").lte(netconfTimeStamp);
462         }
463     }
464     public static class EsFaultCurrent {
465         /**
466          * @param nodeName name of the node
467          * @return query builder
468          */
469         public static QueryBuilder getQueryForOneNode(String nodeName) {
470             return QueryBuilders.matchQuery("node-id", nodeName);
471         }
472
473         public static QueryBuilder getQueryForOneNodeAndObjectId(String nodeName, String objectId) {
474             BoolQueryBuilder bq = QueryBuilders.boolQuery();
475             bq.must(QueryBuilders.matchQuery("node-id", nodeName));
476             bq.must(QueryBuilders.matchQuery("object-id", objectId));
477             return bq;
478         }
479     }
480
481     @Override
482     public List<NetworkElementConnectionEntity> getNetworkElementConnections() {
483         return this.networkelementConnectionDB.doReadAll().getHits();
484     }
485
486     @Override
487     public void doWritePerformanceData(List<PmdataEntity> list) {
488
489         list.forEach(elem -> {
490             GranularityPeriodType granularityPeriod = nnGetGranularityPeriodType(elem.getGranularityPeriod());
491             //_id": "Sim12600/LP-MWPS-TTP-01/2017-07-04T15:15:00.0+00:00"
492             StringBuffer id = new StringBuffer();
493             DateAndTime date = elem.getTimeStamp();
494             id.append(elem.getNodeName());
495             id.append("/");
496             id.append(elem.getUuidInterface());
497             id.append("/");
498             id.append(date != null ? date.getValue() : "null");
499
500             switch (granularityPeriod) {
501                 case Period15Min:
502                     pmData15mDB.write(elem, id.toString());
503                     break;
504                 case Period24Hours:
505                     pmData24hDB.write(elem, id.toString());
506                     break;
507                 case Unknown:
508                 default:
509                     LOG.debug("Unknown granularity {} id {}", granularityPeriod, id);
510                     break;
511             }
512         });
513
514     }
515
516     @NonNull
517     GranularityPeriodType nnGetGranularityPeriodType(@Nullable GranularityPeriodType granularityPeriod) {
518         return granularityPeriod != null ? granularityPeriod : GranularityPeriodType.Unknown;
519     }
520
521     @Override
522     public HtDatabaseClient getRawClient() {
523         return this.client;
524     }
525
526 }