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