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