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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.index.impl;
20 import java.util.Date;
21 import org.elasticsearch.index.query.QueryBuilder;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.archiveservice.ArchiveCleanProvider;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDataBaseReaderAndWriter;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseClientAbstract;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseNode;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.IndexClientBuilder;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.NetconfTimeStamp;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.index.database.types.EsEventOdluxLog;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Setup index mwtn in the database
37 public class IndexMwtnService implements AutoCloseable, ArchiveCleanProvider {
39 private static final Logger LOG = LoggerFactory.getLogger(IndexMwtnService.class);
40 private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter();
42 /** Index name to be used */
43 public static final String INDEX = "mwtn";
44 /** Location of mapping data **/
45 private static final String MAPPING = "elasticsearch/index/mwtn/mwtnMapping.json";
46 /** Location of configuration data **/
48 private final HtDataBaseReaderAndWriter<EsEventOdluxLog> eventRWOdluxLog;
51 private final HtDatabaseClientAbstract client;
53 // --- Construct and initialize
55 public IndexMwtnService(HtDatabaseNode database) throws Exception {
56 LOG.info("Create {} start", this.getClass().getSimpleName());
59 IndexClientBuilder clientBuilder = IndexClientBuilder.getBuilder(INDEX)
60 .setMappingSettingJsonFileName(MAPPING);
61 client = clientBuilder.create(database);
62 clientBuilder.close();
64 eventRWOdluxLog = new HtDataBaseReaderAndWriter<>(client, EsEventOdluxLog.ESDATATYPENAME, EsEventOdluxLog.class);
66 LOG.info("Create {} finished. DB Service sucessfully started.", this.getClass().getSimpleName());
70 * Get client to be used in other services
73 public HtDatabaseClientAbstract getClient() {
78 * Write into Odlux log, used by client
79 * @param logEntry as test data
81 public void writeOdluxEventForTestpurpose(EsEventOdluxLog logEntry) {
82 eventRWOdluxLog.doWrite(logEntry);
87 public void close() throws Exception {
94 public int doIndexClean(Date olderAreOutdated) {
96 String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
98 QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
99 int removed = eventRWOdluxLog.doRemoveByQuery(queryOdluxLog);
105 public int getNumberOfOldObjects(Date olderAreOutdated) {
107 String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
108 int numberOfElements = 0;
110 QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
111 numberOfElements += eventRWOdluxLog.doReadAll(queryOdluxLog).size();
113 return numberOfElements;