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 **/
47 private static final String MODELDATA = "elasticsearch/index/mwtn/modelDescription";
49 private final HtDataBaseReaderAndWriter<EsEventOdluxLog> eventRWOdluxLog;
52 private final HtDatabaseClientAbstract client;
54 // --- Construct and initialize
56 public IndexMwtnService(HtDatabaseNode database) throws Exception {
57 LOG.info("Create {} start", this.getClass().getSimpleName());
60 IndexClientBuilder clientBuilder = IndexClientBuilder.getBuilder(INDEX)
61 .setMappingSettingJsonFileName(MAPPING)
62 .setModelDataDirectory(MODELDATA);
63 client = clientBuilder.create(database);
64 clientBuilder.close();
66 eventRWOdluxLog = new HtDataBaseReaderAndWriter<>(client, EsEventOdluxLog.ESDATATYPENAME, EsEventOdluxLog.class);
68 LOG.info("Create {} finished. DB Service sucessfully started.", this.getClass().getSimpleName());
72 * Get client to be used in other services
75 public HtDatabaseClientAbstract getClient() {
80 * Write into Odlux log, used by client
81 * @param logEntry as test data
83 public void writeOdluxEventForTestpurpose(EsEventOdluxLog logEntry) {
84 eventRWOdluxLog.doWrite(logEntry);
89 public void close() throws Exception {
96 public int doIndexClean(Date olderAreOutdated) {
98 String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
100 QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
101 int removed = eventRWOdluxLog.doRemoveByQuery(queryOdluxLog);
107 public int getNumberOfOldObjects(Date olderAreOutdated) {
109 String netconfTimeStamp = NETCONFTIME_CONVERTER.getTimeStampAsNetconfString(olderAreOutdated);
110 int numberOfElements = 0;
112 QueryBuilder queryOdluxLog = EsEventOdluxLog.getQueryForTimeStamp(netconfTimeStamp);
113 numberOfElements += eventRWOdluxLog.doReadAll(queryOdluxLog).size();
115 return numberOfElements;